Mutable string in immutable variable
-
push_str
-
If we initialize the variable using
String::from
then the literal value is copied to the heap and it can be changed. -
But if the variable is not mutable, then what's the point?
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`.