如何正确将Rust模块拆分为多个文件?
大家好!我开始学习Rust,遇到了在模块中组织代码的问题。我有一个 `main.rs` 文件,在其中声明了 `shapes` 模块如下:
```rust
mod shapes;
fn main() {
let rect = shapes::Rectangle::new(3.0, 4.0);
println!("面积: {}", rect.area());
}
```
在 `shapes.rs` 中我定义了结构和函数:
```rust
pub struct Rectangle {
width: f64,
height: f64,
}
impl Rectangle {
pub fn new(w: f64, h: f64) -> Rectangle {
Rectangle { width: w, height: h }
}
pub fn area(&self) -> f64 {
self.width * self.height
}
}
```
一切正常,但现在我想添加更多图形:`Circle`、`Triangle`。如果我把所有内容都塞进一个 `shapes.rs`,文件会变得很大。我尝试创建 `shapes/` 文件夹,并在其中放入 `mod.rs`、`rectangle.rs`、`circle.rs`。在 `mod.rs` 中我写了:
```rust
pub mod rectangle;
pub mod circle;
```
但编译器报错,说找不到类型。如何正确