-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
121 additions
and
203 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
--- | ||
minutes: 5 | ||
--- | ||
|
||
# Destructuring | ||
|
||
You can destructure tuples and arrays by matching on their elements: | ||
|
||
## Tuples | ||
|
||
```rust,editable | ||
fn main() { | ||
describe_point((1, 0)); | ||
} | ||
fn describe_point(point: (i32, i32)) { | ||
match point { | ||
(0, _) => println!("on Y axis"), | ||
(_, 0) => println!("on X axis"), | ||
(x, _) if x < 0 => println!("left of Y axis"), | ||
(_, y) if y < 0 => println!("below X axis"), | ||
_ => println!("first quadrant"), | ||
} | ||
} | ||
``` | ||
|
||
## Arrays | ||
|
||
```rust,editable | ||
{{#include ../../third_party/rust-by-example/destructuring-arrays.rs}} | ||
``` | ||
|
||
<details> | ||
|
||
* Create a new pattern using `_` to represent an element. | ||
* Add more values to the array. | ||
* Point out that how `..` will expand to account for different number of elements. | ||
* Show matching against the tail with patterns `[.., b]` and `[a@..,b]` | ||
|
||
</details> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
--- | ||
minutes: 3 | ||
--- | ||
|
||
# Array Iteration | ||
|
||
The `for` statement supports iterating over arrays (but not tuples). | ||
|
||
```rust,editable | ||
fn main() { | ||
let primes = [2, 3, 5, 7, 11, 13, 17, 19]; | ||
for prime in primes { | ||
for i in 2..prime { | ||
assert_ne!(prime % i, 0); | ||
} | ||
} | ||
} | ||
``` | ||
|
||
<details> | ||
|
||
This functionality uses the `IntoIterator` trait, but we haven't covered that yet. | ||
|
||
The `assert_ne!` macro is new here. There are also `assert_eq!` and `assert!` | ||
macros. These are always checked while, debug-only variants like | ||
`debug_assert!` compile to nothing in release builds. | ||
|
||
</details> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.