![](/img/trans.png)
[英]How do I bound a trait with a supertrait that uses the trait's associated type as a parameter?
[英]How to create a trait that specifies the associated type of a supertrait?
我想創建一個抽象特征,它指定一個索引類型和一個值類型,其中實現該特征的任何結構都必須實現Index<IndexType>
和IndexMut<IndexType>
並定義Output
類型,該類型在實現特征。
我嘗試創建一個特征,但似乎我無法指定 output 類型:
use std::ops::{Index, IndexMut};
struct Coord;
struct LightValue;
trait LightMap: Index<Coord> + IndexMut<Coord> {}
impl LightMap {
type Output = LightValue;
}
warning: trait objects without an explicit `dyn` are deprecated
--> src/lib.rs:8:6
|
8 | impl LightMap {
| ^^^^^^^^ help: use `dyn`: `dyn LightMap`
|
= note: `#[warn(bare_trait_objects)]` on by default
error[E0191]: the value of the associated type `Output` (from the trait `std::ops::Index`) must be specified
--> src/lib.rs:8:6
|
8 | impl LightMap {
| ^^^^^^^^ associated type `Output` must be specified
error[E0202]: associated types are not yet supported in inherent impls (see #8995)
--> src/lib.rs:9:5
|
9 | type Output = LightValue;
| ^^^^^^^^^^^^^^^^^^^^^^^^^
如果我沒有指定 output 類型,那么在我嘗試使用該特征的任何地方associated type Output must be specified
。
您需要將關聯類型的限制放在超特征上:
trait LightMap: Index<Coord, Output = LightValue> + IndexMut<Coord> {}
也可以看看:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.