无法理解Rust模块系统

问题描述 投票:0回答:2

我出于教育目的创建了一个简单的项目,所以我有一个主要功能和 3 个特征

Battery
Display
GSM
以及它们的实现。我希望 main 函数位于文件 main.rs 中,而 3 个特征位于另一个名为 phone.rs 的文件中:

电话.rs

mod phone{
    pub struct Battery{
        model : String,
        hours_idle : i16,
        hours_talk : i16
    }

    pub struct Display{
        size : i16,
        number_of_colors : i32
    }

    pub struct GSM{
        model : String,
        manufactor : String,
        price : f32,
        owner : String,
        battery: Battery,
        display : Display
    }

     impl Battery {
        pub fn new(model : String, hours_idle : i16, hours_talk : i16) -> Battery{
            Battery {model : model, hours_idle : hours_idle ,hours_talk : hours_talk}
        }
        pub fn print(&self){
            println!("Battery model: {}", self.model);
            println!("Hours idle: {}", self.hours_idle);
            println!("hours talk: {}", self.hours_talk);
        }
    }

    impl Display {
        pub fn new(size: i16, number_of_colors : i32) -> Display{
            Display{size : size, number_of_colors : number_of_colors}
        }
        pub fn print(&self){
            println!("size: {}", self.size);
            println!("number_of_colors: {}", self.number_of_colors);
        }
    }

     impl GSM {
        pub fn new(model : String, manufactor : String, price : f32, owner : String, battery : Battery, display : Display) -> GSM{
            GSM{model : model, manufactor : manufactor, price : price, owner : owner, battery : battery, display : display }
        }
        pub fn print(&self){
            println!("Model: {}, Manufactor: {}", self.model, self.manufactor);
            println!("price: {}", self.price);
            println!("owner: {}", self.owner);
            self.battery.print();
            self.display.print();
        }
    }
}

main.rs

pub mod phone;
fn main(){
    let battey = phone::Battery::new("modelBattery".to_string(), 1,2);
    let display =  phone::Display::new(10,20);
    //model : String, manufactor : String, price : f32, owner : String, baterry : Battery, display : Display
    let gsm =  phone::GSM::new("modelPhone".to_string(), "manufactor".to_string(), 100.0, "owner".to_string(), battey, display);
    gsm.print();
}

一开始,所有内容都在一个文件(main.rs)中,并且一切正常(有一些区别,例如我在 main.rs 中的

phone::
Display
GSM
前面添加了
Battery
),当我移动特征并在phone.rs中添加
mod phone{}
和在main.rs中添加
pub mod phone
时,我从编译器中收到以下错误:

src\main.rs:3:18: 3:37 error: failed to resolve. Could not find `Battery` in `phone` [E0433]
src\main.rs:3     let battey = phone::Battery::new("modelBattery".to_string(), 1,2);
                               ^~~~~~~~~~~~~~~~~~~
src\main.rs:3:18: 3:37 help: run `rustc --explain E0433` to see a detailed explanation
src\main.rs:3:18: 3:37 error: unresolved name `phone::Battery::new` [E0425]
src\main.rs:3     let battey = phone::Battery::new("modelBattery".to_string(), 1,2);
                               ^~~~~~~~~~~~~~~~~~~
src\main.rs:3:18: 3:37 help: run `rustc --explain E0425` to see a detailed explanation
src\main.rs:4:20: 4:39 error: failed to resolve. Could not find `Display` in `phone` [E0433]
src\main.rs:4     let display =  phone::Display::new(10,20);
                                 ^~~~~~~~~~~~~~~~~~~
src\main.rs:4:20: 4:39 help: run `rustc --explain E0433` to see a detailed explanation
src\main.rs:4:20: 4:39 error: unresolved name `phone::Display::new` [E0425]
src\main.rs:4     let display =  phone::Display::new(10,20);
                                 ^~~~~~~~~~~~~~~~~~~
src\main.rs:4:20: 4:39 help: run `rustc --explain E0425` to see a detailed explanation
src\main.rs:6:16: 6:31 error: failed to resolve. Could not find `GSM` in `phone` [E0433]
src\main.rs:6     let gsm =  phone::GSM::new("modelPhone".to_string(), "manufactor".to_string(), 100.0, "owner".to_string(), battey, display);
                             ^~~~~~~~~~~~~~~
src\main.rs:6:16: 6:31 help: run `rustc --explain E0433` to see a detailed explanation
src\main.rs:6:16: 6:31 error: unresolved name `phone::GSM::new` [E0425]
src\main.rs:6     let gsm =  phone::GSM::new("modelPhone".to_string(), "manufactor".to_string(), 100.0, "owner".to_string(), battey, display);
                             ^~~~~~~~~~~~~~~
src\main.rs:6:16: 6:31 help: run `rustc --explain E0425` to see a detailed explanation
src\main.rs:7:5: 7:16 error: the type of this value must be known in this context
src\main.rs:7     gsm.print();

我不明白它们,我在这里和谷歌中搜索,但没有找到解决方案。另外,我收到很多警告,称从未使用过phone.rs中的方法

#[warn(dead_code)]
,感谢任何帮助。

module rust traits
2个回答
13
投票

简短回答:您不需要

mod phone
中的
phone.rs
。只需删除它,您的代码就可以工作(假设代码的其余部分是正确的)。

更长的答案:

main.rs
中的以下代码:

pub mod phone;

相当于:

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

所以你不需要将所有内容都包裹在

phone.rs
mod phone {}

您当前的代码翻译为:

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

这意味着您需要在

Battery
中将
phone::phone::Battery
访问为
Display
(将
phone::phone::Display
访问为
main.rs
等)。


0
投票

正如您在 main.rs 中所写的那样

pub mod phone;

Rust 模块系统将开始在与 main.rs 相同的目录级别查找名为phone.rs 的文件。并将在 main.rs 中包含来自phone.rs 的代码。 由于phone.rs代码被包裹在

mod phone{...}

因此需要像phone::phone::GSM 一样访问。为了防止这种情况,只需删除 modphone{} 并在顶层获取phone.rs 的代码,它应该可以工作!!

© www.soinside.com 2019 - 2024. All rights reserved.