Skip to content

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

Merged
merged 10 commits into from
Sep 21, 2018
Merged

Patterns chapter #419

merged 10 commits into from
Sep 21, 2018

Conversation

ehuss
Copy link
Contributor

@ehuss ehuss commented Sep 16, 2018

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).

src/patterns.md Outdated
> _IdentifierPattern_ :\
> &nbsp;&nbsp; &nbsp;&nbsp; `ref`<sup>?</sup> `mut`<sup>?</sup> [IDENTIFIER] (`@` [_Pattern_] ) <sup>?</sup>

_Identifier patterns_ bind the value they match to a **previously undeclared** variable.
Copy link
Contributor Author

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.

Copy link
Contributor

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.

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

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.

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

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."

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

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.

Copy link
Contributor

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?

Copy link
Contributor Author

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
> &nbsp;&nbsp; | [_SlicePattern_]\
> &nbsp;&nbsp; | [_PathPattern_]

Patterns in Rust are used to match values against structures and to,
Copy link
Contributor

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

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".

Copy link
Contributor

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

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

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

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

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

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

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

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

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

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

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
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 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.

@Havvy
Copy link
Contributor

Havvy commented Sep 17, 2018

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

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

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

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

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

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.

@Havvy
Copy link
Contributor

Havvy commented Sep 20, 2018

Alright, I've finished looking over all of the changes.

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

ehuss commented Sep 21, 2018

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?

@Havvy
Copy link
Contributor

Havvy commented Sep 21, 2018

Just need to rebase against master (conflict with tokens.md) and this should be good to merge.

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

ehuss commented Sep 21, 2018

Rebased!

@Havvy Havvy merged commit 5e296d2 into rust-lang:master Sep 21, 2018
@Havvy
Copy link
Contributor

Havvy commented Sep 21, 2018

And merged! Many thanks for taking this over and getting it finished! (Imagine twenty part hat emojis here)

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.

3 participants