如果您有这样的目录结构:
src/main.rs
src/module1/blah.rs
src/module1/blah2.rs
src/utils/logging.rs
Run Code Online (Sandbox Code Playgroud)
你如何使用其他文件中的函数?
从Rust教程,听起来我应该能够做到这一点:
main.rs
mod utils { pub mod logging; }
mod module1 { pub mod blah; }
fn main() {
utils::logging::trace("Logging works");
module1::blah::doit();
}
Run Code Online (Sandbox Code Playgroud)
logging.rs
pub fn trace(msg: &str) {
println!(": {}\n", msg);
}
Run Code Online (Sandbox Code Playgroud)
blah.rs
mod blah2;
pub fn doit() {
blah2::doit();
}
Run Code Online (Sandbox Code Playgroud)
blah2.rs
mod utils { pub mod logging; }
pub fn doit() {
utils::logging::trace("Blah2 invoked");
}
Run Code Online (Sandbox Code Playgroud)
但是,这会产生错误:
error[E0583]: file not found for module `logging`
--> src/main.rs:1:21
|
1 | mod utils { pub …Run Code Online (Sandbox Code Playgroud) 这是我尝试cargo test在项目中运行时遇到的错误。这是什么意思?我如何解决它?
我可以尝试更新更多细节,但不幸的是,我无法用最小的例子重现它......
完整错误:
cargo test
Compiling ranges v0.1.0 (file:///Users/user/code/rust-project)
error: cannot satisfy dependencies so `std` only shows up once
|
= help: having upstream crates all available in one format will likely make this go away
error: cannot satisfy dependencies so `core` only shows up once
|
= help: having upstream crates all available in one format will likely make this go away
error: cannot satisfy dependencies so `collections` only shows up once
|
= help: having …Run Code Online (Sandbox Code Playgroud) 我正在试图弄清楚如何在Rust中编译多文件包,但我不断收到编译错误.
我有要导入到crate thing.rs的文件:
mod asdf {
pub enum stuff {
One,
Two,
Three
}
}
Run Code Online (Sandbox Code Playgroud)
我的包文件test.rc:
mod thing;
use thing::asdf::*;
fn main(){
}
Run Code Online (Sandbox Code Playgroud)
当我运行Rust build test.rc时,我得到:
test.rc:3:0: 3:19 error: `use` and `extern mod` declarations must precede items
test.rc:3 use thing::asdf::*;
^~~~~~~~~~~~~~~~~~~
error: aborting due to previous error
Run Code Online (Sandbox Code Playgroud)
关于模块,板条箱和使用工作的方式显然有些简单,我只是没有得到.我的理解是mod某事; 对于同一目录或extern mod中的文件; 对于库路径上的库导致目标文件被链接.然后使用将允许您将模块的部分导入当前文件,函数或模块.这似乎适用于核心库中的东西.
这是使用版本0.6的Rust编译器.
rust ×3