Consider this statement: From<T> for U implies Into<U> for T. source
let t="abc"; and note that t has type &str
Everyone has used: let s=String::from(t);
So that we have: From<&str> for String
According to the implication above we have:Into<String> for &str
However, the following does not work:
fn main(){
let z="abc";
let x = String::from(z);
let s=&str::into(x);
}
What am I not understanding?
>Solution :
It works, except that &str::into(z) is understood as &(str::into(z)). If you want to call the associated function into for type &str, you must use a qualified name for that type:
fn main(){
let z = "abc";
let x = String::from(z);
let s: String = <&str>::into(x);
}
Note that the type annotation String was added because otherwise Rust cannot know which type is the target of into.
Otherwise, you can also use the method syntax
let s: String = x.into();
or the associated function of trait Into:
let s: String = Into::into(x);