Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can I not borrow a boxed vector content as mutable?

Tags:

vector

rust

Why this doesn't compile:

fn main() {
    let mut b = Box::new(Vec::new());
    b.push(Vec::new());
    b.get_mut(0).unwrap().push(1);
}

While this does:

fn main() {
    let a = Box::new(Vec::new());
    let mut b = *a;
    b.push(Vec::new());
    b.get_mut(0).unwrap().push(1);
}

And also this does:

fn main() {
    let mut b = Vec::new();
    b.push(Vec::new());
    b.get_mut(0).unwrap().push(Vec::new());
    b.get_mut(0).unwrap().get_mut(0).unwrap().push(1)
}

The first and third one for me are the conceptually the same - Box of a Vector of Vectors of integers and a Vector of Vector of Vectors of integers, but the last one results in each vector being mutable, whereas the first one makes the inner vector immutable.

like image 915
PL_kolek Avatar asked Oct 20 '22 17:10

PL_kolek


1 Answers

In at least Rust 1.25.0, all three original examples work. This was a bug in some previous version of Rust.

like image 100
Shepmaster Avatar answered Oct 28 '22 21:10

Shepmaster