Pla*_*rth 3 module traits rust
我创建了一个用于教育目的的简单项目,因此我有一个主要功能和3个特征Battery,Display以及GSM它们的实现.我希望main函数位于文件main.rs中,而另一个文件名为phone.rs中的3个特征:
phone.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();
}
}
}
Run Code Online (Sandbox Code Playgroud)
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();
}
Run Code Online (Sandbox Code Playgroud)
起初,一切都在一个文件中(main.rs)和一切工作得很好(有例如一些差异我加phone::在前面Display,GSM并Battery在main.rs),当我移动的特点,并增加mod phone{}在phone.rs,和pub mod phone在main.rs中,我从编译器中得到以下错误:
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();
Run Code Online (Sandbox Code Playgroud)
我不明白他们,我在这里和谷歌搜索,但我找不到解决方案.此外,我得到很多警告,电话中的方法从未使用#[warn(dead_code)],任何帮助表示赞赏.
Dog*_*ert 10
简短的回答:你不需要mod phonein phone.rs.只需删除它,您的代码就可以工作(假设其余代码是正确的).
更长的答案:以下代码main.rs:
pub mod phone;
Run Code Online (Sandbox Code Playgroud)
相当于:
pub mod phone {
// literally insert the contents of phone.rs here
}
Run Code Online (Sandbox Code Playgroud)
所以你不需要把所有东西都包phone.rs在里面mod phone {}.
您当前的代码转换为:
pub mod phone {
pub mod phone {
pub struct Battery { ... }
...
}
}
Run Code Online (Sandbox Code Playgroud)
这意味着你需要访问Battery的phone::phone::Battery(和Display作为phone::phone::Display等)main.rs.