无法理解Rust模块系统 [英] Can't understand Rust module system
问题描述
我创建了一个用于教育目的的简单项目,因此我有一个主要功能和3个特质Battery
,Display
和GSM
及其实现.我希望主要功能位于文件main.rs中,并将3个特征保存在另一个名为phone.rs的文件中:
I created a simple project for educational purpose, so I have a main function and 3 traits Battery
, Display
and GSM
and implementations for them. I want the the main function to be in file main.rs and the 3 traits in another file called phone.rs:
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();
}
}
}
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中的Display
,GSM
和Battery
前面添加了phone::
),当我移动特征并在phone.rs中添加mod phone{}
并在main.rs中添加pub mod phone
时,我从编译器中收到以下错误:
At first, everything was in one file (main.rs) and everything worked fine (there was some difference for example I added phone::
in front of Display
, GSM
and Battery
in main.rs), when I moved the traits and added mod phone{}
in phone.rs, and pub mod phone
in main.rs, I got the following errors from the compiler:
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();
我不理解它们,我在这里和Google中进行了搜索,但是找不到解决方案.同样,我也收到很多警告,表示永远不要使用phone.rs中的方法#[warn(dead_code)]
,我们将提供任何帮助.
I don't understand them, I searched here and in Google, but I failed to find a solution. Also I get a lot of warning that the methods in phone.rs are never used #[warn(dead_code)]
, any help is appreciated.
推荐答案
简短的回答:您不需要phone.rs
中的mod phone
.只需删除它,您的代码就可以使用(假设其余代码正确).
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).
更长的答案:main.rs
中的以下代码:
Longer answer: The following code in main.rs
:
pub mod phone;
等效于:
pub mod phone {
// literally insert the contents of phone.rs here
}
因此您无需将所有内容都包装在mod phone {}
中的phone.rs
中.
so you don't need to wrap everything in phone.rs
in mod phone {}
.
您当前的代码转换为:
pub mod phone {
pub mod phone {
pub struct Battery { ... }
...
}
}
这意味着您需要在main.rs
中以phone::phone::Battery
的身份访问Battery
(以phone::phone::Display
的身份访问Display
,等等).
which means you need to access Battery
as phone::phone::Battery
(and Display
as phone::phone::Display
, etc.) in main.rs
.
这篇关于无法理解Rust模块系统的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!