I have a function that takes the mutable reference of the string and appends some text.
fn append_str(s: &mut String) {
s.push_str(" hi");
}
Suppose I have a string.
let mut s: String = "hi".to_string();
If I create the mutable reference to s and pass it to append_str, it compiles without a problem.
let mut ss = &mut s;
append_str(&mut ss);
However, if I expliclty define ss with &mut String, it does not compile.
let ss: &mut String = &mut s;
append_str(&mut ss);
it shows following compiler error.
|
80 | let ss: &mut String = &mut s;
| -- help: consider changing this to be mutable: `mut ss`
81 | append_str(&mut ss);
| ^^^^^^^ cannot borrow as mutable
One thing funny is that if I dereference it, then it works.
let ss: &mut String = &mut s;
append_str(&mut *ss); // OK
What is the reason that we have to explicitly dereference in this case?
One more question: Why do we have to specify mut to the reference if we want to pass it to the function?
let ss = &mut s;
append_str(&mut ss); // ERROR