在单独的文件中定义特征

Har*_*ork 5 module traits rust

我定义了两个结构

pub struct Rect {
    pub width:  f32,
    pub length: f32
}

//and

pub struct Circle {
    pub radius:  f32
}

Run Code Online (Sandbox Code Playgroud)

然后我定义了一个特征,Area并为Circle和实现了这个特征Rect。当我将所有源代码放在一个main.rs文件中时,一切正常。

现在我想组织我的源代码。特别是,我想创建一个文件夹/src/geometryrs在该文件夹下创建三个文件:

// /src/geometry/rect.rs
pub struct Rect {
    pub width:  f32,
    pub length: f32
}
Run Code Online (Sandbox Code Playgroud)
// /src/geometry/circle.rs
pub struct Circle {
    pub radius:  f32
}
Run Code Online (Sandbox Code Playgroud)

// /src/geometry/traits.rs
pub trait Area {
    fn area(&self) -> f32;
}
Run Code Online (Sandbox Code Playgroud)

最后我想使用这些来自main.rs.

我花了几天时间,通读了我在 Internet 上找到的所有示例,但仍然无法使其正常工作。有什么建议?

更新:项目结构:

src
  geometry
     rect.rs 
     circle.rs
     traits.rs
  geometry.rs
  main.rs
Run Code Online (Sandbox Code Playgroud)
src
  geometry
     rect.rs 
     circle.rs
     traits.rs
  geometry.rs
  main.rs
Run Code Online (Sandbox Code Playgroud)
// 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)
    }
}
Run Code Online (Sandbox Code Playgroud)
// 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
    }
}
Run Code Online (Sandbox Code Playgroud)
// traits.rs

pub trait Perimeter {
    fn perimeter(&self) -> f32;
}

pub trait Area {
    fn area(&self) -> f32;
}
Run Code Online (Sandbox Code Playgroud)
// geometry.rs
pub mod rect;
pub mod circle;
Run Code Online (Sandbox Code Playgroud)

编译器错误消息

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`.
Run Code Online (Sandbox Code Playgroud)

lbo*_*lla 8

首先,您需要添加circle.rs:这会处理您粘贴的错误。rect.rsuse crate::geometry::traits::{Area, Perimeter};

然后,main.rs您需要use geometry::traits::Area;否则无法调用.area()方法。为此,您需要traitsgeometry.rs:中公开该模块pub mod traits;(或者至少在 crate: 中公开pub(crate) mod traits;)。

就我个人而言,我也会重命名geometry.rsgeometry/mod.rs.