Skip to content

Commit 4ce87c5

Browse files
authored
Improve tuple destructuring (#2582)
This slide had two code samples, neither of which had a `main` and thus neither of which would run. This removes the first (which is redundant to one a few slides earlier), adds a `main`, and expands the second to use a 3-tuple.
1 parent b3734de commit 4ce87c5

File tree

1 file changed

+11
-15
lines changed

1 file changed

+11
-15
lines changed

src/tuples-and-arrays/destructuring.md

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,21 @@ minutes: 5
44

55
# Patterns and Destructuring
66

7-
When working with tuples and other structured values it's common to want to
8-
extract the inner values into local variables. This can be done manually by
9-
directly accessing the inner values:
7+
Rust supports using pattern matching to destructure a larger value like a tuple
8+
into its constituent parts:
109

1110
```rust,editable
12-
fn print_tuple(tuple: (i32, i32)) {
13-
let left = tuple.0;
14-
let right = tuple.1;
15-
println!("left: {left}, right: {right}");
11+
fn check_order(tuple: (i32, i32, i32)) -> bool {
12+
let (left, middle, right) = tuple;
13+
left < middle && middle < right
1614
}
17-
```
1815
19-
However, Rust also supports using pattern matching to destructure a larger value
20-
into its constituent parts:
21-
22-
```rust,editable
23-
fn print_tuple(tuple: (i32, i32)) {
24-
let (left, right) = tuple;
25-
println!("left: {left}, right: {right}");
16+
fn main() {
17+
let tuple = (1, 5, 3);
18+
println!(
19+
"{tuple:?}: {}",
20+
if check_order(tuple) { "ordered" } else { "unordered" }
21+
);
2622
}
2723
```
2824

0 commit comments

Comments
 (0)