首页 > 解决方案 > 如何为 trait 参数指定类型?

问题描述

我正在尝试将特征用作结构中的字段:

pub trait Scene {
    type Renderer;

    fn update(&mut self);
    fn render(&mut self, r: &mut Self::Renderer);
}

struct Example {
    active_scene: *mut Scene,
}

当我尝试使用它时,我得到了错误:

error[E0191]: the value of the associated type `Renderer` (from the trait `Scene`) must be specified
 --> src/lib.rs:9:24
  |
9 |     active_scene: *mut Scene,
  |                        ^^^^^ missing associated type `Renderer` value

如何指定字段中的类型?我有什么明显的遗漏吗?

标签: rusttraits

解决方案


这是语法:

Scene<Renderer = YourRenderer>

推荐阅读