@@ -62,7 +62,7 @@ the easiest way to keep people updated while Rust is in its alpha state.
6262
6363Oh, we should also mention the officially supported platforms:
6464
65- * Windows (7, 8, Server 2008 R2), x86 only
65+ * Windows (7, 8, Server 2008 R2)
6666* Linux (2.6.18 or later, various distributions), x86 and x86-64
6767* OSX 10.7 (Lion) or greater, x86 and x86-64
6868
@@ -378,9 +378,15 @@ of your time with Rust.
378378The first thing we'll learn about are 'variable bindings.' They look like this:
379379
380380``` {rust}
381- let x = 5i;
381+ fn main() {
382+ let x = 5i;
383+ }
382384```
383385
386+ Putting ` fn main() { ` in each example is a bit tedious, so we'll leave that out
387+ in the future. If you're following along, make sure to edit your ` main() `
388+ function, rather than leaving it off. Otherwise, you'll get an error.
389+
384390In many languages, this is called a 'variable.' But Rust's variable bindings
385391have a few tricks up their sleeves. Rust has a very powerful feature called
386392'pattern matching' that we'll get into detail with later, but the left
@@ -683,7 +689,7 @@ fn main() {
683689```
684690
685691This is the simplest possible function declaration. As we mentioned before,
686- ` fn ` says 'this is a function,' followed by the name, some parenthesis because
692+ ` fn ` says 'this is a function,' followed by the name, some parentheses because
687693this function takes no arguments, and then some curly braces to indicate the
688694body. Here's a function named ` foo ` :
689695
@@ -884,7 +890,7 @@ Tuples are an ordered list of a fixed size. Like this:
884890let x = (1i , " hello" );
885891```
886892
887- The parenthesis and commas form this two-length tuple. Here's the same code, but
893+ The parentheses and commas form this two-length tuple. Here's the same code, but
888894with the type annotated:
889895
890896``` rust
@@ -908,9 +914,9 @@ let (x, y, z) = (1i, 2i, 3i);
908914println! (" x is {}" , x );
909915```
910916
911- Remember before when I said the left hand side of a ` let ` statement was more
917+ Remember before when I said the left- hand side of a ` let ` statement was more
912918powerful than just assigning a binding? Here we are. We can put a pattern on
913- the left hand side of the ` let ` , and if it matches up to the right hand side,
919+ the left- hand side of the ` let ` , and if it matches up to the right- hand side,
914920we can assign multiple bindings at once. In this case, ` let ` 'destructures,'
915921or 'breaks up,' the tuple, and assigns the bits to three bindings.
916922
@@ -1453,9 +1459,9 @@ focus. Any time you have a data structure of variable size, things can get
14531459tricky, and strings are a re-sizable data structure. That said, Rust's strings
14541460also work differently than in some other systems languages, such as C.
14551461
1456- Let's dig into the details. A ** string** is a sequence of unicode scalar values
1462+ Let's dig into the details. A ** string** is a sequence of Unicode scalar values
14571463encoded as a stream of UTF-8 bytes. All strings are guaranteed to be
1458- validly- encoded UTF-8 sequences. Additionally, strings are not null-terminated
1464+ validly encoded UTF-8 sequences. Additionally, strings are not null-terminated
14591465and can contain null bytes.
14601466
14611467Rust has two main types of strings: ` &str ` and ` String ` .
@@ -3933,7 +3939,7 @@ match x {
39333939}
39343940```
39353941
3936- Here, the ` val ` inside the ` match ` has type ` int ` . In other words, the left hand
3942+ Here, the ` val ` inside the ` match ` has type ` int ` . In other words, the left- hand
39373943side of the pattern destructures the value. If we have ` &5i ` , then in ` &val ` , ` val `
39383944would be ` 5i ` .
39393945
@@ -3991,6 +3997,35 @@ match origin {
39913997}
39923998```
39933999
4000+ You can do this kind of match on any member, not just the first:
4001+
4002+ ``` {rust}
4003+ # #![allow(non_shorthand_field_patterns)]
4004+ struct Point {
4005+ x: int,
4006+ y: int,
4007+ }
4008+
4009+ let origin = Point { x: 0i, y: 0i };
4010+
4011+ match origin {
4012+ Point { y: y, .. } => println!("y is {}", y),
4013+ }
4014+ ```
4015+
4016+ If you want to match against a slice or array, you can use ` [] ` :
4017+
4018+ ``` {rust}
4019+ fn main() {
4020+ let v = vec!["match_this", "1"];
4021+
4022+ match v.as_slice() {
4023+ ["match_this", second] => println!("The second element is {}", second),
4024+ _ => {},
4025+ }
4026+ }
4027+ ```
4028+
39944029Whew! That's a lot of different ways to match things, and they can all be
39954030mixed and matched, depending on what you're doing:
39964031
@@ -4681,7 +4716,7 @@ let x: Option<int> = Some(5i);
46814716
46824717In the type declaration, we say ` Option<int> ` . Note how similar this looks to
46834718` Option<T> ` . So, in this particular ` Option ` , ` T ` has the value of ` int ` . On
4684- the right hand side of the binding, we do make a ` Some(T) ` , where ` T ` is ` 5i ` .
4719+ the right- hand side of the binding, we do make a ` Some(T) ` , where ` T ` is ` 5i ` .
46854720Since that's an ` int ` , the two sides match, and Rust is happy. If they didn't
46864721match, we'd get an error:
46874722
@@ -5249,7 +5284,7 @@ immediately.
52495284
52505285## Success and failure
52515286
5252- Tasks don't always succeed, they can also panic. A task that wishes to panic
5287+ Tasks don't always succeed, they can also panic. A task that wishes to panic
52535288can call the ` panic! ` macro, passing a message:
52545289
52555290``` {rust}
0 commit comments