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 Vec
tor of Vec
tors of integers and a Vec
tor of Vec
tor of Vec
tors of integers, but the last one results in each vector being mutable, whereas the first one makes the inner vector immutable.
In at least Rust 1.25.0, all three original examples work. This was a bug in some previous version of Rust.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With