book icon indicating copy to clipboard operation
book copied to clipboard

Potentially incorrect text in Chapter 7.4: Bringing Paths into Scope with the `use` Keyword

Open Mindstormer619 opened this issue 2 years ago • 0 comments

  • I have searched open and closed issues and pull requests for duplicates, using these search terms:
    • 7.4
    • Before this change, external code would have to call
  • I have checked the latest main branch to see if this has already been fixed, in this file:
    • https://github.com/rust-lang/book/blob/a60f4316ec923a5ac2ed6a2eba6960edb832d855/src/ch07-04-bringing-paths-into-scope-with-the-use-keyword.md?plain=1#L158

URL to the section(s) of the book with this problem: https://doc.rust-lang.org/book/ch07-04-bringing-paths-into-scope-with-the-use-keyword.html#bringing-paths-into-scope-with-the-use-keyword

Description of the problem:

The problem is in the segment: Before this change, external code would have to call the add_to_waitlist function by using the path restaurant::front_of_house::hosting::add_to_waitlist(). External code cannot call the add_to_waitlist() function because the front_of_house module is not marked pub. Code to demonstrate:

lib.rs

mod front_of_house {
    pub mod hosting {
        pub fn add_to_waitlist() {}
    }
}

use crate::front_of_house::hosting;

pub fn eat_at_restaurant() {
    hosting::add_to_waitlist();
}

main.rs

fn main() {
    backyard::front_of_house::hosting::add_to_waitlist(); // does not compile
}

Error:

minds@Zero-Requiem MINGW64 /d/Workspace/Rust/RustBook/backyard (main)
$ cargo c
    Checking backyard v0.1.0 (D:\Workspace\Rust\RustBook\backyard)
error[E0603]: module `front_of_house` is private
 --> src\main.rs:2:15
  |
2 |     backyard::front_of_house::hosting::add_to_waitlist();
  |               ^^^^^^^^^^^^^^ private module
  |
note: the module `front_of_house` is defined here
 --> d:\Workspace\Rust\RustBook\backyard\src\lib.rs:1:1
  |
1 | mod front_of_house {
  | ^^^^^^^^^^^^^^^^^^

Suggested fix:

pub needs to be added before mod front_of_house.

Mindstormer619 avatar Dec 03 '22 14:12 Mindstormer619