我在/src/lib.rs
中得到了这个 crate ,我正在尝试对其运行测试:
#![crate_type = "lib"]
#![crate_name = "mycrate"]
pub mod mycrate {
pub struct Struct {
field: i32,
}
impl Struct {
pub fn new(n: i32) -> Struct {
Struct { field: n }
}
}
}
/tests/test.rs
中的测试文件:
extern crate mycrate;
use mycrate::*;
#[test]
fn test() {
...
}
运行 cargo test
给出了这个错误:
tests/test.rs:3:5: 3:16 error: import `mycrate` conflicts with imported crate in this module (maybe you meant `use mycrate::*`?) [E0254]
tests/test.rs:3 use mycrate::*;
^~~~~~~~~
我在这里做错了什么?
最佳答案
一个 crate 也自动成为一个有自己名字的模块。所以你不需要指定一个子模块。由于您导入了 mycrate
crate 中的所有内容,因此您还导入了 mycrate::mycrate
模块,这导致了命名冲突。
只需将 src/lib.rs
的内容更改为
pub struct Struct {
field: i32,
}
impl Struct {
pub fn new(n: i32) -> Struct {
Struct { field: n }
}
}
也不需要 crate_name
和 crate_type
属性。
关于unit-testing - 测试自定义箱子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34723575/