You've already forked comprehensive-rust
mirror of
https://github.com/google/comprehensive-rust.git
synced 2025-12-03 09:45:17 +02:00
Updated struct and enums in pattern-matching. (#2021)
#1464 issue. --------- Co-authored-by: Martin Geisler <martin@geisler.net>
This commit is contained in:
committed by
GitHub
parent
9a66fb1451
commit
48325a08fe
@@ -76,7 +76,8 @@
|
||||
- [Welcome](welcome-day-2.md)
|
||||
- [Pattern Matching](pattern-matching.md)
|
||||
- [Matching Values](pattern-matching/match.md)
|
||||
- [Destructuring](pattern-matching/destructuring.md)
|
||||
- [Destructuring Structs](pattern-matching/destructuring-structs.md)
|
||||
- [Destructuring Enums](pattern-matching/destructuring-enums.md)
|
||||
- [Let Control Flow](pattern-matching/let-control-flow.md)
|
||||
- [Exercise: Expression Evaluation](pattern-matching/exercise.md)
|
||||
- [Solution](pattern-matching/solution.md)
|
||||
|
||||
@@ -1,18 +1,10 @@
|
||||
---
|
||||
minutes: 8
|
||||
minutes: 4
|
||||
---
|
||||
|
||||
# Destructuring
|
||||
# Enums
|
||||
|
||||
Like tuples, structs and enums can also be destructured by matching:
|
||||
|
||||
## Structs
|
||||
|
||||
```rust,editable
|
||||
{{#include ../../third_party/rust-by-example/destructuring-structs.rs}}
|
||||
```
|
||||
|
||||
## Enums
|
||||
Like tuples, enums can also be destructured by matching:
|
||||
|
||||
Patterns can also be used to bind variables to parts of your values. This is how
|
||||
you inspect the structure of your types. Let us start with a simple `enum` type:
|
||||
@@ -46,18 +38,6 @@ arm, `half` is bound to the value inside the `Ok` variant. In the second arm,
|
||||
|
||||
<details>
|
||||
|
||||
# Structs
|
||||
|
||||
- Change the literal values in `foo` to match with the other patterns.
|
||||
- Add a new field to `Foo` and make changes to the pattern as needed.
|
||||
- The distinction between a capture and a constant expression can be hard to
|
||||
spot. Try changing the `2` in the second arm to a variable, and see that it
|
||||
subtly doesn't work. Change it to a `const` and see it working again.
|
||||
|
||||
# Enums
|
||||
|
||||
Key points:
|
||||
|
||||
- The `if`/`else` expression is returning an enum that is later unpacked with a
|
||||
`match`.
|
||||
- You can try adding a third variant to the enum definition and displaying the
|
||||
21
src/pattern-matching/destructuring-structs.md
Normal file
21
src/pattern-matching/destructuring-structs.md
Normal file
@@ -0,0 +1,21 @@
|
||||
---
|
||||
minutes: 4
|
||||
---
|
||||
|
||||
# Structs
|
||||
|
||||
Like tuples, Struct can also be destructured by matching:
|
||||
|
||||
```rust,editable
|
||||
{{#include ../../third_party/rust-by-example/destructuring-structs.rs}}
|
||||
```
|
||||
|
||||
<details>
|
||||
|
||||
- Change the literal values in `foo` to match with the other patterns.
|
||||
- Add a new field to `Foo` and make changes to the pattern as needed.
|
||||
- The distinction between a capture and a constant expression can be hard to
|
||||
spot. Try changing the `2` in the second arm to a variable, and see that it
|
||||
subtly doesn't work. Change it to a `const` and see it working again.
|
||||
|
||||
</details>
|
||||
Reference in New Issue
Block a user