-
Notifications
You must be signed in to change notification settings - Fork 3.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Chapter 18: Patterns #469
Chapter 18: Patterns #469
Conversation
src/ch18-00-patterns.md
Outdated
println!("The value of y is: {}", y); | ||
} | ||
Here, we have a tuple that we're matching against a pattern. Rust will compare | ||
the value `(1, 2, 3)` to the pattern `(x, y, z)`, and see that it's valid. In |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just realised you do this in the concurrency chapter when creating a channel. We may want to mention this chapter there, depending on how that chapter shakes out.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good eyes!
src/ch18-00-patterns.md
Outdated
the most commonly used ones in this section. Any of these abilities work in any | ||
place where a pattern is used. | ||
One of Rust's more subtle features is the simple *pattern*. Patterns let us | ||
pick apart complex strucutres and do all kinds of fun things with them. In this |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
strucutres => structures
src/ch18-00-patterns.md
Outdated
And run it with `cargo run`: | ||
Where we nest tons of things inside of each other. | ||
|
||
## Refutability |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we move this section lower? It feels like I want more hands-on with working with patterns before we get into the peculiarities of them.
src/ch18-00-patterns.md
Outdated
familliar with the word when you see it, and realize that you need to change | ||
either the pattern, or the construct you're using the pattern with. | ||
|
||
## Where can I use patterns? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also if let
src/ch18-00-patterns.md
Outdated
In simple cases like this, two `let`s may be clearer, but in others, creating | ||
multiple variables at once is nice. As we become more proficient in Rust, we’ll | ||
figure out which style is better, but it’s mostly a judgment call. | ||
These patterns are refuatble. However... |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
refuatble => refutable
src/ch18-00-patterns.md
Outdated
In simple cases like this, two `let`s may be clearer, but in others, creating | ||
multiple variables at once is nice. As we become more proficient in Rust, we’ll | ||
figure out which style is better, but it’s mostly a judgment call. | ||
These patterns are refuatble. However... |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm wondering if we could say let
and match
must cover all cases that the value can be. We can introduce the terms for this later, as I think they're less interesting than the concept of patterns matching values
src/ch18-00-patterns.md
Outdated
## Literals & _ | ||
Here's a list of all of the different types of patterns. | ||
|
||
### Literals & _ | ||
|
||
You can match against literals directly, and `_` acts as an any case: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is rendering for me as �any�
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
your comment is rendering for me as �any�
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think i found all of these?
src/ch18-00-patterns.md
Outdated
## Literals & _ | ||
Here's a list of all of the different types of patterns. | ||
|
||
### Literals & _ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We may want to say "the underscore (_) pattern"
src/ch18-00-patterns.md
Outdated
@@ -178,7 +280,7 @@ match name { | |||
println!("name is: {:?}", name); | |||
``` | |||
|
|||
## Destructuring | |||
### Destructuring |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Destructuring mentions structs and enums, but only shows structs.
Since you're quite often going to use pattern matching against enums, we probably want to cover them again here.
src/ch18-00-patterns.md
Outdated
``` | ||
|
||
In this case, we want to compare `id` against the range `3...7`, but we also | ||
want to save the actual value of `id`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we want a summary?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yep
This one feels like a reference rather than the narrative style of the rest of the book. Perhaps that's mostly okay, but I think we should at least chat about whether or not we want the narrative style here as well. |
@steveklabnik @jonathandturner I rearranged this a bunch, better or worse? I like this chapter as a reference; trying to think of an example or use case that could weave through this whole chapter seems hard and like we'd end up with a gargantuan @steveklabnik some of the TODOs in here are places that I think we could use some more text, could you take care of those please, or let me know if you'd rather I do it? |
The problem here is that almost all uses of |
Sure, that works too.
Only if the value itself is a literal, that is, |
I've pushed up some TODO fixes; @carols10cents if you get the chance, feel free to wrap this up; if not, I'll try to take another look soon. |
Fixes #468
c8f4aae
to
cebc14e
Compare
I'm so confused right now. In my research for this chapter, I found this tracking issue (still open but partially complete) and this merged RFC which seem to imply that attempting to use floating point literals in However, this code does not give me any warnings and seems to work happily. It's only if I put the floating point in a constant that I can trigger the error mentioned. Why are floating point literals allowed but not floating point constants? From the discussion on the RFC and linked threads, it's floating point numbers only implementing Did I find a bug or am I misunderstanding something...? |
OK, I'm ignoring whether or not you're supposed to be allowed to match floating point literals, and I think this is ready for review. @steveklabnik @jonathandturner @ScottAbbey @matthewjasper ? ❤️ Anyone else? ❤️ |
fail to match for some possible value are said to be *refutable*. `let` | ||
statements, function parameters, and `for` loops are restricted to only accept | ||
irrefutable patterns, since there's nothing correct the program could do if the | ||
pattern fails to match. `match`, `if let`, and `while let` expressions are |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not match
. As this section says later: 'the last arm that might match any remaining values with an irrefutable pattern'.
`Some(y)` introduces a new variable name `y` that will match any value inside a | ||
`Some` value. Because we're in a new scope inside the `match` expression, this | ||
is a new variable, not the `y` we declared at the beginning that has the value | ||
10. The new `y` binding will match any value inside a `Some`, which is what we |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sigh... 'value' needs to be moved down to this line.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
aaaiiiieeee
|
||
### Multiple patterns | ||
|
||
You can match multiple patterns with `|`, which means *or*: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is for match
only. Perhaps that could be clearer.
|
||
### Matching Ranges of Values with `...` | ||
|
||
You can match a range of values with `...`: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(inclusive)
|
||
match p { | ||
Point { x, y: 0 } => println!("On the x axis at {}", x), | ||
Point { x: 0, y} => println!("On the y axis at {}", y), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
space between y
and }
?
# } | ||
# | ||
let points = vec![ | ||
Point { x: 0, y: 0}, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Space before }
(and following two lines)?
let _s = s; | ||
|
||
println!("{}", s); | ||
``` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would this and the next example be more helpful if they showed this in a match
/if let
, like 18-16.
|
||
This works just fine. Because we never bind `s` to anything, it's not moved. | ||
|
||
### Ignoring Remaining Parts of a Value with `..` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should have 4 '#'?
`robot_name` is moved within the `match` when `name` binds to that value. | ||
|
||
Using `&` in a pattern matches an existing reference in the value, as we saw in | ||
the previous section on destructuring. If you want to create a reference |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
'a previous section'?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I used "the" to refer to the specific section; I'm changing this to have the section heading to be clearer which section we're referring to 👍
<span class="caption">Listing 18-21: Ignoring all fields of a `Point` except | ||
for `x` by using `..`</span> | ||
|
||
Using `..` is shorter to type than having to list out `_y` and `_z`. The `..` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
y: _
and z: _
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lol idk why I thought i could combine the shorthand struct syntax and underscores....
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💯 some very small typo things
|
||
The first call to `enumerate` produces the tuple `(0, 1)`. When this value is | ||
matched to the pattern `(index, value)`, `index` will be 0 and `value` will | ||
equal 1. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
s/equal/be/, that is, changing the verb in the middle of the sentence feels weird to me (so we could also change 'be' to 'equal' on the line above and i'd be fine either way
let x = 5; | ||
``` | ||
|
||
We've done this hundreds of times throughout this book. You may not have |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
don't we know it
|
||
With `let`, we compare the expression against the pattern, and assign any names | ||
we find. So for example, in our `let x = 5;` case, `x` is a pattern that says | ||
"bind what matches here to the variable `x`. And since the name `x` is the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
need a "
after the .
## Refutability: Whether a Pattern Might Fail to Match | ||
|
||
Patterns come in two forms: refutable and irrefutable. Patterns which cannot | ||
fail to match for any possible value are *irrefutable*, and patterns which can |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"are said to be" so this matches up with the latter part of the sentence
println!("Found some other id: {}", id) | ||
}, | ||
} | ||
``` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 😍 zomg i have not found an example this good in years of trying, every @
example I've seen has been to complex
I'm surprised there is no explanation about the Even other books use it, see here Am I missing something here ? It deserves an entire page IMO If someone adds an explanation in chapter 18, it would be usefull to add a link from "deref" or pointers more broadly to this explanation |
Fixes #468 (I ended up using the last example, we should give credit)
Fixes #298 by discussing destructuring,
ref
, and more complex patterns.