Hi Everyone,
I'm new to learning Rust, so sorry if this is an obvious question. I've made this code example where I'm trying to call a method iplus1
to modify elements in a vector kvec
. I can accomplish this with foo
where I iterate over the index of the vector, and I can do it in bar
or baz
where I inline the contents of iplus1
. But how can I combine the two where I use an iterator and call the method? The methods badbar
or badbaz
don't compile. What would the preferred approach be in a case like this? Using foo
just seems really ugly.
Thanks! Craig
fn main() {
#[derive(Debug)]
struct A {
i : i64,
kvec : Vec<i64>
}
impl A {
// some method
fn iplus1(&self) -> i64 {
self.i + 1
}
// this works using the index
fn foo(&mut self) {
for j in 0..self.kvec.len() {
self.kvec[j] += self.iplus1();
}
}
// so does this
// where we inline the contents of iplus1
fn bar(&mut self) {
for kref in &mut self.kvec {
*kref += self.i + 1;
}
}
// and this works with iter_mut()
fn baz(&mut self) {
for kref in self.kvec.iter_mut() {
*kref += self.i + 1;
}
}
// but how do we do it with an iterator
// and the method call?
// this doesn't compile
// cannot borrow `*self` as immutable because
// `self.kvec` is also borrowed as mutable
fn badbar(&mut self) {
for kref in &mut self.kvec {
*kref += self.iplus1();
}
}
// cannot borrow `*self` as immutable because
// `self.kvec` is also borrowed as mutable
fn badbaz(&mut self) {
for kref in self.kvec.iter_mut() {
*kref += self.iplus1();
}
}
}
let mut a = A { i : 1, kvec : vec![1,2,3]};
a.foo();
a.bar();
a.baz();
a.badbar();
a.badbaz();
println!("{:?}", a);
}