通过遵循这个指南,我创建了一个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
and the content of the files:
src/main.rs
use hello;
fn main() {
hello::print_hello();
}
src/hello.rs
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
有没有一个简单的例子,说明如何将当前项目中的一个模块包含到项目的主文件中?