【发布时间】:2014-12-10 21:39:59
【问题描述】:
通过关注this guide,我创建了一个 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 编译它时,我得到了
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
有没有一个简单的例子说明如何将当前项目中的一个模块包含到项目的主文件中?
【问题讨论】:
标签: rust