盛安安*_*盛安安 7 traits type-parameter rust
码:
trait Trait<T> {}
struct Struct<U>;
impl<T, U: Trait<T>> Struct<U> {}
Run Code Online (Sandbox Code Playgroud)
错误:
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
Run Code Online (Sandbox Code Playgroud)
似乎RFC 447禁止这种模式; 有没有办法解决这个问题?我认为可以通过更改T为关联类型来解决,但这会阻止我执行multidispatch.
struct中未使用的类型参数可以使用PhantomData:
struct Struct<U> {
_marker: PhantomData<U>,
}
impl<U> Struct<U> {
fn example<T>(&self)
where
U: Trait<T>,
{
// use `T` and `U`
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1018 次 |
| 最近记录: |