Skip to content
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

Document irrefutable_let_patterns #515

Merged
merged 2 commits into from
Jan 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions src/expressions/if-expr.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,10 @@ assert_eq!(y, "Bigger");

An `if let` expression is semantically similar to an `if` expression but in
place of a condition expression it expects the keyword `let` followed by a
refutable pattern, an `=` and an expression. If the value of the expression on
the right hand side of the `=` matches the pattern, the corresponding block
will execute, otherwise flow proceeds to the following `else` block if it
exists. Like `if` expressions, `if let` expressions have a value determined by
the block that is evaluated.
pattern, an `=` and a [scrutinee] expression. If the value of the scrutinee
matches the pattern, the corresponding block will execute. Otherwise, flow
proceeds to the following `else` block if it exists. Like `if` expressions,
`if let` expressions have a value determined by the block that is evaluated.

```rust
let dish = ("Ham", "Eggs");
Expand All @@ -74,6 +73,10 @@ if let ("Bacon", b) = dish {
if let ("Ham", b) = dish {
println!("Ham is served with {}", b);
}

if let _ = 5 {
println!("Irrefutable patterns are always true");
}
```

`if` and `if let` expressions can be intermixed:
Expand Down Expand Up @@ -136,3 +139,4 @@ if let PAT = ( EXPR || EXPR ) { .. }
[_Pattern_]: patterns.html
[_LazyBooleanOperatorExpression_]: expressions/operator-expr.html#lazy-boolean-operators
[_eRFCIfLetChain_]: https://github.com/rust-lang/rfcs/blob/master/text/2497-if-let-chains.md#rollout-plan-and-transitioning-to-rust-2018
[scrutinee]: glossary.html#scrutinee
17 changes: 11 additions & 6 deletions src/expressions/loop-expr.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Rust supports four loop expressions:

* A [`loop` expression](#infinite-loops) denotes an infinite loop.
* A [`while` expression](#predicate-loops) loops until a predicate is false.
* A [`while let` expression](#predicate-pattern-loops) tests a refutable pattern.
* A [`while let` expression](#predicate-pattern-loops) tests a pattern.
* A [`for` expression](#iterator-loops) extracts values from an iterator,
looping until the iterator is empty.

Expand Down Expand Up @@ -71,18 +71,23 @@ while i < 10 {
> [_BlockExpression_]

A `while let` loop is semantically similar to a `while` loop but in place of a
condition expression it expects the keyword `let` followed by a refutable
pattern, an `=`, a [scrutinee] expression and a block expression. If the value of
the expression on the right hand side of the `=` matches the pattern, the loop
body block executes then control returns to the pattern matching statement.
Otherwise, the while expression completes.
condition expression it expects the keyword `let` followed by a pattern, an
`=`, a [scrutinee] expression and a block expression. If the value of the
scrutinee matches the pattern, the loop body block executes then control
returns to the pattern matching statement. Otherwise, the while expression
completes.

```rust
let mut x = vec![1, 2, 3];

while let Some(y) = x.pop() {
println!("y = {}", y);
}

while let _ = 5 {
println!("Irrefutable patterns are always true");
break;
}
```

A `while let` loop is equivalent to a `loop` expression containing a `match`
Expand Down