-
Notifications
You must be signed in to change notification settings - Fork 533
Patterns chapter #419
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
Patterns chapter #419
Conversation
src/patterns.md
Outdated
> _IdentifierPattern_ :\ | ||
> `ref`<sup>?</sup> `mut`<sup>?</sup> [IDENTIFIER] (`@` [_Pattern_] ) <sup>?</sup> | ||
|
||
_Identifier patterns_ bind the value they match to a **previously undeclared** variable. |
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.
The phrase "previously undeclared" here is confusing to me. I'm not certain if the original intent is to emphasize that the identifier must be unique within a pattern (i.e. (x, x)
is an invalid pattern), or if it is trying to express something else. The identifier can shadow bindings of the same name from outside the pattern, so that's why I'm confused by the "previously undeclared" part. What do you think of changing it to something like this?
Identifier patterns bind the value they match to a variable. The identifier must be unique within the pattern. The variable will shadow any variables of the same name in scope.
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.
SGTM. I would also add that the scope of the variable is dependent upon the context of where the pattern is used.
src/expressions/match-expr.md
Outdated
`match` expression has a *head expression*, which is the value to compare to | ||
the patterns. The type of the patterns must equal the type of the head | ||
expression. | ||
occurs depends on the pattern. See [Patterns] for more details. A `match` |
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 don't know if we want to explicitly nudge people to the chapter like that, or just make "pattern" in the previous sentence.
src/expressions/match-expr.md
Outdated
expression. | ||
occurs depends on the pattern. See [Patterns] for more details. A `match` | ||
expression has a *head expression*, which is the value to compare to the | ||
patterns. The type of the patterns must equal the type of the head expression. |
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 last sentence is okay, but I feel it would read better as "The head expression and the patterns must have the same type."
src/expressions/match-expr.md
Outdated
@@ -65,62 +62,9 @@ match x { | |||
Patterns that bind variables default to binding to a copy or move of the | |||
matched value (depending on the matched value's type). This can be changed to | |||
bind to a reference by using the `ref` keyword, or to a mutable reference using | |||
`ref mut`. | |||
`ref mut`. See [Identifier 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.
Make the keywords links to that section instead of adding a "See also" line. I would also change "using" to "prepending", but that doesn't have to be done in this PR, although it would be nice.
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 entire section seems duped from the Identifier Patterns" 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.
Yea, the paragraph is mostly a dupe, so I rewrote it to say something relevant.
src/patterns.md
Outdated
> | [_SlicePattern_]\ | ||
> | [_PathPattern_] | ||
|
||
Patterns in Rust are used to match values against structures and to, |
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.
"in Rust" is redundant unless contrasting with another language, which this is not doing.
src/patterns.md
Outdated
|
||
Patterns in Rust are used to match values against structures and to, | ||
optionally, bind variables to values inside these structures. They are also | ||
used in variable declarations and function/closure parameters, though in these |
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.
General style right now is to not use x/y
. Could rewrite as "parameters for functions and closures".
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.
The clause after the comma could be its own sentence, or maybe even removed. If not removed, "though in these cases most of the time" -> "though in most of these cases,".
src/patterns.md
Outdated
* [`if let` expressions](expressions/if-expr.html) | ||
* [`while let` expressions](expressions/loop-expr.html#predicate-pattern-loops) | ||
* [`for` expressions](expressions/loop-expr.html#iterator-loops) | ||
* Inside other 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.
This is a weird way of saying that patterns are recursive. I wouldn't really call a subpattern a pattern on its own right personally.
src/patterns.md
Outdated
|
||
## Destructuring | ||
|
||
Patterns can be used to *destructure* structs, enums, and tuples. 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.
Not required, but would be nice to make "structs", "enums", and "tuples" be links.
src/patterns.md
Outdated
## Destructuring | ||
|
||
Patterns can be used to *destructure* structs, enums, and tuples. Destructuring | ||
breaks a value up into its component pieces. The syntax used is almost the same as |
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.
"breaks up a value"?
src/patterns.md
Outdated
|
||
Patterns can be used to *destructure* structs, enums, and tuples. Destructuring | ||
breaks a value up into its component pieces. The syntax used is almost the same as | ||
when creating such values. When destructing a data structure with named (but |
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.
"destructing" -> "destructuring"
src/patterns.md
Outdated
breaks a value up into its component pieces. The syntax used is almost the same as | ||
when creating such values. When destructing a data structure with named (but | ||
not numbered) fields, it is allowed to write `fieldname` as a shorthand for | ||
`fieldname: fieldname`. In a pattern whose head expression has a `struct`, |
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 feel the sentence about _
and ..
should precede the one about field (de)structuring shorthand. Also "stands" -> "stands in"
src/patterns.md
Outdated
|
||
## Refutability | ||
|
||
A pattern is said to be *Refutable* when it **has the possibility of not being matched** |
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.
"Refutable" -> "refutable" (casing). I'm iffy about bolding the definition part. especially since the irrefutable definition part isn't bolded. If you want to propose changing our style to actually bold them like that, file an issue.
src/patterns.md
Outdated
not literals in Rust, literal patterns also accept an optional minus sign before the | ||
literal. | ||
|
||
Floating-point literals are currently accepted, but due to the complexity of comparing |
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 generally don't talk about the future of Rust, but this seems a worthwhile exception. But it should be placed in a warning box (you can find the syntax for it in introduction.md)
src/patterns.md
Outdated
[FLOAT_LITERAL]: tokens.html#floating-point-literals | ||
|
||
_Literal patterns_ match exactly the value they represent. Since negative numbers are | ||
not literals in Rust, literal patterns also accept an optional minus sign before 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.
- Link "literals" please.
- Remove "in Rust".
- State that they match the same value as what is created by the literal.
- State that the minus sign acts like the negation operator expression.
src/patterns.md
Outdated
``` | ||
|
||
match any value and bind it to that identifier. This is the most commonly | ||
used pattern in variable declarations and function/closure parameters. |
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.
Same "x/y" as earlier.
src/patterns.md
Outdated
binds to `e` the value 2 (not the entire range: the range here is a range subpattern). | ||
|
||
By default, identifier patterns bind a variable to a copy of or move from the | ||
matched value (depending whether the matched value implements the [Copy trait]). |
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.
- The parenthesis are superfluous. The words inside are crucial to the meaning of the sentence.
- "depending" -> "depending on".
the [Copy trait]
->[`Copy`]
src/tokens.md
Outdated
@@ -611,3 +611,8 @@ them are referred to as "token trees" in [macros]. The three types of brackets | |||
[extern]: items/external-blocks.html | |||
[struct expressions]: expressions/struct-expr.html | |||
[tuple index]: expressions/tuple-expr.html#tuple-indexing-expressions | |||
[Wildcard patterns]: patterns.html#wildcard-pattern |
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 you alphabetize these? You don't need to alphabetize all of the ones that are already here, but at least from extern
downwards (since the three already at the end are alphabetized by coincidence). Uppercase sorts before all lowercase.
Overall this looks good. I'm not done nitpicking it, and you don't have to listen to my nitpicks (especially the ones with "?"s attached. Just respond to the nitpick saying so if you don't. |
src/patterns.md
Outdated
} | ||
``` | ||
|
||
binds to `e` the value 2 (not the entire range: the range here is a range subpattern). |
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 should be on the same line as the "For example".
src/patterns.md
Outdated
match any value and bind it to that identifier. This is the most commonly | ||
used pattern in variable declarations and parameters for functions and closures. | ||
|
||
To bind non-trivial patterns to a variable, the use of the syntax `variable @ |
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.
The passive voice here is an inversion on what is normally written. I also don't like the word "non-trivial", but don't have a better word to replace it personally.
src/patterns.md
Outdated
if let Person{name: &person_name, age: 18..=150} = value { } | ||
``` | ||
|
||
is not valid. What we must do is: |
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.
"What we must do is:" -> "To make it valid, we can write the following."
src/patterns.md
Outdated
|
||
A struct pattern is refutable when one of its subpatterns is refutable. | ||
|
||
## TupleStruct 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.
Do we want "TupleStruct" to be one word or two?
I generally don't like smashing words together.
src/patterns.md
Outdated
## Tuple patterns | ||
|
||
> **<sup>Syntax</sup>**\ | ||
> _TuplePattern_ :<a name="tuple-pattern-syntax"></a>\ |
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.
"name" attribute is deprecated for "id" in HTML, IIRC.
Alright, I've finished looking over all of the changes. |
I rewrote all the situations where a sentence was split across the example code. In the very first example, I'm not sure if it is clearer if the code sample should appear before or after the text talking about it. It's fairly small, so it should fit on one screen on most devices, so it probably doesn't matter too much? |
Just need to rebase against master (conflict with |
- Added the chapter about patterns, with some explanations and the grammar. - Had to move some documentation about patterns from the `match` section. - Removed the bit about the new syntax of inclusive pattern ranges (a..=b), since it is not stable nor in the process of being stabilized.
87a63a5
to
7edf953
Compare
Rebased! |
And merged! Many thanks for taking this over and getting it finished! (Imagine twenty part hat emojis here) |
This is a repost of #284, rebased, updated, most review comments addressed, and some additional information added.
Closes #83, #300, #301.
This adds a small description of binding modes which covers #280, but I'm not confident it is detailed enough to consider it complete. There are a lot of details not covered in depth (like const references).