Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ch07-03] clarify private items usage inside child modules #3701

Closed
wants to merge 1 commit into from

Conversation

said026
Copy link

@said026 said026 commented Jul 14, 2023

Add some clarification regarding the usage of private items inside child modules

Copy link
Contributor

@chriskrycho chriskrycho left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the suggestion! I think in this case we'll leave it as is. It's actually not just about where the items are defined, but about what privacy they have in that module. Consider:

// pub or not does not matter to children
struct InRoot;

mod child {
    mod grandchild {
        // public anywhere that `child` module chooses to expose them
        pub struct PubInGrandchildAndChild;
        pub struct OnlyPubInGrandchild;
        
        // *only* public where
        pub(super) struct CanOnlyBeUsedInChild;
        
        // Not accessible outside `grandchild`;
        struct PrivateInGrandchild;
    }
    
    // This makes `PubInGrandchildAndChild` visible outside `child`
    pub use grandchild::PubInGrandchildAndChild;
    
    // this is not legal
    // pub use grandchild::PrivateInGrandchild;
    
    // neither is this, though:
    // pub use grandchild::CanOnlyBeUsedInChild;
}

Notice that we can use PubInGrandchildAndChild in the root regardless of where it was defined (in this case, in child::grandchild) because it was defined in a way that it could be exported to a level of visibility that allows the root to see it. By the same token, even though OnlyPubInGrandchild is defined exactly the same way as PubInGrandchildAndChild, it is not visible in the root because child does not re-export it. The same basic dynamics are in play in general throughout the module system. You can mess around with this example and these details in this playground.

Hope that helps clarify; Rust’s module system is definitely a bit confusing at times! And thanks again for the suggestion!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants