Programming Languages
Rust
Subjective
Oct 04, 2025
What are enums in Rust and how do they differ from other languages?
Detailed Explanation
Enums in Rust:
• Algebraic data types
• Variants can hold data
• Each variant can have different types
• Pattern matching required
• Memory efficient
Example:
enum Message {
Quit,
Move { x: i32, y: i32 },
Write(String),
ChangeColor(i32, i32, i32),
}
fn process_message(msg: Message) {
match msg {
Message::Quit => println!("Quit"),
Message::Move { x, y } => println!("Move to {}, {}", x, y),
Message::Write(text) => println!("Text: {}", text),
Message::ChangeColor(r, g, b) => println!("RGB: {}, {}, {}", r, g, b),
}
}
Discussion (0)
No comments yet. Be the first to share your thoughts!
Share Your Thoughts