|
| 1 | +#[derive(PartialEq, Clone, Debug)] |
| 2 | +struct ConfigData { |
| 3 | + id: u32, |
| 4 | + enabled: bool, |
| 5 | + params: (f32, f32), // Nested tuple |
| 6 | +} |
| 7 | + |
| 8 | +#[derive(PartialEq, Clone, Debug)] |
| 9 | +enum ComplexEnum { |
| 10 | + SimpleVariant, // No data |
| 11 | + Count(i64), // Single primitive data |
| 12 | + Coords((i32, i32, i32)), // Tuple data |
| 13 | + UserData { name: String, age: u8 }, // Struct-like variant |
| 14 | + RawData([u8; 8]), // Array data |
| 15 | + Settings(ConfigData), // Struct data |
| 16 | +} |
| 17 | + |
| 18 | +fn main() { |
| 19 | + // Initialize with one variant |
| 20 | + let mut current_state = ComplexEnum::UserData { |
| 21 | + name: "Alice".to_string(), |
| 22 | + age: 30, |
| 23 | + }; |
| 24 | + |
| 25 | + // Access initial values using match |
| 26 | + match current_state { |
| 27 | + ComplexEnum::UserData { ref name, age } => { |
| 28 | + assert!(name == "Alice", "Initial name should be Alice"); |
| 29 | + assert!(age == 30, "Initial age should be 30"); |
| 30 | + } |
| 31 | + _ => panic!("Initial state should be UserData!"), |
| 32 | + } |
| 33 | + |
| 34 | + // Access initial values using if let |
| 35 | + if let ComplexEnum::UserData { name, .. } = current_state { |
| 36 | + assert!(name.starts_with('A'), "Name should start with A"); |
| 37 | + } else { |
| 38 | + panic!("Still expect UserData here!"); |
| 39 | + } |
| 40 | + |
| 41 | + // Mutate the enum variable to a different variant (Settings) |
| 42 | + current_state = ComplexEnum::Settings(ConfigData { |
| 43 | + id: 123, |
| 44 | + enabled: true, |
| 45 | + params: (1.0, -0.5), |
| 46 | + }); |
| 47 | + |
| 48 | + // Access nested values in the new variant |
| 49 | + match current_state { |
| 50 | + ComplexEnum::Settings(config) => { |
| 51 | + assert!(config.id == 123, "Settings ID should be 123"); |
| 52 | + assert!(config.enabled, "Settings should be enabled"); |
| 53 | + assert!(config.params.0 == 1.0, "Settings params.0 should be 1.0"); |
| 54 | + assert!(config.params.1 == -0.5, "Settings params.1 should be -0.5"); |
| 55 | + } |
| 56 | + _ => panic!("State should now be Settings!"), |
| 57 | + } |
| 58 | + |
| 59 | + // Mutate the enum variable to a different variant (RawData) |
| 60 | + current_state = ComplexEnum::RawData([0, 1, 2, 3, 4, 5, 6, 7]); |
| 61 | + |
| 62 | + // Mutate data *inside* the RawData variant |
| 63 | + let mut mutated_internally = false; |
| 64 | + match &mut current_state { |
| 65 | + // Use mutable borrow (&mut) to modify internal data |
| 66 | + ComplexEnum::RawData(data_array) => { |
| 67 | + assert!(data_array[0] == 0, "RawData[0] should be 0 initially"); |
| 68 | + assert!(data_array[7] == 7, "RawData[7] should be 7 initially"); |
| 69 | + |
| 70 | + // Modify elements |
| 71 | + data_array[0] = 100; |
| 72 | + data_array[7] = 200; |
| 73 | + data_array[1] *= 5; // Modify based on existing value |
| 74 | + |
| 75 | + mutated_internally = true; |
| 76 | + } |
| 77 | + _ => { /* No mutation needed for other branches in this step */ } |
| 78 | + } |
| 79 | + assert!(mutated_internally, "Internal mutation should have happened"); |
| 80 | + |
| 81 | + // Assert internal mutations in RawData |
| 82 | + if let ComplexEnum::RawData(data_array) = current_state { |
| 83 | + assert!(data_array[0] == 100, "RawData[0] should now be 100"); |
| 84 | + assert!(data_array[1] == 5, "RawData[1] should now be 5 (1*5)"); |
| 85 | + assert!(data_array[7] == 200, "RawData[7] should now be 200"); |
| 86 | + } else { |
| 87 | + panic!("State should still be RawData after internal mutation!"); |
| 88 | + } |
| 89 | + |
| 90 | + // Mutate data *inside* the nested ConfigData struct within Settings variant |
| 91 | + current_state = ComplexEnum::Settings(ConfigData { |
| 92 | + // Reset state |
| 93 | + id: 999, |
| 94 | + enabled: false, |
| 95 | + params: (0.0, 0.0), |
| 96 | + }); |
| 97 | + |
| 98 | + match &mut current_state { |
| 99 | + ComplexEnum::Settings(config) => { |
| 100 | + config.enabled = true; // Mutate bool field |
| 101 | + config.params.1 = config.params.0 + 10.0; // Mutate tuple field |
| 102 | + config.id += 1; // Mutate id field |
| 103 | + } |
| 104 | + _ => panic!("State should be Settings for nested mutation!"), |
| 105 | + } |
| 106 | + |
| 107 | + // Assert internal nested mutations |
| 108 | + match current_state { |
| 109 | + ComplexEnum::Settings(config) => { |
| 110 | + assert!(config.id == 1000, "ConfigData id should be 1000"); |
| 111 | + assert!(config.enabled, "ConfigData enabled should be true"); |
| 112 | + assert!( |
| 113 | + config.params.0 == 0.0, |
| 114 | + "ConfigData params.0 should be unchanged" |
| 115 | + ); |
| 116 | + assert!(config.params.1 == 10.0, "ConfigData params.1 should be 10.0"); |
| 117 | + } |
| 118 | + _ => panic!("State should still be Settings after nested mutation!"), |
| 119 | + } |
| 120 | + |
| 121 | + // Test remaining variants |
| 122 | + current_state = ComplexEnum::Count(5000); |
| 123 | + assert!(current_state == ComplexEnum::Count(5000)); |
| 124 | + if let ComplexEnum::Count(c) = current_state { |
| 125 | + assert!(c == 5000); |
| 126 | + } else { |
| 127 | + panic!("State should be Count"); |
| 128 | + } |
| 129 | + |
| 130 | + current_state = ComplexEnum::Coords((-10, 0, 20)); |
| 131 | + assert!(current_state == ComplexEnum::Coords((-10, 0, 20))); |
| 132 | + if let ComplexEnum::Coords((x, y, z)) = current_state { |
| 133 | + assert!(x == -10); |
| 134 | + assert!(y == 0); |
| 135 | + assert!(z == 20); |
| 136 | + } else { |
| 137 | + panic!("State should be Coords"); |
| 138 | + } |
| 139 | + |
| 140 | + current_state = ComplexEnum::SimpleVariant; |
| 141 | + assert!(current_state == ComplexEnum::SimpleVariant); |
| 142 | +} |
0 commit comments