Rust's ownership model ensures memory safety without needing a garbage collector. In the previous challenge, you learned about immutable references. Now, let's dive into mutable references.
Mutable references allow you to modify the value you are borrowing. You can only have one mutable reference to a particular piece of data in a particular scope. This prevents data races at compile time.
In Rust, &mut
is used to create a mutable reference. This means that you can borrow a value and make changes to it without taking ownership. However, Rust enforces that there can only be one mutable reference to a particular piece of data in a particular scope. This ensures that no data races occur, as only one part of the code can modify the data at a time.
Create a function append_suffix
that takes a mutable reference to a String
and appends a given suffix to it.
append_suffix
function should take a mutable reference to the input String
and append the given suffix to it.String
type in Rust has methods like push_str
which can be useful for modifying strings.