1

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);
}
  1. Is there a rule in rust to follow a reading direction in relation to the operator?
  2. Is there a reason to favour From/Into over to_string()/to_owned()?
  3. 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.

Roi Danton
  • 7,933
  • 6
  • 68
  • 80

0 Answers0