rust-lang / book

The Rust Programming Language

Home Page:https://doc.rust-lang.org/book/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

The Modules Cheat Sheet describes the declaration of submodules as more restricted than it is

agoodwin opened this issue · comments

  • I have searched open and closed issues and pull requests for duplicates, using these search terms:
    • In any file other than the crate root, you can declare submodules
    • declaring submodules
    • submodules
  • I have checked the latest main branch to see if this has already been fixed, in this file:
    • src/ch07-02-defining-modules-to-control-scope-and-privacy.md

URL to the section(s) of the book with this problem:
Link to published page
Link to line of source code

Description of the problem:

I may be missing the intent of this line, but from what I can tell the line in this chapter that says:

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

Isn't actually true.

It seems to me that you can declare a submodule inline in the crate root, as is done just a few paragraphs later in Listing 7-1 (i.e. in the front_of_house module example, the hosting and serving submodules are declared inline in the crate root file of src/lib.rs).

From what I can tell, submodules can also be declared in the crate root file even if they aren't declared inline, and are defined using the subfolder and file organisation approaches (though it does get pretty messy doing that, I don't know why anyone would want to).

I think the only way to define submodules in the crate root file is when the parent module is declared inline in the crate root file, so I guess that's maybe what this line was trying to get across?

Suggested fix:

I think it would be more straightforward if the line was replaced with something like the following:

"You can also declare submodules within other modules."

So removing any implied link between submodules and the crate root file, given you can in practice achieve submodules both with and without directly touching the crate root file.

(I saw from the other issues I found in the search that a couple of years back you've said you weren't revisiting this section of the book anytime soon, but as this is such a small suggestion I thought I'd raise it anyway.)

I think the distinction causing confusion is "declaring modules" vs "declaring submodules". My intent is for "submodules" to mean "modules within a module that isn't the crate root". Furthermore, the cheat sheet is meant to describe one way to use modules that will always work, not all possible ways that will work.