利用 match 循环匹配数组中的值,如果是 Apple,改成 RedApple:
let mut names = ["Apple", "Banana"];
for name in names.iter_mut() {
*name = match name {
&mut "Apple" => { format!("Red{}", name).as_str() }
_ => "Hello"
}
}
报错:
&mut "Apple" => { format!("Red{}", name).as_str() }
- temporary value is freed at the end of this statement
| | |
| | creates a temporary which is freed while still in use
_________- borrow later used here
|
= note: consider using a `let` binding to create a longer lived value