Search code examples
rusttuples

How to Get the Second Field of a Tuple in Rust?


I'm trying to copy only one section of an array, and I got kind of lost when one of the methods returned a tuple with two generics instead of an array.

Here's the code:

for i in 0..v.len() {
    stack = Vecthing::clone_vec(&v)
                       .as_mut_slice()
                       .split_at(i)
                       .B// aware this doesn't work just showing where I try to access the second field
                       .iter()
                       .collect();
}

Any help would be greatly appreciated!


Solution

  • Rust tuples can be indexed by . followed by their index, in your case:

    for i in 0..v.len() {
        stack = Vecthing::clone_vec(&v)
                           .as_mut_slice()
                           .split_at(i)
                           .1
                           .iter()
                           .collect();
    }
    

    (As your example is not reproducible, I cannot easily verify my answer.)

    For more information, see the Rust documentation.