Skip to content

Commit b32e797

Browse files
Update destructuring chapter from feedback
1 parent 55ed183 commit b32e797

File tree

1 file changed

+22
-18
lines changed

1 file changed

+22
-18
lines changed

courses/rust_essentials/050_tuples_and_arrays/03_destructuring.rst

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -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
--------------------------
5050
Destructuring 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
------------------------
6466
Destructuring 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
113115
Nested 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

Comments
 (0)