In Rust, there are several ways to create a String from a string literal:
fn main() {
let s_from = String::from("string"); // type on the right of the operator
let s_into: String = "string".into(); // type on the left of the operator
let s_to_string = "string".to_string(); // expresses type
let s_to_owned = "string".to_owned(); // expresses ownership
assert_eq!(s_from, s_into);
assert_eq!(s_from, s_to_string);
assert_eq!(s_from, s_to_owned);
}
- Is there a rule in rust to follow a reading direction in relation to the operator?
- Is there a reason to favour
From
/Into
overto_string()
/to_owned()
? - Is there a reason to favour one of those over all the others?
With several developers working on a project, a mixture usage of those happens.