-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenums.rs
More file actions
61 lines (51 loc) · 1.67 KB
/
enums.rs
File metadata and controls
61 lines (51 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
//! enums.rs
//!
//! Introduction to Rust enums — one of Rust's most powerful features.
//! Covers basic enums, enums with data, and the match expression.
fn main() {
// 1. Basic enum (no data)
#[derive(Debug)]
enum Color {
Red,
Green,
Blue,
}
let favorite_color: Color = Color::Blue;
let primary_color: Color = Color::Red;
let secondary_color: Color = Color::Green;
println!("Favorite color: {:?}", favorite_color);
println!("Primary color: {:?}", primary_color);
println!("Secondary color: {:?}", secondary_color);
// 2. Enum with data (most useful form)
#[derive(Debug)]
enum Message {
Quit,
Move { x: i32, y: i32 },
Write(String),
ChangeColor(i32, i32, i32),
}
let msg1: Message = Message::Quit;
let msg2: Message = Message::Move { x: 10, y: 20 };
let msg3: Message = Message::Write(String::from("Hello Rust!"));
let msg4: Message = Message::ChangeColor(255, 0, 128);
// 3. Using match to handle every variant
fn process_message(msg: Message) {
match msg {
Message::Quit => println!("The program is quitting..."),
Message::Move { x, y } => {
println!("Moving to coordinates: ({}, {})", x, y);
}
Message::Write(text) => {
println!("Writing message: {}", text);
}
Message::ChangeColor(r, g, b) => {
println!("Changing color to RGB({}, {}, {})", r, g, b);
}
}
}
// Process every message variant
process_message(msg1);
process_message(msg2);
process_message(msg3);
process_message(msg4);
}