这个简单的例子适用于main.rs
pub mod types {
pub struct Foo {
_var: usize,
}
}
use types::Foo;
fn main() {
let _unused: Foo;
println!("Hello, world!");
}
但是,当它编译到一个自身为模块的文件中时,会出现一个错误:
error: unresolved import `types::Foo`. Maybe a missing `extern crate types`?
是否可以从模块引用子模块的公共成员?
最佳答案
在模块中,需要使用self
,例如:
pub mod types {
pub struct Foo {
_var: usize,
}
}
use self::types::Foo; // <-- self here
感谢IRC上的@nox给出了这个答案。