通过遵循this guide,我创建了一个货运项目.
100
fn main() {
hello::print_hello();
}
mod hello {
pub fn print_hello() {
println!("Hello, world!");
}
}
我用它 run
cargo build && cargo run
它编译时没有错误.现在,我正试图将主模块一分为二,但不知道如何从另一个文件中包含一个模块.
我的项目树是这样的
├── src
├── hello.rs
└── main.rs
以及文件的内容:
100
use hello;
fn main() {
hello::print_hello();
}
100
mod hello {
pub fn print_hello() {
println!("Hello, world!");
}
}
当我用cargo build
编译它时,我得到
error[E0432]: unresolved import `hello`
--> src/main.rs:1:5
|
1 | use hello;
| ^^^^^ no `hello` external crate
我试着按照编译器的建议修改了main.rs
:
#![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
有没有一个简单的例子,说明如何将当前项目中的一个模块包含到项目的主文件中?