我习惯于根据问题域将文件组织在单独的目录中(例如,将图像处理资料分组在一起,将IO资料放在另一个目录中等等)。我不确定在Rust中是否建议采用这种方式或组织。

我已经在多个目录中设置了我的项目:

- helloworld
    - Cargo.toml
    - src
        - main.rs
        - test
            - one.rs

我正在尝试使用one.rs中的main.rs中的函数


fn test() {
    println!("Calling test...");
}

main.rs
use test::one::*;

fn main() {
    println!("Hello, world!");
    test();
}

这会导致编译时错误:

error[E0432]: unresolved import `test::one::*`
 --> src/main.rs:1:5
  |
1 | use test::one::*;
  |     ^^^^^^^^^^^^^ Maybe a missing `extern crate test;`?

error[E0425]: cannot find function `test` in this scope
 --> src/main.rs:6:5
  |
6 |     test();
  |     ^^^^ not found in this scope

看一些在线项目,似乎这样的事情应该是可能的。

最佳答案

可能的,但是您需要使用以下方法将代码内部的其他模块告知您的代码:

mod test;

然后创建其他文件
// src/test/mod.rs

pub mod one;

关于rust - 在多个目录中组织Rust代码时无法解析的导入,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45637192/

10-15 12:18