搞得有点复杂嘛,反正一边学习一边试了。如题所示,代码如下
fn main() {
let mut names = vec!["hello","rust","other"];
//names[2] = "CastlePeak";本句等于下面的for循环功能
for name in names.iter_mut(){
*name = match name{
&mut "other" => "CastlePeak",
_ => name,
}
}
println!("names: {:?}", names);
}
运行如上代码,输出
names: ["hello", "rust", "CastlePeak"]