Hi all,
I have error E0502 , and even reading others posts, I can't figure out how get out of it.
struct A {
list: Vec<i16>,
}
impl A {
fn f1(&self) -> Vec<(usize,&i16)>
{
// search for indices matching predicate. Return is a vector of tuples
let result: Vec<_> = self.list.iter().enumerate().filter(|e| e.1%2 == 0).collect();
result
}
fn f2(&mut self) {
let result = { self.f1() };
self.list.remove(0);
}
}
fn main() {
let mut a = A { list: Vec::new() };
a.list = vec![1,2,3,4,5,6,7,8,9];
let result = a.f1();
println!("{:?}", result);
}
I understand the cause, but can't fix it. I thought putting the let result
in a scope {}
, the error will disappear. I thought the borrowing will end after the scope..
rustc 1.15.1 (021bd294c 2017-02-08)
error[E0502]: cannot borrow `self.list` as mutable because `*self` is also borrowed as immutable
--> <anon>:16:9
|
14 | let result = { self.f1() };
| ---- immutable borrow occurs here
15 |
16 | self.list.remove(0);
| ^^^^^^^^^ mutable borrow occurs here
17 | }
| - immutable borrow ends here.
Any hint?
Thanks a lot.