@@ -7,9 +7,9 @@ What is Destructuring?
77------------------------
88
99 - **Convenient ** data access
10- - **Breaking down ** a complex data structure into components
10+ - **Breaking down ** a complex data structure into its inner parts
1111 - Like a tuple, an array, or other compound types
12- - **Assigning ** those components to individual variables
12+ - **Assigning ** those parts to individual variables
1313 - In a single step!
1414
1515-----------------------
@@ -41,10 +41,10 @@ Irrefutable Patterns with Tuples
4141
4242 let point: (i32, i32) = (10, 20);
4343
44- // The pattern (xx, yy) is IRREFUTABLE
45- // It perfectly matches the structure of that tuple
44+ // Pattern (xx, yy) is IRREFUTABLE
45+ // Perfectly matches the structure of that tuple
4646 let (xx, yy) = point;
47- // We're guaranteed to get an xx and a yy!
47+ // Guaranteed to get an xx and a yy!
4848
4949--------------------------
5050Destructuring Assignment
@@ -56,9 +56,11 @@ Destructuring Assignment
5656
5757.. code :: rust
5858
59- let mut cat_snacks = 0;
60- let mut dog_treats = 0;
61- (cat_snacks, dog_treats) = (42, 1);
59+ let mut cat_snacks = 1;
60+ let mut dog_treats = 42;
61+
62+ // No temporary variable required!
63+ (cat_snacks, dog_treats) = (dog_treats, cat_snacks);
6264
6365------------------------
6466Destructuring an Array
@@ -77,9 +79,9 @@ Destructuring an Array
7779 println!("pants: {}", pants); // 20
7880 println!("socks: {}", socks); // 30
7981
80- -------------------
81- Ignoring Elements
82- -------------------
82+ ----------------------------
83+ Ignoring Specific Elements
84+ ----------------------------
8385
8486 - Ignore specific elements using the underscore (:rust: `_ `)
8587
@@ -92,9 +94,9 @@ Ignoring Elements
9294 println!("Second color: {}", second); // green
9395 println!("Fourth color: {}", fourth); // yellow
9496
95- -------------------------
96- Using ".." for the Rest
97- -------------------------
97+ ----------------------------
98+ Ignoring Multiple Elements
99+ ----------------------------
98100
99101 - Ignore multiple elements using the **rest pattern ** (:rust: `.. `)
100102 - Useful when you only need elements from the beginning or end
@@ -113,10 +115,12 @@ Using ".." for the Rest
113115Nested Destructuring
114116----------------------
115117
116- - Use a pattern within a pattern to destructure an array of arrays
118+ - Use a pattern * within * a pattern to destructure an array of arrays
117119
118120.. code :: rust
119121
120- let matrix = [[1, 2], [3, 4]];
121- // Destructures the outer array AND the inner array
122- let [[a, b], [c, d]] = matrix; // a=1, b=2, c=3, d=4
122+ let line_data = [[10, 20], [80, 90]];
123+ let [[start_x, start_y], [end_x, end_y]] = line_data;
124+
125+ println!("Drawing line from ({}, {}) to ({}, {})",
126+ start_x, start_y, end_x, end_y);
0 commit comments