Code:
trait Trait<T> {}
struct Struct<U>;
impl<T, U: Trait<T>> Struct<U> {}
Error:
error[E0207]: the type parameter `T` is not constrained by the impl trait, self type, or predicates
--> src/main.rs:5:6
|
5 | impl<T, U: Trait<T>> Struct<U> {}
| ^ unconstrained type parameter
看起来RFC 447 https://github.com/rust-lang/rfcs/pull/447禁止这种模式;有什么办法可以解决这个问题吗?我认为可以通过改变来解决T
到关联的类型,但这会阻止我进行多重调度。
结构体中未使用的类型参数可以使用PhantomData https://doc.rust-lang.org/std/marker/struct.PhantomData.html:
struct Struct<U> {
_marker: PhantomData<U>,
}
impl<U> Struct<U> {
fn example<T>(&self)
where
U: Trait<T>,
{
// use `T` and `U`
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)