How to use match to unpack (extract) a tuple in Rust

3 Answers

0 votes
fn main() {
    let triple = (6, 3.14, -98);

    match triple {
        (6, y, z) => println!("0, {:?}, {:?}", y, z),
        (3, ..) => println!("First = 3, the rest doesn't matter"),
        (.., 7) => println!("Last = 7, the rest doesn't matter"),
        (9, .., -21) => println!("First = 9, last = -21, the rest doesn't matter"),

        _ => println!("else, doesn't matter"),
    }
}




/*
run:

0, 3.14, -98

*/

 



answered May 4, 2023 by avibootz
edited May 4, 2023 by avibootz
0 votes
fn main() {
    let triple = (9, 5, 0, -4, 4.76, -21);

    match triple {
        (6, y, z, ..) => println!("0, {:?}, {:?}", y, z),
        (3, ..) => println!("First = 3, the rest doesn't matter"),
        (.., 7) => println!("Last = 7, the rest doesn't matter"),
        (9, .., -21) => println!("First = 9, last = -21, the rest doesn't matter"),

        _ => println!("else, doesn't matter"),
    }
}




/*
run:

First = 9, last = -21, the rest doesn't matter

*/

 



answered May 4, 2023 by avibootz
0 votes
fn main() {
    let triple = (9, 5, 0, -4, 4.76, 0.003, 98);

    match triple {
        (6, y, z, ..) => println!("0, {:?}, {:?}", y, z),
        (3, ..) => println!("First = 3, the rest doesn't matter"),
        (.., 7) => println!("Last = 7, the rest doesn't matter"),
        (9, .., -21) => println!("First = 9, last = -21, the rest doesn't matter"),

        _ => println!("else, doesn't matter"),
    }
}




/*
run:

else, doesn't matter

*/

 



answered May 4, 2023 by avibootz

Related questions

3 answers 190 views
4 answers 212 views
1 answer 122 views
2 answers 137 views
137 views asked May 5, 2023 by avibootz
1 answer 111 views
111 views asked May 5, 2023 by avibootz
2 answers 167 views
...