简体   繁体   中英

How do I push a value into a 2D Vec in Rust?

Here is a really simple attempt at a 2D Vec . I'm trying to add an element to the last entry in the top-level Vec :

fn main() {
    let mut vec_2d = vec![vec![]];
    if let Some(v) = vec_2d.last() {
        v.push(1);
    }
    println!("{:?}", vec_2d);
}

I get this error:

error[E0596]: cannot borrow `*v` as mutable, as it is behind a `&` reference
 --> src/main.rs:4:9
  |
3 |     if let Some(v) = vec_2d.last() {
  |                 - help: consider changing this to be a mutable reference: `&mut std::vec::Vec<i32>`
4 |         v.push(1);
  |         ^ `v` is a `&` reference, so the data it refers to cannot be borrowed as mutable

I've also tried Some(ref v) and Some(ref mut v) with the same results. I can't find any documentation that describes this error specifically. What is the right approach here?

An answer to a similar question recommends something more like Some(&mut v) . Then I get these errors:

error[E0308]: mismatched types
 --> src/main.rs:3:17
  |
3 |     if let Some(&mut v) = vec_2d.last() {
  |                 ^^^^^^ types differ in mutability
  |
  = note: expected type `&std::vec::Vec<_>`
             found type `&mut _`
  = help: did you mean `mut v: &&std::vec::Vec<_>`?

If I try Some(&ref mut v) I get:

error[E0596]: cannot borrow data in a `&` reference as mutable
 --> src/main.rs:3:18
  |
3 |     if let Some(&ref mut v) = vec_2d.last() {
  |                  ^^^^^^^^^ cannot borrow as mutable

Grab a mutable reference to the last element with last_mut ; no need to change patterns.

fn main() {
    let mut vec_2d = vec![vec![]];
    if let Some(v) = vec_2d.() {
        v.push(1);
    }
    println!("{:?}", vec_2d);
}

A (much) more elegant solution for this particular case would be:

fn main() {
    let vec_2d = vec![vec![1i32]];

    println!("{:?}", vec_2d);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM