我正在尝试将锈蚀中的一个示例项目配置为工作项目。
我的结构是:
src/potter.rs
tests/tests.rs
还有我的Cargo.toml
[package]
name = "potter"
version = "0.1.0"
authors = ["my name"]
[dependencies]
我的potter.rs
包含:
pub mod potter {
pub struct Potter {
}
impl Potter {
pub fn new() -> Potter {
return Potter {};
}
}
}
我的tests.rs
包含:
use potter::Potter;
#[test]
fn it_works() {
let pot = potter::Potter::new();
assert_eq!(2 + 2, 4);
}
但我收到了这个错误:
error[E0432]: unresolved import `potter`
--> tests/tests.rs:1:5
|
1 | use potter::Potter;
| ^^^^^^ Maybe a missing `extern crate potter;`?
error[E0433]: failed to resolve. Use of undeclared type or module `potter`
--> tests/tests.rs:6:19
|
6 | let pot = potter::Potter::new();
| ^^^^^^ Use of undeclared type or module `potter`
warning: unused import: `potter::Potter`
--> tests/tests.rs:1:5
|
1 | use potter::Potter;
| ^^^^^^^^^^^^^^
|
= note: #[warn(unused_imports)] on by default
如果我加上extern crate potter;
,它不会修复任何.
error[E0463]: can't find crate for `potter`
--> tests/tests.rs:1:1
|
1 | extern crate potter;
| ^^^^^^^^^^^^^^^^^^^^ can't find crate
发布于 2017-10-21 12:52:24
回去,关于包、板条箱、模块和文件系统。
共同痛点:
lib.rs
定义了与机箱同名的模块;mod.rs
定义了与其所在目录同名的模块;其他每个文件都定义了文件名的模块。lib.rs
;二进制板条箱可以使用main.rs
。要解决您的问题:
src/potter.rs
移动到src/lib.rs
。pub mod potter
中删除src/lib.rs
。不严格要求,但消除不必要的嵌套模块。extern crate potter
添加到集成测试tests/tests.rs
(只有在使用Rust 2015时才需要)。文件系统
├── Cargo.lock
├── Cargo.toml
├── src
│ └── lib.rs
├── target
└── tests
└── tests.rs
src/lib.rs
pub struct Potter {}
impl Potter {
pub fn new() -> Potter {
Potter {}
}
}
tests/tests.rs
use potter::Potter;
#[test]
fn it_works() {
let pot = Potter::new();
assert_eq!(2 + 2, 4);
}
https://stackoverflow.com/questions/46867652
复制相似问题