1
0
mirror of https://github.com/google/comprehensive-rust.git synced 2025-11-30 17:15:23 +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:
Manichand Kondapaka
2024-04-30 19:37:10 +05:30
committed by GitHub
parent 9a66fb1451
commit 48325a08fe
4 changed files with 27 additions and 26 deletions

View File

@@ -207,8 +207,7 @@ use-boolean-and = true
"ownership/moves-function-calls.html" = "../memory-management/move.html" "ownership/moves-function-calls.html" = "../memory-management/move.html"
"ownership/shared-unique-borrows.html" = "../borrowing/shared.html" "ownership/shared-unique-borrows.html" = "../borrowing/shared.html"
"pattern-matching/destructuring-arrays.html" = "../tuples-and-arrays/destructuring.html" "pattern-matching/destructuring-arrays.html" = "../tuples-and-arrays/destructuring.html"
"pattern-matching/destructuring-enums.html" = "../pattern-matching/destructuring.html" "pattern-matching/destructuring.html" = "destructuring-structs.html"
"pattern-matching/destructuring-structs.html" = "../pattern-matching/destructuring.html"
"pattern-matching/match-guards.html" = "../tuples-and-arrays/match.html" "pattern-matching/match-guards.html" = "../tuples-and-arrays/match.html"
"running-the-course/day-4.html" = "course-structure.html" "running-the-course/day-4.html" = "course-structure.html"
"sintaxe-básica/funções-interlude.html" = "../basic-syntax/functions-interlude.html" "sintaxe-básica/funções-interlude.html" = "../basic-syntax/functions-interlude.html"

View File

@@ -76,7 +76,8 @@
- [Welcome](welcome-day-2.md) - [Welcome](welcome-day-2.md)
- [Pattern Matching](pattern-matching.md) - [Pattern Matching](pattern-matching.md)
- [Matching Values](pattern-matching/match.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) - [Let Control Flow](pattern-matching/let-control-flow.md)
- [Exercise: Expression Evaluation](pattern-matching/exercise.md) - [Exercise: Expression Evaluation](pattern-matching/exercise.md)
- [Solution](pattern-matching/solution.md) - [Solution](pattern-matching/solution.md)

View File

@@ -1,18 +1,10 @@
--- ---
minutes: 8 minutes: 4
--- ---
# Destructuring # Enums
Like tuples, structs and enums can also be destructured by matching: Like tuples, enums can also be destructured by matching:
## Structs
```rust,editable
{{#include ../../third_party/rust-by-example/destructuring-structs.rs}}
```
## Enums
Patterns can also be used to bind variables to parts of your values. This is how 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: 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> <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 - The `if`/`else` expression is returning an enum that is later unpacked with a
`match`. `match`.
- You can try adding a third variant to the enum definition and displaying the - You can try adding a third variant to the enum definition and displaying the

View 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>