Setting a tuple elem of a vec

Suppose we have:

arr: Vec<(K, V)>;
i: usize;
v_new: V

Now, I can update the vec via:

let t = &mut arr[i];
t.1 = v_new;

However, I am wondering if there is a simpler way to do this.

Just something like t[0].1 = v_new works for me:

fn test<K, V>(a: K, b: V, c: V) -> Vec<(K, V)> {
    let mut t: Vec<(K, V)> = Vec::new();
    t.push((a, b));
    t[0].1 = c;
    t
}

fn main() {
    println!("{:?}", test(1, 2, 3));
}

(Playground)

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.