问题描述
为什么除了最后一个调用之外,调用链中的所有方法都需要对可变迭代器的可变引用?
我的意图是更改迭代器(...: &mut std::slice::Iter<u8>
)的内部状态,而不是指针/引用本身(mut it: ...
)。我的直觉是建立在这个answer上的。
fn main() {
let v = vec![1,2,3];
let mut it = v.iter();
match it.next() {
Some(v) => {
fun1(&mut it);
println!("{:?}",v);
}
None => (),}
}
fn fun1(mut it: &mut std::slice::Iter<u8>) {
// ^-- why mut?
match it.next() {
Some(v) => {
fun2(&mut it);
println!("{:?}",}
}
fn fun2(it: &mut std::slice::Iter<u8>) {
// ^-- not needed
match it.next() {
Some(v) => {
println!("{:?}",}
}
当我删除此mut
时,出现此错误:
error[E0596]: cannot borrow `it` as mutable,as it is not declared as mutable
--> src/main.rs:18:18
|
14 | fn fun1(it: &mut std::slice::Iter<u8>) {
| -- help: consider changing this to be mutable: `mut it`
...
18 | fun2(&mut it);
| ^^^^^^^ cannot borrow as mutable
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)