按照本指南,我创建了一个货运项目
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) 我希望有一个包含多个结构的模块,每个模块都在自己的文件中.以Math模块为例:
Math/
Vector.rs
Matrix.rs
Complex.rs
Run Code Online (Sandbox Code Playgroud)
我希望每个结构都在同一个模块中,我将从我的主文件中使用它,如下所示:
use Math::Vector;
fn main() {
// ...
}
Run Code Online (Sandbox Code Playgroud)
然而,Rust的模块系统(开始时有点混乱)并没有提供一种明显的方法来实现这一点.它似乎只允许您将整个模块放在一个文件中.这不是质朴的吗?如果没有,我该怎么做?