|
1 |
| -#[derive(Debug)] |
2 |
| -struct NestedStruct { |
3 |
| - // A nested struct that holds an array. |
4 |
| - values: [i32; 3], |
| 1 | +struct Inner { |
| 2 | + x: i32, |
| 3 | + y: (i32, i32), |
5 | 4 | }
|
6 | 5 |
|
7 |
| -#[derive(Debug)] |
8 |
| -struct TestStruct { |
9 |
| - field1: i32, |
10 |
| - field2: String, |
11 |
| - // Nested struct inside the main struct. |
12 |
| - nested: NestedStruct, |
| 6 | +struct Outer { |
| 7 | + label: String, |
| 8 | + inner_struct: Inner, |
| 9 | + data: [i32; 3], |
13 | 10 | }
|
14 | 11 |
|
15 | 12 | fn main() {
|
16 |
| - // Initialize the nested struct with an array. |
17 |
| - let nested = NestedStruct { values: [10, 20, 30] }; |
18 |
| - |
19 |
| - // Initialize the main structure with two simple fields and one nested struct. |
20 |
| - let test_struct = TestStruct { |
21 |
| - field1: 180, |
22 |
| - field2: "rust".to_string(), |
23 |
| - nested, |
| 13 | + // === Nested STRUCT + TUPLE + ARRAY === |
| 14 | + let mut outer = Outer { |
| 15 | + label: "start".to_string(), |
| 16 | + inner_struct: Inner { |
| 17 | + x: 100, |
| 18 | + y: (5, 10), |
| 19 | + }, |
| 20 | + data: [1, 2, 3], |
24 | 21 | };
|
25 | 22 |
|
26 |
| - // Test simple field accesses. |
27 |
| - assert!(test_struct.field1 == 180, "Field1 should be 180"); |
28 |
| - assert!(test_struct.field2 == "rust", "Field2 should be 'rust'"); |
29 |
| - |
30 |
| - // Test nested field access (accessing a field from the nested struct). |
31 |
| - assert!(test_struct.nested.values[0] == 10, "First element in nested.values should be 10"); |
32 |
| - assert!(test_struct.nested.values[1] == 20, "Second element in nested.values should be 20"); |
33 |
| - assert!(test_struct.nested.values[2] == 30, "Third element in nested.values should be 30"); |
34 |
| - |
35 |
| - // If you want to test even deeper nesting, for instance, an array containing nested structs: |
36 |
| - let more_nested: [TestStruct; 2] = [ |
37 |
| - TestStruct { |
38 |
| - field1: 100, |
39 |
| - field2: "foo".to_string(), |
40 |
| - nested: NestedStruct { values: [1, 2, 3] }, |
41 |
| - }, |
42 |
| - TestStruct { |
43 |
| - field1: 200, |
44 |
| - field2: "bar".to_string(), |
45 |
| - nested: NestedStruct { values: [4, 5, 6] }, |
46 |
| - }, |
47 |
| - ]; |
| 23 | + // === Access nested values === |
| 24 | + assert!(outer.label == "start"); |
| 25 | + assert!(outer.inner_struct.x == 100, "Inner x should be 100"); |
| 26 | + assert!(outer.inner_struct.y.0 == 5, "Inner tuple y.0 should be 5"); |
| 27 | + assert!(outer.inner_struct.y.1 == 10, "Inner tuple y.1 should be 10"); |
| 28 | + assert!(outer.data[0] == 1, "Array element at index 0 should be 1"); |
| 29 | + |
| 30 | + // === Mutate nested values === |
| 31 | + outer.label = "updated".to_string(); |
| 32 | + outer.inner_struct.x = 200; |
| 33 | + outer.inner_struct.y.1 = 999; |
| 34 | + outer.data[1] = 42; |
| 35 | + |
| 36 | + // === Assert mutations === |
| 37 | + assert!(outer.label == "updated", "Outer label should be updated"); |
| 38 | + assert!(outer.inner_struct.x == 200, "Inner x should now be 200"); |
| 39 | + assert!(outer.inner_struct.y.1 == 999, "Inner tuple y.1 should now be 999"); |
| 40 | + assert!(outer.data[1] == 42, "Array element at index 1 should now be 42"); |
| 41 | + |
| 42 | + // === Tuple nesting test === |
| 43 | + let mut big_tuple = ( |
| 44 | + (10, 20), |
| 45 | + Inner { x: 50, y: (7, 8) }, |
| 46 | + ["a", "b", "c"], |
| 47 | + ); |
| 48 | + |
| 49 | + // Access nested tuple values |
| 50 | + assert!((big_tuple.0).1 == 20, "First tuple's second element should be 20"); |
| 51 | + assert!(big_tuple.1.y.0 == 7, "Nested tuple inside struct should be 7"); |
| 52 | + assert!(big_tuple.2[2] == "c", "Array inside tuple should contain 'c' at index 2"); |
| 53 | + |
| 54 | + // Mutate nested values |
| 55 | + (big_tuple.0).0 = 99; |
| 56 | + big_tuple.1.x = 123; |
| 57 | + big_tuple.2[1] = "z"; |
48 | 58 |
|
49 |
| - // Test access into the array and then the nested struct. |
50 |
| - assert!(more_nested[0].field1 == 100, "First element's field1 should be 100"); |
51 |
| - assert!(more_nested[1].nested.values[2] == 6, "Second element's nested.values[2] should be 6"); |
| 59 | + // Assert changes |
| 60 | + assert!((big_tuple.0).0 == 99, "Tuple value mutated to 99"); |
| 61 | + assert!(big_tuple.1.x == 123, "Inner struct field x mutated to 123"); |
| 62 | + assert!(big_tuple.2[1] == "z", "Tuple's array element at index 1 mutated to 'z'"); |
52 | 63 | }
|
0 commit comments