Hi as seen in the Rust Book you can use clone to set s2 to s1 like this
let s1 = String::from("hello");
let s2 = s1.clone();
println!("s1 = {}, s2 = {}", s1, s2);
Why can’t I change s2 to be s1.clone() + s1.clone()
let s1 = String::from("hello");
let s2 = s1.clone()+ s1.clone();
println!("s1 = {}, s2 = {}", s1, s2);
>Solution :
The Add instance for strings takes a String on the left and a &str on the right. This consumes the string on the left and extends it with (a copy of) the string on the right. It does this for efficiency reasons, to avoid having to copy both strings in every case.
So when you want to concatenate two strings in Rust with +, the left-hand side needs to be a String (an owned string) and the right-hand side needs to be a &str (a borrowed string slice). So consider
let s2 = s1.clone() + &s1.clone();
or, since we’re borrowing it, there’s no need for the second clone at all.
let s2 = s1.clone() + &s1;