What happens here, auto deref?

what happens here?
it seems an implicit deref,
but no documents says Deref will erase &, it just transmute &String to &str, etc...

I know method call will repeat deref the reciver, that will erase &
but it is not a method call, it just is a assignment.

but the following codes compiles:

    fn main3() {
        let r: &f32 = &&&&&3.142;
        assert_eq!(*r, 3.142);
    }

Right, because String implements Deref<Target = str>. Similarly, &f32 implements Deref<Target = f32>. So by the same rule &&f32 can coerce to &f32. And this rule can be applied multiple times at a coercion site, so it doesn't matter how many & there are.

This is type coercion, or deref coercion

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.