rust - 如何指定特征参数的类型?

标签 rust traits

<分区>

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

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>

关于rust - 如何指定特征参数的类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53970995/

相关文章:

x/y 坐标的排序向量

rust - 如何更改默认的 rustc/Cargo 链接器?

rust - Rust 中有队列和堆栈集合吗?

scala - 如何解决 "Implementation restriction: trait ... accesses protected method ... inside a concrete trait method."

rust - 为什么需要 AddAssign 作为 super 特征的特征也需要调整大小?

scala - 在 Scala 中定义日志记录特征时出现问题

rust - 为什么向 trait 添加泛型类型会影响 trait 对象和关联类型的生命周期?

rust - 在 Rust 中指定 FFI 库的链接路径有哪些不同的方法?

generics - 使用具体类型调用通用闭包

oop - 为二叉树实现 IntoIterator