@@ -545,8 +545,8 @@ You can define your own syntax extensions with the macro system. For details, se
545
545
546
546
## Conditionals
547
547
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
550
550
` if ` /` else ` constructs can be chained together:
551
551
552
552
~~~~
@@ -559,10 +559,10 @@ if false {
559
559
}
560
560
~~~~
561
561
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:
566
566
567
567
~~~~
568
568
fn signum(x: int) -> int {
@@ -575,9 +575,10 @@ fn signum(x: int) -> int {
575
575
## Pattern matching
576
576
577
577
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.
581
582
582
583
~~~~
583
584
# let my_number = 1;
@@ -589,15 +590,19 @@ match my_number {
589
590
}
590
591
~~~~
591
592
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
594
595
construct when it is finished.
595
596
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.
601
606
602
607
The patterns in an match arm are followed by a fat arrow, ` => ` , then an
603
608
expression to evaluate. Each case is separated by commas. It's often
@@ -612,13 +617,14 @@ match my_number {
612
617
}
613
618
~~~
614
619
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.
618
624
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:
622
628
623
629
~~~~
624
630
fn angle(vector: (float, float)) -> float {
@@ -631,37 +637,39 @@ fn angle(vector: (float, float)) -> float {
631
637
}
632
638
~~~~
633
639
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,
636
642
y)` matches any tuple whose first element is zero, and binds ` y` to
637
643
the second element. ` (x, y) ` matches any tuple, and binds both
638
- elements to a variable .
644
+ elements to variables .
639
645
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.
644
652
645
653
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 ` .
649
657
650
658
~~~~
651
659
# fn get_tuple_of_two_ints() -> (int, int) { (1, 1) }
652
660
let (a, b) = get_tuple_of_two_ints();
653
661
~~~~
654
662
655
- Let bindings only work with _ irrefutable_ patterns, that is, patterns
663
+ Let bindings only work with _ irrefutable_ patterns: that is, patterns
656
664
that can never fail to match. This excludes ` let ` from matching
657
- literals and most enum variants.
665
+ literals and most ` enum ` variants.
658
666
659
667
## Loops
660
668
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.
665
673
666
674
~~~~
667
675
let mut cake_amount = 8;
@@ -670,7 +678,7 @@ while cake_amount > 0 {
670
678
}
671
679
~~~~
672
680
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 ` :
674
682
675
683
~~~~
676
684
let mut x = 5;
@@ -684,9 +692,8 @@ loop {
684
692
This code prints out a weird sequence of numbers and stops as soon as
685
693
it finds one that can be divided by five.
686
694
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 ) .
690
697
691
698
# Data structures
692
699
0 commit comments