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() {}
error at 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
However, &u32
also satisfy trait bound of get
.
if I change 1 to 2, it is OK.
if I remove 3, it is OK.
if I add 4, it is OK.
Why?
I think it's possible that the trait bound on f
temporarily changed the constraints that already existed on u32
, thus affecting the get
function's choice of the type parameter Q
. If that's the case, how did the u32
bound change? How does get
select the type parameter?
Or is there a simpler way to understand it?