diff --git a/book.toml b/book.toml index e75c6ee5..ae242a64 100644 --- a/book.toml +++ b/book.toml @@ -207,8 +207,7 @@ use-boolean-and = true "ownership/moves-function-calls.html" = "../memory-management/move.html" "ownership/shared-unique-borrows.html" = "../borrowing/shared.html" "pattern-matching/destructuring-arrays.html" = "../tuples-and-arrays/destructuring.html" -"pattern-matching/destructuring-enums.html" = "../pattern-matching/destructuring.html" -"pattern-matching/destructuring-structs.html" = "../pattern-matching/destructuring.html" +"pattern-matching/destructuring.html" = "destructuring-structs.html" "pattern-matching/match-guards.html" = "../tuples-and-arrays/match.html" "running-the-course/day-4.html" = "course-structure.html" "sintaxe-básica/funções-interlude.html" = "../basic-syntax/functions-interlude.html" diff --git a/src/SUMMARY.md b/src/SUMMARY.md index 42d9cfac..1e4be22f 100644 --- a/src/SUMMARY.md +++ b/src/SUMMARY.md @@ -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) diff --git a/src/pattern-matching/destructuring.md b/src/pattern-matching/destructuring-enums.md similarity index 75% rename from src/pattern-matching/destructuring.md rename to src/pattern-matching/destructuring-enums.md index 228959ff..cb82b006 100644 --- a/src/pattern-matching/destructuring.md +++ b/src/pattern-matching/destructuring-enums.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,
-# 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 diff --git a/src/pattern-matching/destructuring-structs.md b/src/pattern-matching/destructuring-structs.md new file mode 100644 index 00000000..982dd83d --- /dev/null +++ b/src/pattern-matching/destructuring-structs.md @@ -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}} +``` + +
+ +- 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. + +