Can’t understand Rust module system

Short answer: you don’t need the mod phone in phone.rs. Just remove that and your code will work (assuming the rest of the code is correct).

Longer answer: The following code in main.rs:

pub mod phone;

is equivalent to:

pub mod phone {
    // literally insert the contents of phone.rs here
}

so you don’t need to wrap everything in phone.rs in mod phone {}.

Your current code translates to:

pub mod phone {
    pub mod phone {
        pub struct Battery { ... }
        ...
    }
}

which means you need to access Battery as phone::phone::Battery (and Display as phone::phone::Display, etc.) in main.rs.

Leave a Comment