News for hackers. Stuff that matters.
Re:Rust, Lifetimes, and Collections by veddan on Wednesday November 19, 2014 @03:52PM (#8632542)
> I really hope that the Rust compiler has some really good error messages explaining what the problem is because I couldn't see what was wrong with this fragment until I read the explanation.

They're not always perfect, but in this case it's quite good.

    err.rs:4:9: 4:10 error: cannot borrow `v` as mutable because it is also borrowed as immutable
    err.rs:4         v.push(6); // oops, sorry x!
                     ^
    err.rs:3:17: 3:18 note: previous borrow of `v` occurs here; the immutable borrow prevents subsequent moves or mutable borrows of `v` until the borrow ends
    err.rs:3         let x = v.get(2);
                             ^
    err.rs:5:6: 5:6 note: previous borrow ends here
    err.rs:1     fn main() {
    err.rs:2         let mut v = vec![1i32, 2, 3, 4, 5];
    err.rs:3         let x = v.get(2);
    err.rs:4         v.push(6); // oops, sorry x!
    err.rs:5     }
                 ^
If you want to try out code examples, the Rust playpen is nice: http://play.rust-lang.org

There are no comments on this comment yet.