According to: https://web.mit.edu/rust-lang_v1.25/arch/amd64_ubuntu1404/share/doc/rust/html/book/first-edition/strings.html
rust str is immutable, and cannot be used when mutability is required.
However, the following program compiles and works
fn main() {
let mut mystr = "foo";
mystr = "bar";
{
mystr = "baz";
}
println!("{:?}", mystr);
}
Can someone explain the mutability of str in Rust?
I expect let mut mystr = "foo"; to result in compilation error since str in Rust is immutable. But it compiles.
strhere, you're just replacing one&strwith another&str.mutonly apply to the variable, not the contents? e.g. you can re-assign the variable to a different string, but you cannot change the string itself? (pretty much likefinalworks in Java). Never coded rust, so I might be totally wrong.