按照本指南,我创建了一个货运项目
SRC/main.rs
fn main() {
hello::print_hello();
}
mod hello {
pub fn print_hello() {
println!("Hello, world!");
}
}
Run Code Online (Sandbox Code Playgroud)
我运行使用
cargo build && cargo run
Run Code Online (Sandbox Code Playgroud)
它编译没有错误.现在我正在尝试将主模块分成两部分,但无法弄清楚如何从另一个文件中包含一个模块.
我的项目树看起来像这样
??? src
??? hello.rs
??? main.rs
Run Code Online (Sandbox Code Playgroud)
和文件的内容:
SRC/main.rs
use hello;
fn main() {
hello::print_hello();
}
Run Code Online (Sandbox Code Playgroud)
SRC/hello.rs
mod hello {
pub fn print_hello() {
println!("Hello, world!");
}
}
Run Code Online (Sandbox Code Playgroud)
当我编译它src/main.rs,我得到
error[E0432]: unresolved import `hello`
--> src/main.rs:1:5
|
1 | use hello;
| ^^^^^ no `hello` external crate
Run Code Online (Sandbox Code Playgroud)
我试图遵循编译器的建议并修改main.rs
#![feature(globs)]
extern crate hello; …Run Code Online (Sandbox Code Playgroud) 我有以下代码定义生成文件的路径:
fn gen_test_dir() -> tempdir::TempDir {
tempdir::TempDir::new_in(Path::new("/tmp"), "filesyncer-tests").unwrap()
}
Run Code Online (Sandbox Code Playgroud)
这个函数定义在tests/lib.rs,在该文件的测试中使用,我也想在位于的单元测试中使用它src/lib.rs.
如果不将实用程序功能编译成非测试二进制文件而不重复代码,是否可以实现?
我项目的路径结构如下:
demo
??? benches
? ??? crypto_bench.rs
??? src
? ??? main.rs
? ??? crypto.rs
??? Cargo.lock
??? Cargo.toml
Run Code Online (Sandbox Code Playgroud)
crypto.rs包含Crypto带有实现的结构。
crypto.rs通过main.rs使用引用mod crypto;
如何crypto.rs从crypto_bench.rs长椅文件夹中使用?
我已经尝试了各种变化extern crate,mod,super和use。我可以在网上找到的所有示例都是针对带有的图书馆项目,lib.rs并且在将带有main.rs文件的项目使用时,这些“导入”不起作用。
我有Rust项目,包括集成测试(在/testsdir中)和基准测试(在/benchesdir中).在测试和长凳中我需要一些实用功能,但它们与我的箱子本身无关,所以我不能把它们放在/utils目录中.
处理这种情况的惯用方法是什么?