依照指示本指南 https://doc.rust-lang.org/0.12.0/guide.html#crates-and-modules我创建了一个 Cargo 项目。
src/main.rs
fn main() {
hello::print_hello();
}
mod hello {
pub fn print_hello() {
println!("Hello, world!");
}
}
我用它运行
cargo build && cargo run
并且编译没有错误。现在我试图将主模块分成两部分,但无法弄清楚如何包含另一个文件中的模块。
我的项目树看起来像这样
├── src
├── hello.rs
└── main.rs
以及文件的内容:
src/main.rs
use hello;
fn main() {
hello::print_hello();
}
src/hello.rs
mod hello {
pub fn print_hello() {
println!("Hello, world!");
}
}
当我编译它时cargo build
I get
error[E0432]: unresolved import `hello`
--> src/main.rs:1:5
|
1 | use hello;
| ^^^^^ no `hello` external crate
我尝试按照编译器的建议进行修改main.rs
to:
#![feature(globs)]
extern crate hello;
use hello::*;
fn main() {
hello::print_hello();
}
但这仍然没有多大帮助,现在我明白了:
error[E0463]: can't find crate for `hello`
--> src/main.rs:3:1
|
3 | extern crate hello;
| ^^^^^^^^^^^^^^^^^^^ can't find crate
是否有一个简单的示例来说明如何将当前项目中的一个模块包含到项目的主文件中?