Rust 有没有办法解决未使用的类型参数?
代码:Rust 有没有办法解决未使用的类型参数?,rust,traits,type-parameter,Rust,Traits,Type Parameter,代码: trait{} struct-struct; impl结构{} 错误: error[E0207]:类型参数'T'不受impl-trait、self-type或谓词的约束 -->src/main.rs:5:6 | 5 | impl Struct{} |^无约束类型参数 这似乎阻止了这种模式;有办法解决这个问题吗?我认为可以通过将T更改为关联类型来解决此问题,但这会阻止我执行多重分派。结构中未使用的类型参数可以使用: struct结构{ _标记:幻影数据, } impl结构{ fn示例(
trait{}
struct-struct;
impl结构{}
错误:
error[E0207]:类型参数'T'不受impl-trait、self-type或谓词的约束
-->src/main.rs:5:6
|
5 | impl Struct{}
|^无约束类型参数
这似乎阻止了这种模式;有办法解决这个问题吗?我认为可以通过将
T
更改为关联类型来解决此问题,但这会阻止我执行多重分派。结构中未使用的类型参数可以使用:
struct结构{
_标记:幻影数据,
}
impl结构{
fn示例(&self)
哪里
U:特质,
{
//使用'T'和'U'`
}
}
Wow,我不知道已经可以在方法上使用where子句了。Rust真是一种令人惊叹的语言。
trait Trait<T> {}
struct Struct<U>;
impl<T, U: Trait<T>> Struct<U> {}
struct Struct<U> {
_marker: PhantomData<U>,
}
impl<U> Struct<U> {
fn example<T>(&self)
where
U: Trait<T>,
{
// use `T` and `U`
}
}