Nested match pattern syntax

   match event {
         ....
            winit::event::Event::WindowEvent {
                event: WindowEvent::KeyboardInput { 
                    event: winit::event::KeyEvent {
                        physical_key,
                        state,
                        .. },
                    is_synthetic,
                    .. },
                 ..
                } => { 
               // Do something with inner event
     }

I have a match pattern with two nested structs, and the fields of interest are both called "event". How do I talk about those names separately? I want the KeyEvent entire value out.

Don't create a pattern for KeyEvent then. Just handle the inner event value directly. Here's an example:

struct A {
   pub event: B
}

struct B {
   pub event: C
}

#[derive(Debug)]
struct C {
   pub value: String
}
fn main() {
    let event = A {
        event: B {
            event: C {
                value: "get me".to_owned()
            }
        }
    };
    
    let extracted = match event {
        A {
            event: B {
                event
            }
        } => event 
    };
    
    println!("{extracted:?}");
}
1 Like

Turns out you can write

event: alternate_name @

to get out of this situation. Obscure Rust feature.

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.