Skip to content

Commit d7b8512

Browse files
committed
Copyedit section 4 of tutorial
1 parent 6627ac6 commit d7b8512

File tree

1 file changed

+49
-42
lines changed

1 file changed

+49
-42
lines changed

doc/tutorial.md

Lines changed: 49 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -545,8 +545,8 @@ You can define your own syntax extensions with the macro system. For details, se
545545

546546
## Conditionals
547547

548-
We've seen `if` pass by a few times already. To recap, braces are
549-
compulsory, an optional `else` clause can be appended, and multiple
548+
We've seen `if` expressions a few times already. To recap, braces are
549+
compulsory, an `if` can have an optional `else` clause, and multiple
550550
`if`/`else` constructs can be chained together:
551551

552552
~~~~
@@ -559,10 +559,10 @@ if false {
559559
}
560560
~~~~
561561

562-
The condition given to an `if` construct *must* be of type boolean (no
563-
implicit conversion happens). If the arms return a value, this value
564-
must be of the same type for every arm in which control reaches the
565-
end of the block:
562+
The condition given to an `if` construct *must* be of type `bool` (no
563+
implicit conversion happens). If the arms are blocks that have a
564+
value, this value must be of the same type for every arm in which
565+
control reaches the end of the block:
566566

567567
~~~~
568568
fn signum(x: int) -> int {
@@ -575,9 +575,10 @@ fn signum(x: int) -> int {
575575
## Pattern matching
576576

577577
Rust's `match` construct is a generalized, cleaned-up version of C's
578-
`switch` construct. You provide it with a value and a number of *arms*,
579-
each labelled with a pattern, and the code will attempt to match each pattern
580-
in order. For the first one that matches, the arm is executed.
578+
`switch` construct. You provide it with a value and a number of
579+
*arms*, each labelled with a pattern, and the code compares the value
580+
against each pattern in order until one matches. The matching pattern
581+
executes its corresponding arm.
581582

582583
~~~~
583584
# let my_number = 1;
@@ -589,15 +590,19 @@ match my_number {
589590
}
590591
~~~~
591592

592-
There is no 'falling through' between arms, as in C—only one arm is
593-
executed, and it doesn't have to explicitly `break` out of the
593+
Unlike in C, there is no 'falling through' between arms: only one arm
594+
executes, and it doesn't have to explicitly `break` out of the
594595
construct when it is finished.
595596

596-
The part to the left of the arrow `=>` is called the *pattern*. Literals are
597-
valid patterns and will match only their own value. The pipe operator
598-
(`|`) can be used to assign multiple patterns to a single arm. Ranges
599-
of numeric literal patterns can be expressed with two dots, as in `M..N`. The
600-
underscore (`_`) is a wildcard pattern that matches everything.
597+
A `match` arm consists of a *pattern*, then an arrow `=>`, followed by
598+
an *action* (expression). Literals are valid patterns and match only
599+
their own value. A single arm may match multiple different patterns by
600+
combining them with the pipe operator (`|`), so long as every pattern
601+
binds the same set of variables. Ranges of numeric literal patterns
602+
can be expressed with two dots, as in `M..N`. The underscore (`_`) is
603+
a wildcard pattern that matches any single value. The asterisk (`*`)
604+
is a different wildcard that can match one or more fields in an `enum`
605+
variant.
601606

602607
The patterns in an match arm are followed by a fat arrow, `=>`, then an
603608
expression to evaluate. Each case is separated by commas. It's often
@@ -612,13 +617,14 @@ match my_number {
612617
}
613618
~~~
614619

615-
`match` constructs must be *exhaustive*: they must have an arm covering every
616-
possible case. For example, if the arm with the wildcard pattern was left off
617-
in the above example, the typechecker would reject it.
620+
`match` constructs must be *exhaustive*: they must have an arm
621+
covering every possible case. For example, the typechecker would
622+
reject the previous example if the arm with the wildcard pattern was
623+
omitted.
618624

619-
A powerful application of pattern matching is *destructuring*, where
620-
you use the matching to get at the contents of data types. Remember
621-
that `(float, float)` is a tuple of two floats:
625+
A powerful application of pattern matching is *destructuring*:
626+
matching in order to bind names to the contents of data
627+
types. Remember that `(float, float)` is a tuple of two floats:
622628

623629
~~~~
624630
fn angle(vector: (float, float)) -> float {
@@ -631,37 +637,39 @@ fn angle(vector: (float, float)) -> float {
631637
}
632638
~~~~
633639

634-
A variable name in a pattern matches everything, *and* binds that name
635-
to the value of the matched thing inside of the arm block. Thus, `(0f,
640+
A variable name in a pattern matches any value, *and* binds that name
641+
to the value of the matched value inside of the arm's action. Thus, `(0f,
636642
y)` matches any tuple whose first element is zero, and binds `y` to
637643
the second element. `(x, y)` matches any tuple, and binds both
638-
elements to a variable.
644+
elements to variables.
639645

640-
Any `match` arm can have a guard clause (written `if EXPR`), which is
641-
an expression of type `bool` that determines, after the pattern is
642-
found to match, whether the arm is taken or not. The variables bound
643-
by the pattern are available in this guard expression.
646+
Any `match` arm can have a guard clause (written `if EXPR`), called a
647+
*pattern guard*, which is an expression of type `bool` that
648+
determines, after the pattern is found to match, whether the arm is
649+
taken or not. The variables bound by the pattern are in scope in this
650+
guard expression. The first arm in the `angle` example shows an
651+
example of a pattern guard.
644652

645653
You've already seen simple `let` bindings, but `let` is a little
646-
fancier than you've been led to believe. It too supports destructuring
647-
patterns. For example, you can say this to extract the fields from a
648-
tuple, introducing two variables, `a` and `b`.
654+
fancier than you've been led to believe. It, too, supports destructuring
655+
patterns. For example, you can write this to extract the fields from a
656+
tuple, introducing two variables at once: `a` and `b`.
649657

650658
~~~~
651659
# fn get_tuple_of_two_ints() -> (int, int) { (1, 1) }
652660
let (a, b) = get_tuple_of_two_ints();
653661
~~~~
654662

655-
Let bindings only work with _irrefutable_ patterns, that is, patterns
663+
Let bindings only work with _irrefutable_ patterns: that is, patterns
656664
that can never fail to match. This excludes `let` from matching
657-
literals and most enum variants.
665+
literals and most `enum` variants.
658666

659667
## Loops
660668

661-
`while` produces a loop that runs as long as its given condition
662-
(which must have type `bool`) evaluates to true. Inside a loop, the
663-
keyword `break` can be used to abort the loop, and `loop` can be used
664-
to abort the current iteration and continue with the next.
669+
`while` denotes a loop that iterates as long as its given condition
670+
(which must have type `bool`) evaluates to `true`. Inside a loop, the
671+
keyword `break` aborts the loop, and `loop` aborts the current
672+
iteration and continues with the next.
665673

666674
~~~~
667675
let mut cake_amount = 8;
@@ -670,7 +678,7 @@ while cake_amount > 0 {
670678
}
671679
~~~~
672680

673-
`loop` is the preferred way of writing `while true`:
681+
`loop` denotes an infinite loop, and is the preferred way of writing `while true`:
674682

675683
~~~~
676684
let mut x = 5;
@@ -684,9 +692,8 @@ loop {
684692
This code prints out a weird sequence of numbers and stops as soon as
685693
it finds one that can be divided by five.
686694

687-
For more involved iteration, such as going over the elements of a
688-
collection, Rust uses higher-order functions. We'll come back to those
689-
in a moment.
695+
For more involved iteration, such as enumerating the elements of a
696+
collection, Rust uses [higher-order functions](#closures).
690697

691698
# Data structures
692699

0 commit comments

Comments
 (0)