Mutable string in immutable variable



examples/ownership/mutable-string-in-immutable-variable/src/main.rs
fn main() {
    let name = String::from("Foo");
    println!("{name}");

    name.push_str(" Bar");
    println!("{name}");
}

Foo
Bar


error[E0596]: cannot borrow `name` as mutable, as it is not declared as mutable
 --> examples/ownership/mutable_string_in_immutable_variable.rs:5:5
  |
5 |     name.push_str(" Bar");
  |     ^^^^^^^^^^^^^^^^^^^^^ cannot borrow as mutable
  |
help: consider changing this to be mutable
  |
2 |     let mut name = String::from("Foo");
  |         +++

error: aborting due to previous error

For more information about this error, try `rustc --explain E0596`.