How to Use while let with Enums in Rust

Use while let to loop over enum variants by matching a specific pattern in the condition.

Use while let to loop over an enum variant by matching a specific pattern in the condition and executing the block only when that pattern matches.

enum Message {
    Quit,
    Move { x: i32, y: i32 },
    Write(String),
    ChangeColor(u8, u8, u8),
}

fn main() {
    let mut messages = vec![
        Message::Move { x: 10, y: 20 },
        Message::Write(String::from("hello")),
        Message::Quit,
    ];

    while let Message::Move { x, y } = messages.pop().unwrap_or(Message::Quit) {
        println!("Moving to ({}, {})", x, y);
    }
}