fn main() {
let s = String::from("sdf");
let rs3:&str = &s;
let rs:&String = &s;
}
so why i can get &str,&String from &String
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.
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() */;
This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.