我如何 "export" 子模块中的东西?
How do I "export" things from a submodule?
我想写一个 mod.rs
文件,例如:
pub use foo::*;
mod foo;
pub mod bar;
但我收到错误 未解决的导入 <code>foo
。正确的做法是什么?
这是您的问题的 MCVE:
pub mod sub {
use foo::function;
pub mod foo {
pub fn function() {}
}
}
fn main() {}
如Adrian mentions,解决方法是在use
语句中使用关键字self
:
pub mod sub {
use self::foo::function;
pub mod foo {
pub fn function() {}
}
}
fn main() {}
所以,这是怎么回事? The Rust Programming Language 问题描述:
What about the self
? Well, by default, use declarations are absolute paths, starting from your crate root. self
makes that path relative to your current place in the hierarchy instead.
也就是说,use foo
意味着从 crate 的根开始使用 foo
。 use self::foo
表示相对于当前模块使用foo
。
我想写一个 mod.rs
文件,例如:
pub use foo::*;
mod foo;
pub mod bar;
但我收到错误 未解决的导入 <code>foo
。正确的做法是什么?
这是您的问题的 MCVE:
pub mod sub {
use foo::function;
pub mod foo {
pub fn function() {}
}
}
fn main() {}
如Adrian mentions,解决方法是在use
语句中使用关键字self
:
pub mod sub {
use self::foo::function;
pub mod foo {
pub fn function() {}
}
}
fn main() {}
所以,这是怎么回事? The Rust Programming Language 问题描述:
What about the
self
? Well, by default, use declarations are absolute paths, starting from your crate root.self
makes that path relative to your current place in the hierarchy instead.
也就是说,use foo
意味着从 crate 的根开始使用 foo
。 use self::foo
表示相对于当前模块使用foo
。