In the below code, it looks like y.foo()
is resolved with y
as a [u32]
, which ends up with Foo<SliceMarker>::foo
getting called. But when using the Full Qualified Syntax, I'm also able to force it to resolve y
as &[u32]
which calls Foo<SizedMarker>::foo
. Why is rustc not causing a compiler error here when it looks like there is possible ambiguity in what function should get called, or why is it that it resolves it to the slice version?
pub trait Foo<Marker> {
fn foo(&self);
}
pub struct SizedMarker();
impl<T: Sized> Foo<SizedMarker> for T {
fn foo(&self) {
println!("Foo for Sized T: T = {:#?}", std::any::type_name::<T>())
}
}
pub struct SliceMarker();
impl<'a, T> Foo<SliceMarker> for [T] {
fn foo(&self) {
println!("Foo for [T]: [T] = {:#?}", std::any::type_name::<[T]>())
}
}
fn main() {
let x: i8 = 5;
x.foo();
let y:&[u32] = &[1, 2, 3];
y.foo();
<&[u32] as Foo<SizedMarker>>::foo(&y);
<[u32] as Foo<SliceMarker>>::foo(&y);
}
Playground: Rust Playground (rust-lang.org)