Tell me what compiler did for me

fn main() {
    let s = String::from("sdf");
    let rs3:&str = &s;
    let rs:&String = &s;
}

so why i can get &str,&String from &String

This is the deref coercion.

1 Like

More precisely,

And for T = String and U = str, we do have String : Deref<Target = str>.

That's why let rs3: &str = &s; works: the compiler "inserts" an implicit .deref() / *_ on s: String:

let rs3: &str = &*s /* = s.deref() */;
1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.