mirror of
https://github.com/google/comprehensive-rust.git
synced 2025-01-14 02:53:01 +02:00
Updates and minor fixes to Day 2: Morning (#372)
This commit is contained in:
parent
e3b4b6a5c7
commit
36ce63cb10
@ -9,8 +9,10 @@ You can define richer enums where the variants carry data. You can then use the
|
||||
|
||||
<details>
|
||||
|
||||
* In the above example, accessing the `char` in `KeyPress`, or `x` and `y` in `Click` only works within a `match` statement.
|
||||
* `match` inspects a hidden discriminant field in the `enum`.
|
||||
* In the above example, accessing the `char` in `KeyPress`, or `x` and `y` in `Click` only works within a `match` or an `if let` statement.
|
||||
* `match` and `if let` inspect a hidden discriminant field in the `enum`.
|
||||
* It is possible to retrieve the discriminant by calling `std::mem::discriminant()`
|
||||
* This is useful, for example, if implementing `PartialEq` for structs where comparing field values doesn't affect equality.
|
||||
* `WebEvent::Click { ... }` is not exactly the same as `WebEvent::Click(Click)` with a top level `struct Click { ... }`. The inlined version cannot implement traits, for example.
|
||||
|
||||
</details>
|
||||
|
@ -33,8 +33,8 @@ Key Points:
|
||||
* Developers may choose to use methods to take advantage of method receiver syntax and to help keep them more organized. By using methods we can keep all the implementation code in one predictable place.
|
||||
* Point out the use of the keyword `self`, a method receiver.
|
||||
* Show that it is an abbreviated term for `self:&Self` and perhaps show how the struct name could also be used.
|
||||
* Explain that Self is a type alias for the type the `impl` block is in and can be used elsewhere in the block.
|
||||
* Note how self is used like other structs and dot notation can be used to refer to individual fields.
|
||||
* Explain that `Self` is a type alias for the type the `impl` block is in and can be used elsewhere in the block.
|
||||
* Note how `self` is used like other structs and dot notation can be used to refer to individual fields.
|
||||
* This might be a good time to demonstrate how the `&self` differs from `self` by modifying the code and trying to run say_hello twice.
|
||||
* We describe the distinction between method receivers next.
|
||||
|
||||
|
@ -10,7 +10,7 @@ expression which will be executed if the pattern matches:
|
||||
<details>
|
||||
|
||||
Key Points:
|
||||
* Match guards as a separate syntax feature are important and necessary.
|
||||
* Match guards as a separate syntax feature are important and necessary when we wish to concisely express more complex ideas than patterns alone would allow.
|
||||
* They are not the same as separate `if` expression inside of the match arm. An `if` expression inside of the branch block (after `=>`) happens after the match arm is selected. Failing the `if` condition inside of that block won't result in other arms
|
||||
of the original `match` expression being considered.
|
||||
* You can use the variables defined in the pattern in your if expression.
|
||||
|
@ -35,6 +35,7 @@ Key Points:
|
||||
* Methods are defined in an `impl` block, which we will see in following slides.
|
||||
* This may be a good time to let people know there are different types of structs.
|
||||
* Zero-sized structs `e.g., struct Foo;` might be used when implementing a trait on some type but don’t have any data that you want to store in the value itself.
|
||||
* The next slide will introduce Tuple structs.
|
||||
* The next slide will introduce Tuple structs, used when the field names are not important.
|
||||
* The syntax `..peter` allows us to copy the majority of the fields from the old struct without having to explicitly type it all out. It must always be the last element.
|
||||
|
||||
</details>
|
||||
|
@ -8,7 +8,7 @@ fn main() {
|
||||
let foo = Foo { x: (1, 2), y: 3 };
|
||||
match foo {
|
||||
Foo { x: (1, b), y } => println!("x.0 = 1, b = {b}, y = {y}"),
|
||||
Foo { y: 2, x: i } => println!("y = 2, i = {i:?}"),
|
||||
Foo { y: 2, x: i } => println!("y = 2, x = {i:?}"),
|
||||
Foo { y, .. } => println!("y = {y}, other fields were ignored"),
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user