Module 如何在模块中拆分代码?
我正在阅读,我在第7.2章,但我肯定遗漏了什么,因为我无法在模块中组织我的代码,编译器(rustc 1.32.0)不断给我错误 我所读的Module 如何在模块中拆分代码?,module,compiler-errors,rust,Module,Compiler Errors,Rust,我正在阅读,我在第7.2章,但我肯定遗漏了什么,因为我无法在模块中组织我的代码,编译器(rustc 1.32.0)不断给我错误 我所读的 我读了rustc--explain E0433,这是编译器的建议,但我仍然无法解决它 我检查了一下,似乎我的代码是正确的,(my/mod.rs正在自己的文件夹中使用模块my/nested.rs) 我在网上找到了一些信息,但都是4年前的,其中包括use,这本书还没有收录 我也检查过了,但我没有使用文件夹,而且,它也没有从书中解释出来 最小示例 这是一个尝试模
- 我读了
,这是编译器的建议,但我仍然无法解决它rustc--explain E0433
- 我检查了一下,似乎我的代码是正确的,(
正在自己的文件夹中使用模块my/mod.rs
)my/nested.rs
- 我在网上找到了一些信息,但都是4年前的,其中包括
,这本书还没有收录use
- 我也检查过了,但我没有使用文件夹,而且,它也没有从书中解释出来
/src/main.rs
和/src/m.rs
梅因
mod m;
fn main() {
let st_0 = m::St::new();
}
pub mod m {
pub struct St {
a:i32,
b:i32,
}
impl St {
pub fn new() -> St{
println!("New St!");
St {
a: 12,
b: 13,
}
}
}
}
mod m;
fn main() {
let st_0 = m::St::new();
}
m、 rs
这就是cargo
告诉我的:
Compiling min v0.1.0 (/home/user/min)
error[E0433]: failed to resolve: could not find `St` in `m`
--> src/main.rs:3:19
|
3 | let st_0 = m::St::new();
| ^^ could not find `St` in `m`
error: aborting due to previous error
For more information about this error, try `rustc --explain E0433`.
error: Could not compile `min`.
To learn more, run the command again with --verbose.
当您将所有内容都保存在一个文件中时,如下所示:
pub struct St {
a:i32,
b:i32,
}
impl St {
pub fn new() -> St{
println!("New St!");
St {
a: 12,
b: 13,
}
}
}
梅因
mod m;
fn main() {
let st_0 = m::St::new();
}
pub mod m {
pub struct St {
a:i32,
b:i32,
}
impl St {
pub fn new() -> St{
println!("New St!");
St {
a: 12,
b: 13,
}
}
}
}
mod m;
fn main() {
let st_0 = m::St::new();
}
您可以使用
pub mod mode_name {
//code...
}
一旦将模块放入另一个文件中,包装就会消失,显示它,但是如果您不仔细查看或者编程时喝醉了,您可能会与嵌套模块的pub mod instrument{…}
混淆
所以m.R必须看起来像这样:
pub struct St {
a:i32,
b:i32,
}
impl St {
pub fn new() -> St{
println!("New St!");
St {
a: 12,
b: 13,
}
}
}
这肯定是一个复制品,我以前没看到过。我该怎么处理我的问题?删除它?