This is the last example from the [for and range - Rust By Example] page:
fn main() {
let mut names = vec!["Bob", "Frank", "Ferris"];
for name in names.iter_mut() {
*name = match name {
&mut "Ferris" => "There is a rustacean among us!",
_ => "Hello",
}
}
println!("names: {:?}", names);
}
It outputs this:
names: ["Hello", "Hello", "There is a rustacean among us!"]
I was attempting to modify the example so that it outputs this:
names: ["Hello, Bob", "Hello, Frank", "There is a rustacean among us!"]
Here is an example of one of many of my unsuccessful attempts:
fn main() {
let mut names = vec!["Bob", "Frank", "Ferris"];
for name in names.iter_mut() {
*name = match name {
&mut "Ferris" => "There is a rustacean among us!",
_ => ("Hello, ".to_string() + *name).as_mut_str(),
}
}
println!("names: {:?}", names);
}
This generates this error message:
error[E0716]: temporary value dropped while borrowed
--> src/main.rs:7:18
|
5 | *name = match name {
| _________________-
6 | | &mut "Ferris" => "There is a rustacean among us!",
7 | | _ => ("Hello, ".to_string() + *name).as_mut_str(),
| | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - temporary value is freed at the end of this statement
| | |
| | creates a temporary which is freed while still in use
8 | | }
| |_________- borrow later used here
|
= note: consider using a `let` binding to create a longer lived value
Any suggestions as to how to fix this would be appreciated.