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"),