-
Notifications
You must be signed in to change notification settings - Fork 535
WIP: Patterns chapter #284
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
Conversation
- 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.
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.
Thanks so much for this! It looks great. I've left some comments from going through this.
Match default binding modes (#280, rust-lang/rust#42640) are stable on nightly now, but I'm fine with merging this without them.
parameters | ||
* [`match` expressions](expressions.html#match-expressions) | ||
* [`if let` expressions](expressions.html#if-let-expressions) | ||
* [`while let` expressions](expressions.html#while-let-loops) |
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.
and for
expressions
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`, | ||
`enum` or `tupl` type, a placeholder (`_`) stands for a *single* data field, |
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.
tuple
0 ..= <u32 as MaxValue>::MAX => "fits in a u32", | ||
_ => "too big", | ||
}); | ||
|
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.
Extra new line
Qualified path patterns can only refer to associated constants. | ||
|
||
Path patterns are irrefutable when they refer to constants or structs. | ||
They are refutable when the refer to enum variants. |
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.
They're irrefutable if the enum only has one variant.
> | _QualifiedPathForExpression_ | ||
|
||
_Path patterns_ are patterns that refer either to constant values or | ||
to structs or enum variants that have no fields. |
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.
unit structs and unit variants, struct A {}
doesn't allow A
as a pattern.
> **<sup>Syntax</sup>** | ||
> _IdentifierPattern_ : | ||
> `mut`<sup>?</sup> IDENTIFIER (`@` [_Pattern_] ) <sup>?</sup> | ||
> | `ref` `mut`<sup>?</sup> IDENTIFIER (`@` [_Pattern_] ) <sup>?</sup> |
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.
Why isn't this `ref` ? `mut` ? ...
?
} | ||
``` | ||
|
||
in the first match expression, the value is copied (or moved). In the second 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.
'In'. I think that code blocks count as the end of a sentence.
|
||
Reference patterns are always irrefutable. | ||
|
||
## 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.
Can this section be moved up to before Wildcard pattern?
> _WildcardPattern_ : | ||
> `_` | ||
|
||
The _wildcard pattern_ matches any value. It is used to ignore values when they don't |
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 add a sentence to this section saying something like "unlike identifier patterns, it does not copy, move or borrow the value it matches."
@@ -60,6 +60,8 @@ | |||
- [Match expressions](expressions/match-expr.md) | |||
- [Return expressions](expressions/return-expr.md) | |||
|
|||
- [Patterns](patterns.md) |
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.
@Havvy should this be under Statements and expressions?
_ => { /* this wildcard is required, since we don't know length statically */ } | ||
} | ||
``` | ||
A range pattern may not be a sub-range of another range pattern inside the same `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.
This doesn't seem to be true; https://play.rust-lang.org/?gist=49813df7725fc36a71959a136841ce14&version=nightly shows only a warning.
This also fixes #300. |
Also #301. |
@brauliobz Are you still working on this? |
I contacted Bráulio and he said that he hasn't been able to work on this lately. If nobody minds, I'm going to try to finish it off. |
Closing in favor of #419 |
match
expression 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.
Closes #83, closes #300, #301.