From d9a2db5344c7a2533cf0ceb8ba6169c9be20174c Mon Sep 17 00:00:00 2001 From: Steve Klabnik Date: Sat, 30 Jul 2016 18:43:22 +0000 Subject: [PATCH] rebuild pages at 1e7e0e6 --- ch01-01-introduction.html | 2 +- ch01-02-installation.html | 2 +- ch01-03-hello-world.html | 2 +- ch01-04-design.html | 2 +- ch03-01-up-and-running.html | 2 +- ch03-02-variable-bindings.html | 79 +--------- ch03-03-functions.html | 2 +- ch03-04-scalar-types.html | 2 +- ch03-05-compound-types.html | 2 +- ch03-06-comments.html | 2 +- ch03-07-if.html | 2 +- ch03-08-loops.html | 2 +- ch04-01-understanding-ownership.html | 2 +- ch04-02-ownership.html | 2 +- ch04-03-references-and-borrowing.html | 2 +- ch04-04-slices.html | 2 +- ch05-01-structs.html | 2 +- ch05-02-method-syntax.html | 2 +- ch05-03-generics.html | 2 +- ch06-01-enums.html | 2 +- ch06-02-option.html | 2 +- ch06-03-match.html | 2 +- ch06-04-if-let.html | 6 +- ch06-05-patterns.html => chXX-patterns.html | 74 +++++++++- index.html | 2 +- print.html | 151 +++++++++----------- 26 files changed, 162 insertions(+), 192 deletions(-) rename ch06-05-patterns.html => chXX-patterns.html (66%) diff --git a/ch01-01-introduction.html b/ch01-01-introduction.html index 8ec0cf7a7..7c6384412 100644 --- a/ch01-01-introduction.html +++ b/ch01-01-introduction.html @@ -47,7 +47,7 @@
diff --git a/ch01-02-installation.html b/ch01-02-installation.html index c73143555..34a323aaa 100644 --- a/ch01-02-installation.html +++ b/ch01-02-installation.html @@ -47,7 +47,7 @@
diff --git a/ch01-03-hello-world.html b/ch01-03-hello-world.html index f014e48a4..269647330 100644 --- a/ch01-03-hello-world.html +++ b/ch01-03-hello-world.html @@ -47,7 +47,7 @@
diff --git a/ch01-04-design.html b/ch01-04-design.html index ec9d1ce4f..ed06b8f45 100644 --- a/ch01-04-design.html +++ b/ch01-04-design.html @@ -47,7 +47,7 @@
diff --git a/ch03-01-up-and-running.html b/ch03-01-up-and-running.html index dfa1c3127..979de49cd 100644 --- a/ch03-01-up-and-running.html +++ b/ch03-01-up-and-running.html @@ -47,7 +47,7 @@
diff --git a/ch03-02-variable-bindings.html b/ch03-02-variable-bindings.html index 9bc6ca902..55f20067c 100644 --- a/ch03-02-variable-bindings.html +++ b/ch03-02-variable-bindings.html @@ -47,7 +47,7 @@
@@ -108,18 +108,6 @@ start of the function’s body.

    let x = 5;
 

This is our first ‘variable binding’, which we create with a ‘let statement’.

-

This let statement has this form:

-
let NAME = EXPRESSION;
-
-

A let statement first evaluates the EXPRESSION, and then binds the -resulting value to NAME so that it can be referred to later in the program. -In our simple example, the expression was already a value, 5, but we could -achieve the same effect with:

-
let x = 2 + 3;
-
-

In general, let statements work with patterns; a name is a particularly -humble form of pattern. Patterns are a big part of Rust, we’ll see more complex -and powerful patterns as we go along.

Before we do that, though, let’s finish investigating this example. Here’s the next line:

    println!("The value of x is: {}", x);
@@ -150,71 +138,6 @@ declared the main() function, and declares its end.

We assign 5 to a binding, x, and then print it to the screen with println!.

-

Multiple binding

-

Let’s try a more complex pattern. Change our example program to this:

-
fn main() {
-    let (x, y) = (5, 6);
-
-    println!("The value of x is: {}", x);
-    println!("The value of y is: {}", y);
-}
-
-

And run it with cargo run:

-
$ cargo run
-   Compiling bindings v0.1.0 (file:///projects/bindings)
-     Running `target/debug/bindings`
-The value of x is: 5
-The value of y is: 6
-
-

We’ve created two bindings with one let! Here’s our pattern:

-
(x, y)
-
-

And here’s the value:

-
(5, 6)
-
-

As you can see, the two line up visually, and so let binds 5 to x and 6 -to y. We could have used two let statements as well:

-
fn main() {
-    let x = 5;
-    let y = 6;
-}
-
-

In simple cases like this, two lets may be clearer, but in others, creating -multiple bindings at once is nice. As we become more proficient in Rust, we’ll -figure out which style is better, but it’s mostly a judgement call.

-

Type annotations

-

You may have noticed that we didn’t declare the type of x or y in our -previous examples. Rust is a statically typed language, which means that at -compile time, we must know the types of all bindings. But annotating every -single binding with a type can feel like busywork, and make code noisy. To -solve this issue, Rust uses ‘type inference’, meaning that it attempts to infer -the types of your bindings.

-

The primary way that the type is inferred is by looking at how it is used. -Let’s look at the example again:

-
fn main() {
-    let x = 5;
-}
-
-

When we bind x to 5, the compiler knows that x should be a numeric type. -Without any other information, it defaults to i32, a thirty-two bit integer -type. We’ll talk more about Rust’s basic types in section 3.3.

-

Here’s what a let statement with a ‘type annotation’ looks like:

-
fn main() {
-    let x: i32 = 5;
-}
-
-

We can add a colon, followed by the type name. Here’s the structure of a let -statement with a type annotation:

-
let PATTERN: TYPE = VALUE;
-
-

Note that the colon and the TYPE go after the PATTERN, not in the pattern -itself. As an example, here’s our more complex pattern with two bindings:

-
fn main() {
-    let (x, y): (i32, i32) = (5, 6);
-}
-
-

Just like we match up the VALUE with the PATTERN, we match up the TYPE -with the PATTERN.

Delayed Initialization

We do not have to provide bindings with an initial value, and can assign it later. Try this program:

diff --git a/ch03-03-functions.html b/ch03-03-functions.html index ea09cb6d4..acd2582aa 100644 --- a/ch03-03-functions.html +++ b/ch03-03-functions.html @@ -47,7 +47,7 @@
diff --git a/ch03-04-scalar-types.html b/ch03-04-scalar-types.html index 1022d2c60..72f0adc00 100644 --- a/ch03-04-scalar-types.html +++ b/ch03-04-scalar-types.html @@ -47,7 +47,7 @@
diff --git a/ch03-05-compound-types.html b/ch03-05-compound-types.html index 58490ea32..34db8f6e6 100644 --- a/ch03-05-compound-types.html +++ b/ch03-05-compound-types.html @@ -47,7 +47,7 @@
diff --git a/ch03-06-comments.html b/ch03-06-comments.html index 3ee4e11d2..a2cc6b842 100644 --- a/ch03-06-comments.html +++ b/ch03-06-comments.html @@ -47,7 +47,7 @@
diff --git a/ch03-07-if.html b/ch03-07-if.html index 0838201b2..29f7c1743 100644 --- a/ch03-07-if.html +++ b/ch03-07-if.html @@ -47,7 +47,7 @@
diff --git a/ch03-08-loops.html b/ch03-08-loops.html index 8084fafa0..5f3aa683a 100644 --- a/ch03-08-loops.html +++ b/ch03-08-loops.html @@ -47,7 +47,7 @@
diff --git a/ch04-01-understanding-ownership.html b/ch04-01-understanding-ownership.html index b3eb6f0ab..cf6dd88c7 100644 --- a/ch04-01-understanding-ownership.html +++ b/ch04-01-understanding-ownership.html @@ -47,7 +47,7 @@
diff --git a/ch04-02-ownership.html b/ch04-02-ownership.html index 286cc8bfc..adcc9755c 100644 --- a/ch04-02-ownership.html +++ b/ch04-02-ownership.html @@ -47,7 +47,7 @@
diff --git a/ch04-03-references-and-borrowing.html b/ch04-03-references-and-borrowing.html index bc15776dc..e66fa68ad 100644 --- a/ch04-03-references-and-borrowing.html +++ b/ch04-03-references-and-borrowing.html @@ -47,7 +47,7 @@
diff --git a/ch04-04-slices.html b/ch04-04-slices.html index 92961971a..a0865399c 100644 --- a/ch04-04-slices.html +++ b/ch04-04-slices.html @@ -47,7 +47,7 @@
diff --git a/ch05-01-structs.html b/ch05-01-structs.html index de62dd176..7d16722b1 100644 --- a/ch05-01-structs.html +++ b/ch05-01-structs.html @@ -47,7 +47,7 @@
diff --git a/ch05-02-method-syntax.html b/ch05-02-method-syntax.html index 0ad8b1451..0bf255156 100644 --- a/ch05-02-method-syntax.html +++ b/ch05-02-method-syntax.html @@ -47,7 +47,7 @@
diff --git a/ch05-03-generics.html b/ch05-03-generics.html index 67018e3d3..2fd0ad27b 100644 --- a/ch05-03-generics.html +++ b/ch05-03-generics.html @@ -47,7 +47,7 @@
diff --git a/ch06-01-enums.html b/ch06-01-enums.html index 735a58bb7..d876c19ca 100644 --- a/ch06-01-enums.html +++ b/ch06-01-enums.html @@ -47,7 +47,7 @@
diff --git a/ch06-02-option.html b/ch06-02-option.html index 3d2a29fa9..bb5e34a51 100644 --- a/ch06-02-option.html +++ b/ch06-02-option.html @@ -47,7 +47,7 @@
diff --git a/ch06-03-match.html b/ch06-03-match.html index 241512aa6..5b302ef3f 100644 --- a/ch06-03-match.html +++ b/ch06-03-match.html @@ -47,7 +47,7 @@
diff --git a/ch06-04-if-let.html b/ch06-04-if-let.html index 6ad16e3c8..97dc8fa6d 100644 --- a/ch06-04-if-let.html +++ b/ch06-04-if-let.html @@ -47,7 +47,7 @@
@@ -129,7 +129,7 @@ do something with a single pattern.

- @@ -143,7 +143,7 @@ do something with a single pattern.

- diff --git a/ch06-05-patterns.html b/chXX-patterns.html similarity index 66% rename from ch06-05-patterns.html rename to chXX-patterns.html index 776ddfd02..6f48f09a2 100644 --- a/ch06-05-patterns.html +++ b/chXX-patterns.html @@ -47,7 +47,7 @@
@@ -68,10 +68,72 @@

Patterns

-

We've mentioned 'patterns' a few times so far: they're used in let bindings, -in function arguments, and in the match expression. Patterns have a lot of -abilities, so in this section, we'll cover all of the different things they can -do. Any of these abilities work in any place where a pattern is used.

+

We've actually used patterns a few times so far: they're used in let +bindings, in function arguments, and in the match expression. Patterns have a +lot more abilities than we have demonstrated so far, so in this section, we'll +cover all of the different things they can do. Any of these abilities work in +any place where a pattern is used.

+

let statements

+

A basic let statement has this form:

+
let PATTERN = EXPRESSION;
+
+

We've seen bindings that have names in the PATTERN slot: a name is just a +particularly humble form of pattern.

+

Multiple bindings

+

Let’s try a more complex pattern. Change our example program to this:

+
fn main() {
+    let (x, y) = (5, 6);
+
+    println!("The value of x is: {}", x);
+    println!("The value of y is: {}", y);
+}
+
+

And run it with cargo run:

+
$ cargo run
+   Compiling bindings v0.1.0 (file:///projects/bindings)
+     Running `target/debug/bindings`
+The value of x is: 5
+The value of y is: 6
+
+

We’ve created two bindings with one let! Here’s our pattern:

+
(x, y)
+
+

And here’s the value:

+
(5, 6)
+
+

As you can see, the two line up visually, and so let binds 5 to x and 6 +to y. We could have used two let statements as well:

+
fn main() {
+    let x = 5;
+    let y = 6;
+}
+
+

In simple cases like this, two lets may be clearer, but in others, creating +multiple bindings at once is nice. As we become more proficient in Rust, we’ll +figure out which style is better, but it’s mostly a judgement call.

+

Type annotations

+

Most of the time, Rust uses ‘type inference’, meaning that it attempts to infer +the types of your bindings rather than you having to declare them explicitly +even though Rust is a statically typed language. Occasionally, Rust won't have +enough information to infer the type of your value, and you will need to add a +type annotation in with the pattern.

+

Here’s what a let statement with a ‘type annotation’ looks like:

+
fn main() {
+    let x: i32 = 5;
+}
+
+

We can add a colon, followed by the type name. Here’s the structure of a let +statement with a type annotation:

+
let PATTERN: TYPE = VALUE;
+
+

Note that the colon and the TYPE go after the PATTERN, not in the pattern +itself. As an example, here’s our more complex pattern with two bindings:

+
fn main() {
+    let (x, y): (i32, i32) = (5, 6);
+}
+
+

Just like we match up the VALUE with the PATTERN, we match up the TYPE +with the PATTERN.

Literals & _

You can match against literals directly, and _ acts as an ‘any’ case:

let x = 1;
@@ -208,7 +270,7 @@ match x {
 

Ranges are usually used with integers or chars:

fn main() {
     let x = 'c';
-    
+
     match x {
         'a' ... 'j' => println!("early ASCII letter"),
         'k' ... 'z' => println!("late ASCII letter"),
diff --git a/index.html b/index.html
index e8c08e32e..f926be6c2 100644
--- a/index.html
+++ b/index.html
@@ -46,7 +46,7 @@
         
 
         
 
         
diff --git a/print.html b/print.html index 021b98b8c..ec6b89892 100644 --- a/print.html +++ b/print.html @@ -47,7 +47,7 @@
@@ -468,18 +468,6 @@ start of the function’s body.

    let x = 5;
 

This is our first ‘variable binding’, which we create with a ‘let statement’.

-

This let statement has this form:

-
let NAME = EXPRESSION;
-
-

A let statement first evaluates the EXPRESSION, and then binds the -resulting value to NAME so that it can be referred to later in the program. -In our simple example, the expression was already a value, 5, but we could -achieve the same effect with:

-
let x = 2 + 3;
-
-

In general, let statements work with patterns; a name is a particularly -humble form of pattern. Patterns are a big part of Rust, we’ll see more complex -and powerful patterns as we go along.

Before we do that, though, let’s finish investigating this example. Here’s the next line:

    println!("The value of x is: {}", x);
@@ -510,71 +498,6 @@ declared the main() function, and declares its end.

We assign 5 to a binding, x, and then print it to the screen with println!.

-

Multiple binding

-

Let’s try a more complex pattern. Change our example program to this:

-
fn main() {
-    let (x, y) = (5, 6);
-
-    println!("The value of x is: {}", x);
-    println!("The value of y is: {}", y);
-}
-
-

And run it with cargo run:

-
$ cargo run
-   Compiling bindings v0.1.0 (file:///projects/bindings)
-     Running `target/debug/bindings`
-The value of x is: 5
-The value of y is: 6
-
-

We’ve created two bindings with one let! Here’s our pattern:

-
(x, y)
-
-

And here’s the value:

-
(5, 6)
-
-

As you can see, the two line up visually, and so let binds 5 to x and 6 -to y. We could have used two let statements as well:

-
fn main() {
-    let x = 5;
-    let y = 6;
-}
-
-

In simple cases like this, two lets may be clearer, but in others, creating -multiple bindings at once is nice. As we become more proficient in Rust, we’ll -figure out which style is better, but it’s mostly a judgement call.

-

Type annotations

-

You may have noticed that we didn’t declare the type of x or y in our -previous examples. Rust is a statically typed language, which means that at -compile time, we must know the types of all bindings. But annotating every -single binding with a type can feel like busywork, and make code noisy. To -solve this issue, Rust uses ‘type inference’, meaning that it attempts to infer -the types of your bindings.

-

The primary way that the type is inferred is by looking at how it is used. -Let’s look at the example again:

-
fn main() {
-    let x = 5;
-}
-
-

When we bind x to 5, the compiler knows that x should be a numeric type. -Without any other information, it defaults to i32, a thirty-two bit integer -type. We’ll talk more about Rust’s basic types in section 3.3.

-

Here’s what a let statement with a ‘type annotation’ looks like:

-
fn main() {
-    let x: i32 = 5;
-}
-
-

We can add a colon, followed by the type name. Here’s the structure of a let -statement with a type annotation:

-
let PATTERN: TYPE = VALUE;
-
-

Note that the colon and the TYPE go after the PATTERN, not in the pattern -itself. As an example, here’s our more complex pattern with two bindings:

-
fn main() {
-    let (x, y): (i32, i32) = (5, 6);
-}
-
-

Just like we match up the VALUE with the PATTERN, we match up the TYPE -with the PATTERN.

Delayed Initialization

We do not have to provide bindings with an initial value, and can assign it later. Try this program:

@@ -3648,10 +3571,72 @@ match expression {

In other words, it's the high-level construct we were originally looking for: do something with a single pattern.

Patterns

-

We've mentioned 'patterns' a few times so far: they're used in let bindings, -in function arguments, and in the match expression. Patterns have a lot of -abilities, so in this section, we'll cover all of the different things they can -do. Any of these abilities work in any place where a pattern is used.

+

We've actually used patterns a few times so far: they're used in let +bindings, in function arguments, and in the match expression. Patterns have a +lot more abilities than we have demonstrated so far, so in this section, we'll +cover all of the different things they can do. Any of these abilities work in +any place where a pattern is used.

+

let statements

+

A basic let statement has this form:

+
let PATTERN = EXPRESSION;
+
+

We've seen bindings that have names in the PATTERN slot: a name is just a +particularly humble form of pattern.

+

Multiple bindings

+

Let’s try a more complex pattern. Change our example program to this:

+
fn main() {
+    let (x, y) = (5, 6);
+
+    println!("The value of x is: {}", x);
+    println!("The value of y is: {}", y);
+}
+
+

And run it with cargo run:

+
$ cargo run
+   Compiling bindings v0.1.0 (file:///projects/bindings)
+     Running `target/debug/bindings`
+The value of x is: 5
+The value of y is: 6
+
+

We’ve created two bindings with one let! Here’s our pattern:

+
(x, y)
+
+

And here’s the value:

+
(5, 6)
+
+

As you can see, the two line up visually, and so let binds 5 to x and 6 +to y. We could have used two let statements as well:

+
fn main() {
+    let x = 5;
+    let y = 6;
+}
+
+

In simple cases like this, two lets may be clearer, but in others, creating +multiple bindings at once is nice. As we become more proficient in Rust, we’ll +figure out which style is better, but it’s mostly a judgement call.

+

Type annotations

+

Most of the time, Rust uses ‘type inference’, meaning that it attempts to infer +the types of your bindings rather than you having to declare them explicitly +even though Rust is a statically typed language. Occasionally, Rust won't have +enough information to infer the type of your value, and you will need to add a +type annotation in with the pattern.

+

Here’s what a let statement with a ‘type annotation’ looks like:

+
fn main() {
+    let x: i32 = 5;
+}
+
+

We can add a colon, followed by the type name. Here’s the structure of a let +statement with a type annotation:

+
let PATTERN: TYPE = VALUE;
+
+

Note that the colon and the TYPE go after the PATTERN, not in the pattern +itself. As an example, here’s our more complex pattern with two bindings:

+
fn main() {
+    let (x, y): (i32, i32) = (5, 6);
+}
+
+

Just like we match up the VALUE with the PATTERN, we match up the TYPE +with the PATTERN.

Literals & _

You can match against literals directly, and _ acts as an ‘any’ case:

let x = 1;
@@ -3788,7 +3773,7 @@ match x {
 

Ranges are usually used with integers or chars:

fn main() {
     let x = 'c';
-    
+
     match x {
         'a' ... 'j' => println!("early ASCII letter"),
         'k' ... 'z' => println!("late ASCII letter"),