This works fine:
fn main() {
let mut mystr = "this is my string";
println!("{}", mystr);
mystr = "new string";
println!("{}", mystr);
}
This does not work:
fn change(s: &mut str) {
s = "new string";
println!("{}", s);
}
fn main() {
let mut mystr = "change me";
//change(mystr);
//change(&mystr);
//change(&mut mystr);
}
error[E0308]: mismatched types
--> src/main.rs:2:9
|
2 | s = "new string";
| ^^^^^^^^^^^^ types differ in mutability
|
= note: expected mutable reference `&mut str`
found reference `&'static str`
I don't know if I'm making a mistake or if the borrow checker is stopping it. The compiler errors don't really tell me.
I've tried the 3 different ways of passing the variable as the compiler error changes and sometimes says trying to pass &str
to &mut str
and when I change it to &mut mystr
I get a new error.
Am I making a mistake or is this not allowed?