Rust 如何从tests目录中的文件访问src目录中的文件?

Rust 如何从tests目录中的文件访问src目录中的文件?,rust,rust-cargo,Rust,Rust Cargo,我的项目布局如下所示: src/ int_rle.rs lib.rs tests/ test_int_rle.rs 该项目使用cargo build编译,但我无法使用cargo test运行测试。我得到了错误 error[E0432]: unresolved import `int_rle`. There is no `int_rle` in the crate root --> tests/test_int_rle.rs:1:5 | 1 | use int

我的项目布局如下所示:

src/
    int_rle.rs
    lib.rs
tests/
    test_int_rle.rs
该项目使用
cargo build
编译,但我无法使用
cargo test
运行测试。我得到了错误

error[E0432]: unresolved import `int_rle`. There is no `int_rle` in the crate root
 --> tests/test_int_rle.rs:1:5
  |
1 | use int_rle;
  |     ^^^^^^^

error[E0433]: failed to resolve. Use of undeclared type or module `int_rle`
 --> tests/test_int_rle.rs:7:9
  |
7 |         int_rle::IntRle { values: vec![1, 2, 3] }
  |         ^^^^^^^^^^^^^^^ Use of undeclared type or module `int_rle`

error: aborting due to 2 previous errors

error: Could not compile `minimal_example_test_directories`.
我的代码:

// src/lib.rs
pub mod int_rle;

// src/int_rle.rs

#[derive(Debug, PartialEq)]
pub struct IntRle {
    pub values: Vec<i32>,
}

// tests/test_int_rle.rs
use int_rle;

#[cfg(test)]
mod tests {
    #[test]
    fn it_works() {
        int_rle::IntRle { values: vec![1, 2, 3] }
    }
}

// Cargo.toml
[package]
name = "minimal_example_test_directories"
version = "0.1.0"
authors = ["Johann Gambolputty de von Ausfern ... von Hautkopft of Ulm"]

[dependencies]
//src/lib.rs
国际酒吧;
//src/int_rle.rs
#[派生(调试,部分Q)]
pub-struct-IntRle{
发布值:Vec,
}
//测试/测试接口
使用int_rle;
#[cfg(测试)]
模试验{
#[测试]
fn it_works(){
int_rle::IntRle{values:vec![1,2,3]}
}
}
//货舱
[套餐]
name=“最小\u示例\u测试\u目录”
version=“0.1.0”
作者=[“乌尔姆大学的约翰·甘波尔普蒂·德·冯·奥斯芬……冯·霍特科普夫”]
[依赖关系]

相关:(如果测试文件和源文件位于同一文件夹中,如何执行。)

文件
src/int_rle.rs
src/lib.rs
构成您的库,它们一起被称为板条箱

您的测试和示例文件夹不被视为板条箱的一部分。这很好,因为当有人使用你的库时,他们不需要你的测试,他们只需要你的库

您可以通过添加行
extern-crater-minimal\u-example\u-test\u目录来解决问题
测试/test\u int\rle.rs
的顶部

你可以在书中阅读更多关于Rust的板条箱和模块结构的信息

这应该是测试文件的工作版本:

// tests/test_int_rle.rs
extern crate minimal_example_test_directories;

pub use minimal_example_test_directories::int_rle;

#[cfg(test)]
mod tests {
    #[test]
    fn it_works() {
        super::int_rle::IntRle { values: vec![1, 2, 3] };
    }
}

还尝试了
使用(super | self | src):
测试/test|int|rle.rs
文件中的:int|rle
。模块不能自动访问其父模块有权访问的符号。您是否尝试过移动
使用int\rle
mod tests{
之后?这个问题涉及不同文件夹中的文件,但我无法让它工作:(请参阅我对答案的评论)。对不起,我应该指定然后导入需要从板条箱名称进行。
在测试函数体中使用未声明的类型或模块int\u rle
。我认为
pub Use min..::int\u rle
修复了它,如果我在测试模块中使用
super::int\u rle::IntRle
)你能确认一下,然后更改你的答案,我会更新吗接受和接受?