在单独的文件中定义特征
Definition of traits in a separate file
我定义了两个结构体
pub struct Rect {
pub width: f32,
pub length: f32
}
//and
pub struct Circle {
pub radius: f32
}
然后我定义了一个特征 Area
并为 Circle
和 Rect
实现了这个特征。当我将所有源代码放在一个 main.rs
文件中时,一切正常。
现在我要整理我的源代码。特别是,我想创建一个文件夹 /src/geometry
,在该文件夹下创建三个 rs
文件:
// /src/geometry/rect.rs
pub struct Rect {
pub width: f32,
pub length: f32
}
// /src/geometry/circle.rs
pub struct Circle {
pub radius: f32
}
和
// /src/geometry/traits.rs
pub trait Area {
fn area(&self) -> f32;
}
最后我想使用 main.rs
中的这些结构。
我花了几天时间,通读了我在互联网上找到的所有示例,但仍然无法正常工作。有什么建议吗?
更新:
项目结构:
src
geometry
rect.rs
circle.rs
traits.rs
geometry.rs
main.rs
// rect.rs
pub struct Rect {
pub width: f32,
pub length: f32
}
impl Area for Rect {
fn area(&self) -> f32 {
self.width * self.length
}
}
impl Perimeter for Rect {
fn perimeter(&self) -> f32 {
2.0*(self.width + self.length)
}
}
// circle.rs
pub struct Circle {
pub radius: f32
}
impl Area for Circle {
fn area(&self) -> f32 {
3.14*self.radius*self.radius
}
}
impl Perimeter for Circle {
fn perimeter(&self) -> f32 {
2.0*3.14*self.radius
}
}
// traits.rs
pub trait Perimeter {
fn perimeter(&self) -> f32;
}
pub trait Area {
fn area(&self) -> f32;
}
// geometry.rs
pub mod rect;
pub mod circle;
// main.rs
mod geometry;
use geometry::rect::Rect;
use geometry::circle::Circle;
fn main() {
let rect = Rect{ width: 1.0, length: 2.0 };
let circle = Circle{ radius: 2.3 };
println!("{}", rect.area());
println!("{}", circle.area());
}
编译器错误消息
error[E0405]: cannot find trait `Area` in this scope
--> src/geometry/rect.rs:6:6
|
6 | impl Area for Rect {
| ^^^^ not found in this scope
error[E0405]: cannot find trait `Perimeter` in this scope
--> src/geometry/rect.rs:12:6
|
12 | impl Perimeter for Rect {
| ^^^^^^^^^ not found in this scope
error[E0405]: cannot find trait `Area` in this scope
--> src/geometry/circle.rs:5:6
|
5 | impl Area for Circle {
| ^^^^ not found in this scope
error[E0405]: cannot find trait `Perimeter` in this scope
--> src/geometry/circle.rs:11:6
|
11 | impl Perimeter for Circle {
| ^^^^^^^^^ not found in this scope
error: aborting due to 4 previous errors
For more information about this error, try `rustc --explain E0405`.
error: could not compile `chapter10`.
首先,您需要在 circle.rs
和 rect.rs
中添加 use crate::geometry::traits::{Area, Perimeter};
:这会处理您粘贴的错误。
然后,在main.rs
中你需要use geometry::traits::Area;
否则你不能调用.area()
方法。为此,您需要在 geometry.rs
中制作 traits
模块 public:pub mod traits;
(或至少 public 在板条箱中:pub(crate) mod traits;
).
就我个人而言,我也会将 geometry.rs
重命名为 geometry/mod.rs
。
我定义了两个结构体
pub struct Rect {
pub width: f32,
pub length: f32
}
//and
pub struct Circle {
pub radius: f32
}
然后我定义了一个特征 Area
并为 Circle
和 Rect
实现了这个特征。当我将所有源代码放在一个 main.rs
文件中时,一切正常。
现在我要整理我的源代码。特别是,我想创建一个文件夹 /src/geometry
,在该文件夹下创建三个 rs
文件:
// /src/geometry/rect.rs
pub struct Rect {
pub width: f32,
pub length: f32
}
// /src/geometry/circle.rs
pub struct Circle {
pub radius: f32
}
和
// /src/geometry/traits.rs
pub trait Area {
fn area(&self) -> f32;
}
最后我想使用 main.rs
中的这些结构。
我花了几天时间,通读了我在互联网上找到的所有示例,但仍然无法正常工作。有什么建议吗?
更新: 项目结构:
src
geometry
rect.rs
circle.rs
traits.rs
geometry.rs
main.rs
// rect.rs
pub struct Rect {
pub width: f32,
pub length: f32
}
impl Area for Rect {
fn area(&self) -> f32 {
self.width * self.length
}
}
impl Perimeter for Rect {
fn perimeter(&self) -> f32 {
2.0*(self.width + self.length)
}
}
// circle.rs
pub struct Circle {
pub radius: f32
}
impl Area for Circle {
fn area(&self) -> f32 {
3.14*self.radius*self.radius
}
}
impl Perimeter for Circle {
fn perimeter(&self) -> f32 {
2.0*3.14*self.radius
}
}
// traits.rs
pub trait Perimeter {
fn perimeter(&self) -> f32;
}
pub trait Area {
fn area(&self) -> f32;
}
// geometry.rs
pub mod rect;
pub mod circle;
// main.rs
mod geometry;
use geometry::rect::Rect;
use geometry::circle::Circle;
fn main() {
let rect = Rect{ width: 1.0, length: 2.0 };
let circle = Circle{ radius: 2.3 };
println!("{}", rect.area());
println!("{}", circle.area());
}
编译器错误消息
error[E0405]: cannot find trait `Area` in this scope
--> src/geometry/rect.rs:6:6
|
6 | impl Area for Rect {
| ^^^^ not found in this scope
error[E0405]: cannot find trait `Perimeter` in this scope
--> src/geometry/rect.rs:12:6
|
12 | impl Perimeter for Rect {
| ^^^^^^^^^ not found in this scope
error[E0405]: cannot find trait `Area` in this scope
--> src/geometry/circle.rs:5:6
|
5 | impl Area for Circle {
| ^^^^ not found in this scope
error[E0405]: cannot find trait `Perimeter` in this scope
--> src/geometry/circle.rs:11:6
|
11 | impl Perimeter for Circle {
| ^^^^^^^^^ not found in this scope
error: aborting due to 4 previous errors
For more information about this error, try `rustc --explain E0405`.
error: could not compile `chapter10`.
首先,您需要在 circle.rs
和 rect.rs
中添加 use crate::geometry::traits::{Area, Perimeter};
:这会处理您粘贴的错误。
然后,在main.rs
中你需要use geometry::traits::Area;
否则你不能调用.area()
方法。为此,您需要在 geometry.rs
中制作 traits
模块 public:pub mod traits;
(或至少 public 在板条箱中:pub(crate) mod traits;
).
就我个人而言,我也会将 geometry.rs
重命名为 geometry/mod.rs
。