Can't understand Rust module system

后端 未结 1 1139
深忆病人
深忆病人 2020-12-07 03:30

I created a simple project for educational purpose, so I have a main function and 3 traits Battery, Display and GSM and implementation

相关标签:
1条回答
  • 2020-12-07 04:03

    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.

    0 讨论(0)
提交回复
热议问题