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

remove pub qualifier #4110

Closed

Conversation

JonathonAnderson
Copy link

I'm new to Rust, so forgive me if I'm wrong here, but from what I'm being told on the Discord server, the pub qualifier is unnecessary in the crate module when declaring the garden module because a parent module has access to a child module it declares, and the pub qualifier is only necessary for the parent to access elements within the child module, not to access the module itself. This was a source of confusion for me because the code compiled without pub, but the example gives the impression that it's required.

@StefanSalewski
Copy link

Great observation, I was wondering about it myself. I do not really understand your explanation. I think it is just, that pub is ONLY necessary to make CHILD modules visible. But "garden" is a toplevel module, so no pub should be required. (Pub is not needed for siblings modules at the same level as well. And as you said, child modules can always access ancestor modules, but then prefixes like super are needed. )

I assume that you just read the whole chapter, so maybe you can tell me what the sentence

"Declaring submodules: In any file other than the crate root, you can declare submodules. "

from https://doc.rust-lang.org/book/ch07-02-defining-modules-to-control-scope-and-privacy.html exactly should teach us? Of course I can create in main.rs a module, and inside that module a submodule?

Actually, the issue with the unneeded pub keyword and the strange sentence above are the only two points I marked when reading that chapter. Rust's module system is a bit complicated, it's not easy to explain. I recently tried to create a shorter and simpler text for a few friends who do not like to read long texts, but it is not easy. At least I avoided the restaurant metaphor, so they don't have to understand how a restaurant works to learn some Rust :-)

@chriskrycho
Copy link
Contributor

@JonathonAnderson, you’re correct that this particular pub modifier is unnecessary! This gets at one of the weird bits of Rust’s module system, because declaring something pub in main.rs is more or less never necessary, whereas it makes a very big difference in a lib.rs, where it’s defining the public API for the package.

In this case, it’s fine either way! It doesn’t hurt to have the pub on the declaration, but it isn’t strictly necessary either. In the introductory code, though, it avoids having to explain that it is not necessary here but is necessary on pub mod vegetable in src/garden.rs. The rest of the chapter gets into that much more, but right here, it would be a distraction, since this is trying to be just an overview and introduction. Net, I’m going to leave it as is, but it was a totally reasonable suggestion.


@StefanSalewski, to your comment:

…maybe you can tell me what the sentence

"Declaring submodules: In any file other than the crate root, you can declare submodules. "

from https://doc.rust-lang.org/book/ch07-02-defining-modules-to-control-scope-and-privacy.html exactly should teach us? Of course I can create in main.rs a module, and inside that module a submodule?

Totally fair! The distinction is exactly as you surmised: between modules, at the top-level, and submodules, children of those top-level modules, as you note. That distinction is rarely important in its own right, but depending on the reader’s background, the relationship between them might not be the “of course” that it was to you or me! In particular (as I expect you know from some of the other languages I think I’ve seen you reference on the repo), it’s quite different from how other languages handle declarations of modules and submodules and so on!

At least I avoided the restaurant metaphor, so they don't have to understand how a restaurant works to learn some Rust

That kind of analogy helps some readers and just gets in the way for others. Can’t win them all! 😅

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

Successfully merging this pull request may close these issues.

3 participants