UTF-8
String collection
Use .push and .push_str to append to a string.
Example:
// opt 1
let _s = "whatever".to_string();
// opt 2
let _s = String::from("whatever");
// opt 3
let mut _s = String::from("foo");
_s.push_str("bar");
_s.push('!');
println!("{}", _s);
// Output: foobar!
Use the + operator to concatenate string values.
let s1 = String::from("Hello, ");
let s2 = String::from("world");
let s3 = s1 + &s2; // s1 has been moved here and can no longer be used
println!("s3 is {}", s3);
With format!
let hi1 = String::from("Shalom");
let hi2 = String::from("Hola");
let full_message = format!("{hi1} - {hi2}");
println!("{full_message}");
// Output: Shalom - Hola
From the Rust book:
"If we need to concatenate multiple strings, the behavior of the + operator gets unwieldy:
let s1 = String::from("tic");
let s2 = String::from("tac");
let s3 = String::from("toe");
let s = s1 + "-" + &s2 + "-" + &s3;
At this point, s will be tic-tac-toe. With all of the + and " characters, it’s difficult to see what’s going on. For combining strings in more complicated ways, we can instead use the format! macro:
let s1 = String::from("tic");
let s2 = String::from("tac");
let s3 = String::from("toe");
let s = format!("{s1}-{s2}-{s3}");
This code also sets s to tic-tac-toe. The format! macro works like println!, but instead of printing the output to the screen, it returns a String with the contents. The version of the code using format! is much easier to read, and the code generated by the format! macro uses references so that this call doesn’t take ownership of any of its parameters."