小编sal*_*t _的帖子

如何共享相同的实现以及共享字段

我怎样才能简化这段代码?在面向对象编程之后,我仍然无法理解 Rust 的特征和结构。

struct Player {
    entity: Entity,
    hp: i32,
    atk: i32
}

struct Chest {
    entity: Entity,
    amount: i32
}

impl Drawable for Chest {
    fn draw(&self, mut pencil: Pencil) {
        pencil.draw(&self.entity);
    }
}

impl Drawable for Player {
    fn draw(&self, mut pencil: Pencil) {
        pencil.draw(&self.entity);
    }
}
Run Code Online (Sandbox Code Playgroud)

也许有一种方法可以像 OOP 中那样继承某些字段?

另外,如果有人知道有关 Rust 特征和结构的良好而清晰的教程,如果您分享它,我将非常高兴!

struct traits rust

6
推荐指数
2
解决办法
438
查看次数

标签 统计

rust ×1

struct ×1

traits ×1