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,879 questions

51,805 answers

573 users

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

3 Answers

0 votes
fn main() {
    struct ST {
        x: (u32, u32),
        y: u32,
    }

    let st = ST { x: (7, 8), y: 2 };

    match st {
        ST { x: (1, b), y } => println!("x = 1, b = {}, y = {} ", b, y),

        ST { y: 2, x: val } => println!("y = 2, x = {:?}", val),

        ST { y, .. } => println!("y = {}, x = don't needed", y),
    }
}



/*
run:

y = 2, x = (7, 8)

*/

 



answered May 4, 2023 by avibootz
0 votes
fn main() {
    struct ST {
        x: (u32, u32),
        y: u32,
    }

    let st = ST { x: (7, 8), y: 9 };

    match st {
        ST { x: (7, b), y } => println!("x = 7, b = {}, y = {} ", b, y),

        ST { y: 2, x: val } => println!("y = 2, x = {:?}", val),

        ST { y, .. } => println!("y = {}, x = don't needed", y),
    }
}



/*
run:

x = 7, b = 8, y = 9 

*/

 



answered May 4, 2023 by avibootz
0 votes
fn main() {
    struct ST {
        x: (u32, u32),
        y: u32,
    }

    let st = ST { x: (7, 8), y: 9 };

    match st {
        ST { x: (41, b), y } => println!("x = 7, b = {}, y = {} ", b, y),

        ST { y: 2, x: val } => println!("y = 2, x = {:?}", val),

        ST { y, .. } => println!("y = {}, x = don't needed", y),
    }
}



/*
run:

y = 9, x = don't needed

*/

 



answered May 4, 2023 by avibootz

Related questions

4 answers 190 views
3 answers 173 views
1 answer 109 views
1 answer 137 views
1 answer 125 views
125 views asked Oct 25, 2022 by avibootz
2 answers 120 views
120 views asked May 5, 2023 by avibootz
1 answer 78 views
78 views asked May 5, 2023 by avibootz
...