Why does “can't leak private type” only apply to structs and not enums?

前端 未结 1 1662
不知归路
不知归路 2020-12-20 14:22

In Learning Rust With Entirely Too Many Linked Lists, they show that a pub enum can\'t hold a private struct:,

struct Node {
    el         


        
相关标签:
1条回答
  • 2020-12-20 15:15

    In the first example, the enum List is public. That means that the enum variant More is also public. However, More cannot be used by external code because Node isn't public. Thus, you have a thing that is externally visible, but can't actually be used, which is probably not what you wanted.

    In the second example, the struct List is public. However, the head field is not public. Thus, it doesn't matter whether Link is public or not, because external code can't see the head field in the first place.

    0 讨论(0)
提交回复
热议问题