Borrowing Mutably and Immutably
A reference is denoted with the &
symbol, so a String
is the actual string instance,
while an &String
is a reference to the string instance. Like variables, references
can be mutable or immutable, and mutable references are written &mut
. These are also
sometimes called mutable borrows, because borrow and reference are used somewhat
interchangeably in Rust literature.
We'll change our foo
function to take a mutable reference to a String
instead of
taking ownership of its parameter. This has another side effect of making the clone()
call unnecessary. Because we are passing a reference as a parameter, we need to
dereference the variable in order to modify it. We also need to change how we pass in
the parameter to foo
. Instead of just passing x
, which is the variable x
, we want
to pass a mutable reference to x
, so we write &mut x
. println!
doesn't need us to
dereference x
for it, because a &String
, &mut String
, and String
can all be
Display
-ed with the same code.
fn foo(bar: &mut String) { *bar += " I'm here!"; println!("Got {}", bar); } fn main() { let mut x: String = "Hello, World!".to_string(); foo(&mut x); println!("X is {}", x); }
We can run this now in the Playground and see that we print out the modified message both times:
Got Hello, World! I'm here!
X is Hello, World! I'm here!
Hopefully you have a rough understanding of what it means to own, reference, borrow,
and dereference in Rust. Remember up above when we brushed
past using .to_string()
to convert a &str
to a String
? The .to_string()
method
is implemented for str
references and returns a String
!