On 4/16/14 5:39 PM, Frank Huang wrote:
Second somewhat related question: why can't I write this?

fn main() {
   let mut v = Vec::new();
   v.push(10);
   // get last item
   let n = v.get_mut(v.len()-1);
   println!("item = {}", *n);
}

For this program, the compiler complains at the line with v.get_mut that
"cannot borrow `v` as immutable because is is also borrowed as mutable".
Is there another way to retrieve, mutably, the last element of a vector?
(this is relevant if I want to have a vector of Handles, since
Handle::recv() requires &mut self)

Unnest it and assign "v.len()" to a temporary first. This is a borrow check annoyance that will probably be fixed at some point. In the meantime we should probably have a better error message.

Patrick

_______________________________________________
Rust-dev mailing list
Rust-dev@mozilla.org
https://mail.mozilla.org/listinfo/rust-dev

Reply via email to