trait B<T> {}
impl<T> B<T> for T {}
fn f<K>()
where
// u32: B<u32>, // 4
u32: B<K>, // 3
{
// get::<u32>(&1u32); // 2
get(&1u32); // 1 error
}
fn get<Q>(k: &Q)
where
u32: B<Q>,
{
}
fn main() {}
错误发生在1:
Compiling playground v0.0.1 (/playground)
error[E0308]: mismatched types
--> src/main.rs:10:9
|
4 | fn f<K>()
| - expected this type parameter
...
10 | get(&1u32); // 1 error
| --- ^^^^^ expected `&K`, found `&u32`
| |
| arguments to this function are incorrect
|
= note: expected reference `&K`
found reference `&u32`
note: function defined here
--> src/main.rs:13:4
|
13 | fn get<Q>(k: &Q)
| ^^^ -----
For more information about this error, try `rustc --explain E0308`.
error: could not compile `playground` (bin "playground") due to 1 previous error
然而,&u32
也满足get
的性状界限.
如果我把1改成2,就可以了.
如果我删除3,就可以了.
如果我加4,就可以了.
为什么?
我认为f
上的特征可能暂时改变了u32
上已经存在的约束,从而影响了get
函数对类型参数Q
的 Select .如果是这样的话,u32
的界限是如何变化的?get
如何 Select 类型参数?
还是有更简单的方法来理解它?