Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,895 questions

51,826 answers

573 users

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 155 views
4 answers 190 views
1 answer 110 views
2 answers 121 views
121 views asked May 5, 2023 by avibootz
1 answer 78 views
78 views asked May 5, 2023 by avibootz
2 answers 145 views
2 answers 112 views
112 views asked May 3, 2023 by avibootz
...