-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.rs
142 lines (125 loc) · 4.93 KB
/
main.rs
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#[derive(PartialEq)]
struct ConfigData {
id: u32,
enabled: bool,
params: (f32, f32), // Nested tuple
}
#[derive(PartialEq)]
enum ComplexEnum<'a> {
SimpleVariant, // No data
Count(i64), // Single primitive data
Coords((i32, i32, i32)), // Tuple data
UserData { name: &'a str, age: u8 }, // Struct-like variant
RawData([u8; 8]), // Array data
Settings(ConfigData), // Struct data
}
fn main() {
// Initialize with one variant
let mut current_state = ComplexEnum::UserData {
name: "Alice",
age: 30,
};
// Access initial values using match
match current_state {
ComplexEnum::UserData { ref name, age } => {
assert!(*name == "Alice", "Initial name should be Alice");
assert!(age == 30, "Initial age should be 30");
}
_ => panic!("Initial state should be UserData!"),
}
// Access initial values using if let
if let ComplexEnum::UserData { name, .. } = current_state {
assert!(name.starts_with('A'), "Name should start with A");
} else {
panic!("Still expect UserData here!");
}
// Mutate the enum variable to a different variant (Settings)
current_state = ComplexEnum::Settings(ConfigData {
id: 123,
enabled: true,
params: (1.0, -0.5),
});
// Access nested values in the new variant
match current_state {
ComplexEnum::Settings(config) => {
assert!(config.id == 123, "Settings ID should be 123");
assert!(config.enabled, "Settings should be enabled");
assert!(config.params.0 == 1.0, "Settings params.0 should be 1.0");
assert!(config.params.1 == -0.5, "Settings params.1 should be -0.5");
}
_ => panic!("State should now be Settings!"),
}
// Mutate the enum variable to a different variant (RawData)
current_state = ComplexEnum::RawData([0, 1, 2, 3, 4, 5, 6, 7]);
// Mutate data *inside* the RawData variant
let mut mutated_internally = false;
match &mut current_state {
// Use mutable borrow (&mut) to modify internal data
ComplexEnum::RawData(data_array) => {
assert!(data_array[0] == 0, "RawData[0] should be 0 initially");
assert!(data_array[7] == 7, "RawData[7] should be 7 initially");
// Modify elements
data_array[0] = 100;
data_array[7] = 200;
data_array[1] *= 5; // Modify based on existing value
mutated_internally = true;
}
_ => { /*No mutation needed for other branches in this step*/ }
}
assert!(mutated_internally, "Internal mutation should have happened");
// Assert internal mutations in RawData
if let ComplexEnum::RawData(data_array) = current_state {
assert!(data_array[0] == 100, "RawData[0] should now be 100");
assert!(data_array[1] == 5, "RawData[1] should now be 5 (1*5)");
assert!(data_array[7] == 200, "RawData[7] should now be 200");
} else {
panic!("State should still be RawData after internal mutation!");
}
// Mutate data *inside* the nested ConfigData struct within Settings variant
current_state = ComplexEnum::Settings(ConfigData {
// Reset state
id: 999,
enabled: false,
params: (0.0, 0.0),
});
match &mut current_state {
ComplexEnum::Settings(config) => {
config.enabled = true; // Mutate bool field
config.params.1 = config.params.0 + 10.0; // Mutate tuple field
config.id += 1; // Mutate id field
}
_ => panic!("State should be Settings for nested mutation!"),
}
// Assert internal nested mutations
match current_state {
ComplexEnum::Settings(config) => {
assert!(config.id == 1000, "ConfigData id should be 1000");
assert!(config.enabled, "ConfigData enabled should be true");
assert!(
config.params.0 == 0.0,
"ConfigData params.0 should be unchanged"
);
assert!(config.params.1 == 10.0, "ConfigData params.1 should be 10.0");
}
_ => panic!("State should still be Settings after nested mutation!"),
}
// Test remaining variants
current_state = ComplexEnum::Count(5000);
assert!(current_state == ComplexEnum::Count(5000));
if let ComplexEnum::Count(c) = current_state {
assert!(c == 5000);
} else {
panic!("State should be Count");
}
current_state = ComplexEnum::Coords((-10, 0, 20));
assert!(current_state == ComplexEnum::Coords((-10, 0, 20)));
if let ComplexEnum::Coords((x, y, z)) = current_state {
assert!(x == -10);
assert!(y == 0);
assert!(z == 20);
} else {
panic!("State should be Coords");
}
current_state = ComplexEnum::SimpleVariant;
assert!(current_state == ComplexEnum::SimpleVariant);
}