Skip to content

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

Closed
wants to merge 3 commits into from
Closed

Conversation

brauliobz
Copy link
Contributor

@brauliobz brauliobz commented Mar 29, 2018

  • Added the chapter about patterns, with some explanations and the grammar.
  • Some sections are lacking good explanations and examples.
  • Moved some documentation about patterns from the 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.
  • Kept the slice patterns, since it is being stabilized.

Closes #83, closes #300, #301.

- 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.
Copy link
Contributor

@matthewjasper matthewjasper left a 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)
Copy link
Contributor

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,
Copy link
Contributor

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",
});

Copy link
Contributor

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.
Copy link
Contributor

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.

> &nbsp;&nbsp; | _QualifiedPathForExpression_

_Path patterns_ are patterns that refer either to constant values or
to structs or enum variants that have no fields.
Copy link
Contributor

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_ :
> &nbsp;&nbsp; &nbsp;&nbsp; `mut`<sup>?</sup> IDENTIFIER (`@` [_Pattern_] ) <sup>?</sup>
> &nbsp;&nbsp; | `ref` `mut`<sup>?</sup> IDENTIFIER (`@` [_Pattern_] ) <sup>?</sup>
Copy link
Contributor

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,
Copy link
Contributor

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
Copy link
Contributor

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_ :
> &nbsp;&nbsp; `_`

The _wildcard pattern_ matches any value. It is used to ignore values when they don't
Copy link
Contributor

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)
Copy link
Contributor

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`.
Copy link
Contributor

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.

@alercah
Copy link
Contributor

alercah commented Apr 3, 2018

This also fixes #300.

@alercah
Copy link
Contributor

alercah commented Apr 3, 2018

Also #301.

@Havvy
Copy link
Contributor

Havvy commented Jul 30, 2018

@brauliobz Are you still working on this?

@ehuss
Copy link
Contributor

ehuss commented Sep 14, 2018

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.

@ehuss ehuss mentioned this pull request Sep 16, 2018
@Havvy
Copy link
Contributor

Havvy commented Sep 20, 2018

Closing in favor of #419

@Havvy Havvy closed this Sep 20, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Document RFC 1492 (dotdot-in-patterns) Patterns aren't documented
5 participants