Mic*_*ael 3 reference mutable rust
我有一个Vec<State>列表,想要搜索一个元素并获取对其的可变引用。如果不存在,则应创建一个新的默认元素并将其添加到列表中:
struct State {
a: usize,
}
fn print_states(states: &Vec<State>) {
for state in states {
print!("State{{a:{}}} ", state.a);
}
println!();
}
fn main() {
let mut states = vec![State { a: 1 }, State { a: 2 }, State { a: 3 }];
print_states(&states);
let mut state = match states.iter_mut().find(|state| state.a == 2) {
Some(state) => state,
None => {
let new_state = State { a: 3 };
states.push(new_state);
states.last().unwrap()
}
};
state.a = 4;
drop(state);
print_states(&states);
}
Run Code Online (Sandbox Code Playgroud)
这导致:
error[E0594]: cannot assign to `state.a` which is behind a `&` reference
--> src/main.rs:25:5
|
17 | let mut state = match states.iter_mut().find(|state| state.a == 2) {
| --------- help: consider changing this to be a mutable reference: `&mut State`
...
25 | state.a = 4;
| ^^^^^^^^^^^ `state` is a `&` reference, so the data it refers to cannot be written
Run Code Online (Sandbox Code Playgroud)
问题是None路径。None => panic!()在不创建这个新的默认元素的情况下使用时,我可以修改找到的元素
我需要改变什么才能使这项工作正常进行?
你的问题是state.last().unwrap()- 线。.last()on方法Vec返回 a ,这会导致编译器推断出to be&State的类型(可以将 -case中的强制转换为该类型)。这就是为什么你不能在第 28 行进行更改。state&State&mut StateSome()state
将行更改为state.last_mut().unwrap()and statewill be a&mut State而不是&State。您的示例在此之后编译。
| 归档时间: |
|
| 查看次数: |
2154 次 |
| 最近记录: |