@@ -90,7 +90,7 @@ supported build environments that are most likely to work.
90
90
To build from source you will also need the following prerequisite
91
91
packages:
92
92
93
- * g++ 4.7 or clang++ 3.x
93
+ * g++ 4.4 or clang++ 3.x
94
94
* python 2.6 or later (but not 3.x)
95
95
* perl 5.0 or later
96
96
* gnu make 3.81 or later
@@ -468,16 +468,19 @@ Unlike in C, there is no "falling through" between arms: only one arm
468
468
executes, and it doesn't have to explicitly ` break ` out of the
469
469
construct when it is finished.
470
470
471
- A ` match ` arm consists of a * pattern* , then a fat arrow ` => ` , followed
472
- by an * action* (expression). Each case is separated by commas. It is
473
- often convenient to use a block expression for each case, in which case
474
- the commas are optional as shown below. Literals are valid patterns and
475
- match only their own value. A single arm may match multiple different
476
- patterns by combining them with the pipe operator (` | ` ), so long as every
477
- pattern binds the same set of variables. Ranges of numeric literal
478
- patterns can be expressed with two dots, as in ` M..N ` . The underscore
479
- (` _ ` ) is a wildcard pattern that matches any single value. (` .. ` ) is a
480
- different wildcard that can match one or more fields in an ` enum ` variant.
471
+ A ` match ` arm consists of a * pattern* , then an arrow ` => ` , followed by
472
+ an * action* (expression). Literals are valid patterns and match only
473
+ their own value. A single arm may match multiple different patterns by
474
+ combining them with the pipe operator (` | ` ), so long as every pattern
475
+ binds the same set of variables. Ranges of numeric literal patterns
476
+ can be expressed with two dots, as in ` M..N ` . The underscore (` _ ` ) is
477
+ a wildcard pattern that matches any single value. (` .. ` ) is a different
478
+ wildcard that can match one or more fields in an ` enum ` variant.
479
+
480
+ The patterns in a match arm are followed by a fat arrow, ` => ` , then an
481
+ expression to evaluate. Each case is separated by commas. It's often
482
+ convenient to use a block expression for each case, in which case the
483
+ commas are optional.
481
484
482
485
~~~
483
486
# let my_number = 1;
@@ -1413,7 +1416,7 @@ contains a point, but allocated in a different location:
1413
1416
1414
1417
~~~
1415
1418
# struct Point { x: f64, y: f64 }
1416
- let on_the_stack : Point = Point { x: 3.0, y: 4.0 };
1419
+ let on_the_stack : Point = Point { x: 3.0, y: 4.0 };
1417
1420
let owned_box : ~ Point = ~ Point { x: 7.0, y: 9.0 };
1418
1421
~~~
1419
1422
@@ -1717,103 +1720,38 @@ environment (sometimes referred to as "capturing" variables in their
1717
1720
environment). For example, you couldn't write the following:
1718
1721
1719
1722
~~~~ {.ignore}
1720
- let x = 3 ;
1723
+ let foo = 10 ;
1721
1724
1722
- // `fun` cannot refer to `x`
1723
- fn fun() -> () { println!("{}", x); }
1725
+ fn bar() -> int {
1726
+ return foo; // `bar` cannot refer to `foo`
1727
+ }
1724
1728
~~~~
1725
1729
1726
- A _closure_ does support accessing the enclosing scope; below we will create
1727
- 2 _closures_ (nameless functions). Compare how `||` replaces `()` and how
1728
- they try to access `x`:
1729
-
1730
- ~~~~ {.ignore}
1731
- let x = 3;
1730
+ Rust also supports _closures_, functions that can access variables in
1731
+ the enclosing scope.
1732
1732
1733
- // `fun` is an invalid definition
1734
- fn fun () -> () { println!("{}", x) } // cannot capture from enclosing scope
1735
- let closure = || -> () { println!("{}", x) }; // can capture from enclosing scope
1736
-
1737
- // `fun_arg` is an invalid definition
1738
- fn fun_arg (arg: int) -> () { println!("{}", arg + x) } // cannot capture
1739
- let closure_arg = |arg: int| -> () { println!("{}", arg + x) }; // can capture
1740
- // ^
1741
- // Requires a type because the implementation needs to know which `+` to use.
1742
- // In the future, the implementation may not need the help.
1733
+ ~~~~
1734
+ fn call_closure_with_ten(b: |int|) { b(10); }
1743
1735
1744
- fun(); // Still won't work
1745
- closure(); // Prints: 3
1736
+ let captured_var = 20;
1737
+ let closure = |arg| println!("captured_var={}, arg={}", captured_var, arg);
1746
1738
1747
- fun_arg(7); // Still won't work
1748
- closure_arg(7); // Prints: 10
1739
+ call_closure_with_ten(closure);
1749
1740
~~~~
1750
1741
1751
1742
Closures begin with the argument list between vertical bars and are followed by
1752
1743
a single expression. Remember that a block, `{ <expr1>; <expr2>; ... }`, is
1753
1744
considered a single expression: it evaluates to the result of the last
1754
1745
expression it contains if that expression is not followed by a semicolon,
1755
- otherwise the block evaluates to `()`, the unit value.
1756
-
1757
- In general, return types and all argument types must be specified
1758
- explicitly for function definitions. (As previously mentioned in the
1759
- [Functions section](#functions), omitting the return type from a
1760
- function declaration is synonymous with an explicit declaration of
1761
- return type unit, `()`.)
1746
+ otherwise the block evaluates to `()`.
1762
1747
1763
- ~~~~ {.ignore}
1764
- fn fun (x: int) { println!("{}", x) } // this is same as saying `-> ()`
1765
- fn square(x: int) -> uint { (x * x) as uint } // other return types are explicit
1748
+ The types of the arguments are generally omitted, as is the return type,
1749
+ because the compiler can almost always infer them. In the rare case where the
1750
+ compiler needs assistance, though, the arguments and return types may be
1751
+ annotated.
1766
1752
1767
- // Error: mismatched types: expected `()` but found `uint`
1768
- fn badfun(x: int) { (x * x) as uint }
1769
1753
~~~~
1770
-
1771
- On the other hand, the compiler can usually infer both the argument
1772
- and return types for a closure expression; therefore they are often
1773
- omitted, since both a human reader and the compiler can deduce the
1774
- types from the immediate context. This is in contrast to function
1775
- declarations, which require types to be specified and are not subject
1776
- to type inference. Compare:
1777
-
1778
- ~~~~ {.ignore}
1779
- // `fun` as a function declaration cannot infer the type of `x`, so it must be provided
1780
- fn fun (x: int) { println!("{}", x) }
1781
- let closure = |x | { println!("{}", x) }; // infers `x: int`, return type `()`
1782
-
1783
- // For closures, omitting a return type is *not* synonymous with `-> ()`
1784
- let add_3 = |y | { 3i + y }; // infers `y: int`, return type `int`.
1785
-
1786
- fun(10); // Prints 10
1787
- closure(20); // Prints 20
1788
- closure(add_3(30)); // Prints 33
1789
-
1790
- fun("String"); // Error: mismatched types
1791
-
1792
- // Error: mismatched types
1793
- // inference already assigned `closure` the type `|int| -> ()`
1794
- closure("String");
1795
- ~~~~
1796
-
1797
- In cases where the compiler needs assistance, the arguments and return
1798
- types may be annotated on closures, using the same notation as shown
1799
- earlier. In the example below, since different types provide an
1800
- implementation for the operator `*`, the argument type for the `x`
1801
- parameter must be explicitly provided.
1802
-
1803
- ~~~~{.ignore}
1804
- // Error: the type of `x` must be known to be used with `x * x`
1805
- let square = |x | -> uint { (x * x) as uint };
1806
- ~~~~
1807
-
1808
- In the corrected version, the argument type is explicitly annotated,
1809
- while the return type can still be inferred.
1810
-
1811
- ~~~~
1812
- let square_explicit = |x: int| -> uint { (x * x) as uint };
1813
- let square_infer = |x: int| { (x * x) as uint };
1814
-
1815
- println!("{}", square_explicit(20)); // 400
1816
- println!("{}", square_infer(-20)); // 400
1754
+ let square = |x: int| -> uint { (x * x) as uint };
1817
1755
~~~~
1818
1756
1819
1757
There are several forms of closure, each with its own role. The most
@@ -2584,18 +2522,11 @@ for `Eq` and can be used with the equality operators, and that a value
2584
2522
of type ` ABC ` can be randomly generated and converted to a string:
2585
2523
2586
2524
~~~
2587
- extern crate rand;
2588
-
2589
2525
#[deriving(Eq)]
2590
2526
struct Circle { radius: f64 }
2591
2527
2592
- #[deriving(Rand , Show)]
2528
+ #[deriving(Clone , Show)]
2593
2529
enum ABC { A, B, C }
2594
-
2595
- fn main() {
2596
- // Use the Show trait to print "A, B, C."
2597
- println!("{}, {}, {}", A, B, C);
2598
- }
2599
2530
~~~
2600
2531
2601
2532
The full list of derivable traits is ` Eq ` , ` TotalEq ` , ` Ord ` ,
0 commit comments