rust - 如何为 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
如何指定字段中的类型?我有什么明显的遗漏吗?
解决方案
这是语法:
Scene<Renderer = YourRenderer>
推荐阅读
- python - 将数值添加到列表中的特定数字,而不是将数字实际加在一起
- python - 使用 python 的 Shapley 值
- python - 在列表中创建开始和结束日期元组
- azure-traffic-manager - 是否可以根据目标 CPU/某些指标进行加权分配?
- r - 如何通过列中的近似值提取行?
- python-3.x - PyQT5 复选框更改指示器和刻度颜色
- javascript - 在 jquery post done 函数中传递或使用单击的对象
- event-sourcing - 是否可以使用 Prooph 设置事件 created_at 时间?
- node.js - 即使我在 package.json 中有 '"type": "module"',也会得到“不能在模块外使用 import 语句”
- python - Soundcloud-api-v2(使用 user_id 抓取播放列表的数据)