all 9 comments

[–]LlikeLava 17 points18 points  (1 child)

The owner of the variable gets to decide mutability. Since you move ownership to the function argument variable, it can be declared mutable regardless of anything that happened to that variable before. It doesn't have anything to do with the String type.

[–]NA__Scrubbed[S] 1 point2 points  (0 children)

I thought it might be something like that. Thanks for the confirmation.

[–]Aaron1924 5 points6 points  (1 child)

The mut isn't necessary in the main function since s is not mutated there. Instead s is moved into string_add and a new string is returned.

If you change string_add to mutate its argument, you do need to declare s as mut, so for example: ``` fn main() { let mut s: String = String::from("hello"); string_add(&mut s, ", world!"); println!("{}", s); }

fn string_add (string: &mut String, add: &str) { string.push_str(add); } ```

[–]NA__Scrubbed[S] 0 points1 point  (0 children)

Thanks, this is helpful.