From 1a87ffabb85384f6dab0484dc74466b16d84fe2a Mon Sep 17 00:00:00 2001 From: Steve Klabnik Date: Fri, 19 Aug 2016 17:26:37 +0000 Subject: [PATCH] rebuild pages at 1b4c061 --- ch01-02-hello-world.html | 4 + ch02-00-guessing-game-tutorial.html | 11 +++ ch03-01-variable-bindings-and-mutability.html | 3 + ch03-02-data-types.html | 11 +++ ch03-03-how-functions-work.html | 9 ++ ch03-04-comments.html | 2 + ch03-05-control-flow.html | 11 +++ ch04-01-ownership.html | 5 + ch04-02-references-and-borrowing.html | 6 ++ ch04-03-slices.html | 7 +- ch05-00-structs.html | 5 + ch06-00-enums.html | 2 +- chXX-patterns.html | 21 ++-- print.html | 97 ++++++++++++++++--- 14 files changed, 164 insertions(+), 30 deletions(-) diff --git a/ch01-02-hello-world.html b/ch01-02-hello-world.html index 2fb25beac..3a46cf22e 100644 --- a/ch01-02-hello-world.html +++ b/ch01-02-hello-world.html @@ -98,6 +98,7 @@ the .rs extension. If you’re using more than one word in your fil an underscore to separate them. For example, you'd use hello_world.rs rather than helloworld.rs.

Now open the main.rs file you just created, and type the following code:

+

Filename: main.rs

fn main() {
     println!("Hello, world!");
 }
@@ -228,6 +229,7 @@ different version control system, or no version control system, by using the
 --vcs flag.

Open up Cargo.toml in your text editor of choice. It should look something like this:

+

Filename: Cargo.toml

[package]
 name = "hello_cargo"
 version = "0.1.0"
@@ -252,6 +254,7 @@ depend on so that Cargo knows to download and compile those too. We won't need
 any other crates for this project, but we will in the guessing game tutorial in
 the next chapter.

Now let's look at src/main.rs:

+

Filename: src/main.rs

fn main() {
     println!("Hello, world!");
 }
@@ -285,6 +288,7 @@ Hello, world!
 

Bam! If all goes well, Hello, world! should print to the terminal once more.

Running cargo build for the first time also causes Cargo to create a new file at the top level called Cargo.lock, which looks like this:

+

Filename: Cargo.lock

[root]
 name = "hello_cargo"
 version = "0.1.0"
diff --git a/ch02-00-guessing-game-tutorial.html b/ch02-00-guessing-game-tutorial.html
index 640d7e38f..de2faad82 100644
--- a/ch02-00-guessing-game-tutorial.html
+++ b/ch02-00-guessing-game-tutorial.html
@@ -82,6 +82,7 @@ $ cd guessing_game
 

We pass the name of our project to cargo new, then the --bin flag, since we’re going to be making another binary like in Chapter 1.

Take a look at the generated Cargo.toml:

+

Filename: Cargo.toml

[package]
 name = "guessing_game"
 version = "0.1.0"
@@ -93,6 +94,7 @@ authors = ["Your Name <you@example.com>"]
 go ahead and fix that.

And as we saw in the last chapter, cargo new generates a "Hello, world!" for us. Check out src/main.rs:

+

Filename: src/main.rs

fn main() {
     println!("Hello, world!");
 }
@@ -111,6 +113,7 @@ file.

Processing a Guess

Let’s get to it! The first thing we need to do for our guessing game is allow our player to input a guess. Put this in your src/main.rs:

+

Filename: src/main.rs

use std::io;
 
 fn main() {
@@ -282,6 +285,7 @@ programs.

the code using rand, we need to modify our Cargo.toml. Open it up, and add this line at the bottom beneath the [dependencies] section header that Cargo created for you:

+

Filename: Cargo.toml

[dependencies]
 
 rand = "0.3.14"
@@ -352,6 +356,7 @@ that’s all we need to know. Cargo makes it really easy to re-use libraries, so
 Rustaceans are able to write smaller projects which are assembled out of a
 number of sub-packages.

Let’s get on to actually using rand. Here’s our next step:

+

Filename: src/main.rs

extern crate rand;
 
 use std::io;
@@ -423,6 +428,7 @@ You guessed: 5
 

Comparing guesses

Now that we’ve got user input, let’s compare our guess to the secret number. Here’s part of our next step. It won't quite compile yet though:

+

Filename: src/main.rs

extern crate rand;
 
 use std::io;
@@ -512,6 +518,7 @@ be the same type.

Ultimately, we want to convert the String we read as input into a real number type so that we can compare it to the guess numerically. We can do that with two more lines. Here’s our new program:

+

Filename: src/main.rs

extern crate rand;
 
 use std::io;
@@ -592,6 +599,7 @@ the secret number works, as well as guessing a number too small.

change that by adding loops!

Looping

The loop keyword gives us an infinite loop. Let’s add that in:

+

Filename: src/main.rs

extern crate rand;
 
 use std::io;
@@ -654,6 +662,7 @@ error: Process didn't exit successfully: `target/debug/guess` (exit code: 101)
 

Ha! quit actually quits. As does any other non-number input. Well, this is suboptimal to say the least. First, let’s actually quit when you win the game:

+

Filename: src/main.rs

extern crate rand;
 
 use std::io;
@@ -696,6 +705,7 @@ win. Exiting the loop also means exiting the program, since the loop is the last
 thing in main(). We have another tweak to make: when someone inputs a
 non-number, we don’t want to quit, we want to ignore it. We can do that
 like this:

+

Filename: src/main.rs

extern crate rand;
 
 use std::io;
@@ -777,6 +787,7 @@ You win!
 think of what it is? That’s right, we don’t want to print out the secret
 number. It was good for testing, but it kind of ruins the game. Here’s our
 final source:

+

Filename: src/main.rs

extern crate rand;
 
 use std::io;
diff --git a/ch03-01-variable-bindings-and-mutability.html b/ch03-01-variable-bindings-and-mutability.html
index a9a50a048..a0d97f980 100644
--- a/ch03-01-variable-bindings-and-mutability.html
+++ b/ch03-01-variable-bindings-and-mutability.html
@@ -82,6 +82,7 @@ in. From there, run these commands:

$ cd bindings

Then open src/main.rs and replace its code with the following:

+

Filename: src/main.rs

fn main() {
     let x = 5;
     println!("The value of x is: {}", x);
@@ -164,6 +165,7 @@ addition to telling the compiler it should allow this value to be changed, it
 conveys intent to future readers of the code and says that other parts of the
 code will be changing this value.

For example, change the program you just wrote to the following:

+

Filename: src/main.rs

fn main() {
     let mut x = 5;
     println!("The value of x is: {}", x);
@@ -192,6 +194,7 @@ that the second binding's value is what you will see when you use the variable
 after the second binding. This can be useful if you’d like to perform a few
 transformations on a value, but have the binding be immutable after those
 transformations have been completed. For example:

+

Filename: src/main.rs

fn main() {
     let x = 5;
 
diff --git a/ch03-02-data-types.html b/ch03-02-data-types.html
index be4059661..f1140ada4 100644
--- a/ch03-02-data-types.html
+++ b/ch03-02-data-types.html
@@ -129,6 +129,7 @@ possible to use an f64 on 32 bit systems, but it will be slower tha
 for better precision is a reasonable initial choice, and you should benchmark
 your code if you suspect floating-point size is a problem in your case.

Here's an example showing floating-point numbers in action:

+

Filename: src/main.rs

fn main() {
     let x = 2.0; // f64
 
@@ -141,6 +142,7 @@ your code if you suspect floating-point size is a problem in your case.

Rust supports the usual basic mathematic operations you’d expect for all of these number types: addition, subtraction, multiplication, division, and modulo. This code shows how you'd use each one in a let statement:

+

Filename: src/main.rs

fn main() {
     // addition
     let sum = 5 + 10;
@@ -164,6 +166,7 @@ to a single value, which is then bound to a variable.

As in most other programming languages, a boolean type in Rust has two possible values: true and false. The boolean type in Rust is specified with bool. For example:

+

Filename: src/main.rs

fn main() {
     let t = true;
 
@@ -177,6 +180,7 @@ section of this chapter.

So far we’ve only worked with numbers, but Rust supports letters too. Rust’s char type is the language's most primitive alphabetic type, and this code shows one way to use it:

+

Filename: src/main.rs

fn main() {
    let c = 'z';
    let z = 'ℤ';
@@ -196,6 +200,7 @@ all unicode code points at http://www.unicode.org/charts/.

The Byte Type

You can work with the bytes of data directly. Byte literals can be created from the ASCII characters using b and single quotes:

+

Filename: src/main.rs

fn main() {
     let byte = b'a';
     println!("byte is {}", byte);
@@ -214,6 +219,7 @@ general way of grouping together some number of other values with distinct
 types into one compound type.

We create a tuple by writing a comma-separated list of values inside parentheses. Each position in the tuple has a distinct type, as in this example:

+

Filename: src/main.rs

fn main() {
     let tup: (i32, f64, u8) = (500, 6.4, 1);
 }
@@ -222,6 +228,7 @@ parentheses. Each position in the tuple has a distinct type, as in this example:
 name tup to the entire tuple, emphasizing the fact that a tuple is considered
 a single compound element. We could then use pattern matching to destructure
 this tuple value, like this:

+

Filename: src/main.rs

fn main() {
     let tup: (i32, f64, u8) = (500, 6.4, 1);
 
@@ -239,6 +246,7 @@ the single tuple into three parts.

In addition to destructuring through pattern matching, we can also access a tuple element directly by using a period (.) followed by the index of the value we want to access. For example:

+

Filename: src/main.rs

fn main() {
     let x: (i32, f64, u8) = (500, 6.4, 1);
 
@@ -259,6 +267,7 @@ Arrays in Rust are different than arrays in some other languages because arrays
 in Rust have a fixed length-- once declared, they cannot grow or shrink in size.

In Rust, the values going into an array are written as a comma separated list inside square brackets:

+

Filename: src/main.rs

fn main() {
     let a = [1, 2, 3, 4, 5];
 }
@@ -271,6 +280,7 @@ and we'll discuss them in more detail in chapter XX.

Accessing Array Elements

An array is a single chunk of memory, allocated on the stack. We can access elements of an array using indexing, like this:

+

Filename: src/main.rs

fn main() {
     let a = [1, 2, 3, 4, 5];
 
@@ -287,6 +297,7 @@ values.

Invalid array element access

What happens if you try to access an element of an array past the end of the array? Say we changed our program to:

+

Filename: src/main.rs

fn main() {
     let a = [1, 2, 3, 4, 5];
 
diff --git a/ch03-03-how-functions-work.html b/ch03-03-how-functions-work.html
index 11f557e59..f7bdde9d0 100644
--- a/ch03-03-how-functions-work.html
+++ b/ch03-03-how-functions-work.html
@@ -77,6 +77,7 @@ snake case, all letters are lower case, and there are underscores separating
 words. (Rust also uses snake case for the names of variable bindings; we just
 haven't used any variable bindings with enough letters to need underscores
 yet). Here's a program containing an example function definition:

+

Filename: src/main.rs

fn main() {
     println!("Hello, world!");
 
@@ -116,6 +117,7 @@ message is printed.

Function Arguments

Functions can also take arguments. The following rewritten version of another_function() shows what arguments look like in Rust:

+

Filename: src/main.rs

fn main() {
     another_function(5);
 }
@@ -140,6 +142,7 @@ definitions means the compiler almost never needs you to use them elsewhere in
 the code in order to figure out what you mean.

When you want a function to have multiple arguments, just separate them inside the function signature with commas, like this:

+

Filename: src/main.rs

fn main() {
     another_function(5, 6);
 }
@@ -177,6 +180,7 @@ instructions that perform some action and do not return a value. Expressions
 evaluate to a resulting value. Let's look at some examples.

Let bindings are statements. They instruct the program to create a binding name and assign a value to it. let y = 6; in this example is a statement:

+

Filename: src/main.rs

fn main() {
     let y = 6;
 }
@@ -185,6 +189,7 @@ name and assign a value to it. let y = 6; in this example is a stat
 statement as well.

Statements do not return values themselves. Therefore, you can’t assign a let binding to another binding, as this code tries to do:

+

Filename: src/main.rs

fn main() {
     let x = (let y = 6);
 }
@@ -214,6 +219,7 @@ Consider a simple math operation, like this:

can be part of statements-- in the previous example that had the statement let y = 6;, 6 is an expression that evaluates to the value 6. Calling a function is an expression. Calling a macro is an expression. The block that we use to create new scopes, {}, is an expression, for example:

+

Filename: src/main.rs

fn main() {
     let x = 5;
 
@@ -245,6 +251,7 @@ return values, but we do declare their type, after an arrow (->)
 the "return value of the function” is synonymous with “the value of the final
 expression in the block of the body of a function.” Here's an example of a
 function that returns a value:

+

Filename: src/main.rs

fn five() -> i32 {
     5
 }
@@ -275,6 +282,7 @@ use the return value of a function to initialize a binding.

arguments and defines the type of the return value, but the body of the function is a lonely 5 with no semicolon because it is an expression whose value we want to return. Let's look at another example:

+

Filename: src/main.rs

fn main() {
     let x = plus_one(5);
 
@@ -288,6 +296,7 @@ fn plus_one(x: i32) -> i32 {
 

Running this code will print The value of x is: 6. What happens if we put a semicolon at the end of the line containing x + 1, changing it from an expression to a statement?

+

Filename: src/main.rs

fn main() {
     let x = plus_one(5);
 
diff --git a/ch03-04-comments.html b/ch03-04-comments.html
index 517ea5c6f..72ad7aed8 100644
--- a/ch03-04-comments.html
+++ b/ch03-04-comments.html
@@ -83,11 +83,13 @@ include // on each line, like this:

// explain what’s going on.

Comments can also be placed at the end of lines of code:

+

Filename: src/main.rs

fn main() {
     let lucky_number = 7; // I’m feeling lucky today.
 }
 

But you’ll more often see them above, like so:

+

Filename: src/main.rs

fn main() {
     // I’m feeling lucky today.
     let lucky_number = 7;
diff --git a/ch03-05-control-flow.html b/ch03-05-control-flow.html
index 2aba19a9b..14c02dd6e 100644
--- a/ch03-05-control-flow.html
+++ b/ch03-05-control-flow.html
@@ -84,6 +84,7 @@ $ cd branches
 

Write this sample program using if and save it in the branches directory in src/main.rs:

+

Filename: src/main.rs

fn main() {
     let number = 3;
 
@@ -119,6 +120,7 @@ condition was false
 

It’s also worth noting that condition here must be a bool. To see what happens if the condition isn't a bool, try running this code:

+

Filename: src/main.rs

fn main() {
     let number = 3;
 
@@ -144,6 +146,7 @@ Could not compile `branches`.
 not automatically try to convert non-boolean types to a boolean here, unlike
 languages like Ruby or JavaScript. We must be explicit and always give if a
 boolean as its condition. If your intention is for the if code block to be run if a number is not equal to 0, for example, we would change the if expression to read:

+

Filename: src/main.rs

fn main() {
     let number = 3;
 
@@ -156,6 +159,7 @@ languages like Ruby or JavaScript. We must be explicit and always give if<
 

Multiple Conditions with else if

We can have multiple coniditions by combining if and else in an else if expression. For example:

+

Filename: src/main.rs

fn main() {
     let number = 5;
 
@@ -187,6 +191,7 @@ for these cases.

The last detail you need to learn about if is that it’s an expression. That means that we can use it on the right hand side of a let binding, for instance:

+

Filename: src/main.rs

fn main() {
     let condition = true;
     let number = if condition {
@@ -211,6 +216,7 @@ expression depends on which block of code executes. This means that the value
 that results from both arms of the if must be the same type; in the previous
 example, they were both i32 integers. But what happens if the types are
 mismatched, as in the following example?

+

Filename: src/main.rs

fn main() {
     let condition = true;
 
@@ -259,6 +265,7 @@ in.

forever or until we explicitly tell it to stop.

For an example, change the src/main.rs file in your loops directory to look like this:

+

Filename: src/main.rs

fn main() {
     loop {
         println!("again!");
@@ -294,6 +301,7 @@ that now if you'd like!

for it, called a while loop. Here's an example using while: this program loops three times, counting down each time. Finally, after the loop, it prints another message, then exits:

+

Filename: src/main.rs

fn main() {
     let mut number = 3;
 
@@ -312,6 +320,7 @@ this code; otherwise, exit the loop.

Looping Through a Collection with for

We could use this while construct to loop over the elements of a collection, like an array. For example:

+

Filename: src/main.rs

fn main() {
     let a = [10, 20, 30, 40, 50];
     let mut index = 0;
@@ -344,6 +353,7 @@ perform the conditional check on every element on every iteration through the
 loop.

As a more efficient alternative, we can use a for loop and execute some code for each item in a collection. A for loop looks like this:

+

Filename: src/main.rs

fn main() {
     let a = [10, 20, 30, 40, 50];
 
@@ -371,6 +381,7 @@ is a type provided by the standard library that generates numbers starting from
 one number and ending before another number. Here's what the countdown would
 look like with a for loop, and using another method we haven't yet talked
 about, .rev(), to reverse the range:

+

Filename: src/main.rs

fn main() {
     for number in (1..4).rev() {
         println!("{}!", number);
diff --git a/ch04-01-ownership.html b/ch04-01-ownership.html
index cb70ba849..fd0998ffe 100644
--- a/ch04-01-ownership.html
+++ b/ch04-01-ownership.html
@@ -316,6 +316,7 @@ is Copy, but (i32, String) is not.
 
 

Ownership and functions

Passing a value to a function has similar semantics as assigning it:

+

Filename: src/main.rs

fn main() {
     let s = String::from("hello");
 
@@ -337,6 +338,7 @@ fn makes_copy(some_integer: i32) {
 

Passing a binding to a function will move or copy, just like assignment. Here’s the same example, but with some annotations showing where things go into and out of scope:

+

Filename: src/main.rs

fn main() {
     let s = String::from("hello");  // s goes into scope.
 
@@ -365,6 +367,7 @@ would throw a compile-time error. These static checks protect us from mistakes.
 Try adding code to main that uses s and x to see where you can use them
 and where the ownership rules prevent you from doing so.

Returning values can also transfer ownership:

+

Filename: src/main.rs

fn main() {
     let s1 = gives_ownership();
 
@@ -385,6 +388,7 @@ fn takes_and_gives_back(a_string: String) -> String {
 }
 

With similiar annotations:

+

Filename: src/main.rs

fn main() {
     let s1 = gives_ownership();         // gives_ownership moves its return
                                         // value into s1.
@@ -421,6 +425,7 @@ a value but not take ownership? It’s quite annoying that anything we pass in
 also needs to be passed back if we want to use it again, in addition to any
 data resulting from the body of the function that we might want to return as
 well. It's possible to return multiple values, using a tuple, like this:

+

Filename: src/main.rs

fn main() {
     let s1 = String::from("hello");
 
diff --git a/ch04-02-references-and-borrowing.html b/ch04-02-references-and-borrowing.html
index 549393f52..462a1ab98 100644
--- a/ch04-02-references-and-borrowing.html
+++ b/ch04-02-references-and-borrowing.html
@@ -70,6 +70,7 @@
                     

References and Borrowing

At the end of the last section, we had some example Rust that wasn’t very good. Here it is again:

+

Filename: src/main.rs

fn main() {
     let s1 = String::from("hello");
 
@@ -88,6 +89,7 @@ fn calculate_length(s: String) -> (String, usize) {
 function so that we can still use it there, since it was moved when we called
 calculate_length().

There is a better way. It looks like this:

+

Filename: src/main.rs

fn main() {
     let s1 = String::from("hello");
 
@@ -140,6 +142,7 @@ with real life, if a person owns something, you can borrow it from them. When
 you’re done, you have to give it back.

Speaking of which, what if you try to modify something you borrow from me? Try this code out. Spoiler alert: it doesn’t work!

+

Filename: src/main.rs

fn main() {
     let s = String::from("hello");
 
@@ -161,6 +164,7 @@ fn change(some_string: &String) {
 allowed to modify something we have a reference to.

Mutable references

We can fix this bug! Just a small tweak:

+

Filename: src/main.rs

fn main() {
     let mut s = String::from("hello");
 
@@ -174,6 +178,7 @@ fn change(some_string: &mut String) {
 

First, we had to change s to be mut. Then we had to create a mutable reference with &mut s and accept a mutable reference with some_string: &mut String.

Mutable references have one big restriction, though. This code fails:

+

Filename: src/main.rs

let mut s = String::from("hello");
 
 let r1 = &mut s;
@@ -235,6 +240,7 @@ contrast, the compiler guarantees that references will never be dangling: if we
 have a reference to something, the compiler will ensure that it will not go
 out of scope before the reference does.

Let’s try to create a dangling reference:

+

Filename: src/main.rs

fn main() {
     let reference_to_nothing = dangle();
 }
diff --git a/ch04-03-slices.html b/ch04-03-slices.html
index 52744b59d..7b45759ef 100644
--- a/ch04-03-slices.html
+++ b/ch04-03-slices.html
@@ -82,6 +82,7 @@ then the whole string is a word, so the whole thing should be returned.

ownership, so this is fine. But what should we return? We don’t really have a way to talk about part of a string. We could return the index of the end of the word, though. Let’s try that:

+

Filename: src/main.rs

fn first_word(s: &String) -> usize {
     let bytes = s.as_bytes();
 
@@ -124,6 +125,7 @@ of the string, using s.len().

it’s only a meaningful number in the context of the &String. In other words, because it’s a separate value from the String, there’s no guarantee that it will still be valid in the future. Consider this:

+

Filename: src/main.rs

# fn first_word(s: &String) -> usize {
 #     let bytes = s.as_bytes();
 #
@@ -135,7 +137,7 @@ that it will still be valid in the future. Consider this:

# # s.len() # } - +# fn main() { let mut s = String::from("hello world"); @@ -199,6 +201,7 @@ let slice = &s[0..len]; let slice = &s[..];

With this in mind, let’s re-write first_word() to return a slice:

+

Filename: src/main.rs

fn first_word(s: &String) -> &str {
     let bytes = s.as_bytes();
 
@@ -220,6 +223,7 @@ slice and the number of elements in the slice.

We now have a straightforward API that’s much harder to mess up.

But what about our error condition from before? Slices also fix that. Using the slice version of first_word() will throw an error:

+

Filename: src/main.rs

fn main() {
     let mut s = String::from("hello world");
 
@@ -270,6 +274,7 @@ a string slice that’s the full length of a String, so we haven’
 the ability to talk about full Strings. And additionally, we can take
 string slices of string literals too, so this function is more useful, but
 with no loss of functionality:

+

Filename: src/main.rs

# fn first_word(s: &str) -> &str {
 #     let bytes = s.as_bytes();
 #
diff --git a/ch05-00-structs.html b/ch05-00-structs.html
index d474db03e..c198b8b90 100644
--- a/ch05-00-structs.html
+++ b/ch05-00-structs.html
@@ -84,6 +84,7 @@ $ cd points
 

Here’s a short program which calculates the distance between two points. Put it into your src/main.rs:

+

Filename: src/main.rs

fn main() {
     let x1 = 0.0;
     let y1 = 5.0;
@@ -147,6 +148,7 @@ in our program itself. It would be nicer if we had a way to group (x1, y1)
 and (x2, y2) together.

We’ve already discussed one way to do that: tuples. Here’s a version of our program which uses tuples:

+

Filename: src/main.rs

fn main() {
     let p1 = (0.0, 5.0);
 
@@ -206,6 +208,7 @@ let x = p1.x;
 

Let’s convert our program to use our Point struct. Here’s what it looks like now:

+

Filename: src/main.rs

#[derive(Debug,Copy,Clone)]
 struct Point {
     x: f64,
@@ -248,6 +251,7 @@ struct so that we can see its value while we are debugging our code.

So far, we’ve been printing values using {} in a println! macro. If we try that with a struct, however, by default, we'll get an error. Say we have the following program:

+

Filename: src/main.rs

struct Point {
     x: f64,
     y: f64,
@@ -286,6 +290,7 @@ implementation of Debug formatting to easily get this ability for t
 defined. To ask println! to use Debug formatting with our Point, we add
 the annotation to derive the trait and include :? in the print string, like
 this:

+

Filename: src/main.rs

#[derive(Debug)]
 struct Point {
     x: f64,
diff --git a/ch06-00-enums.html b/ch06-00-enums.html
index 8cfce0001..560dee26d 100644
--- a/ch06-00-enums.html
+++ b/ch06-00-enums.html
@@ -182,7 +182,7 @@ possibilities is that they are just like different kinds of struct definitions
 that you already know, except without the struct keyword and they are grouped
 together under the Message type. These structs could hold the same data that
 these enum variants hold:

-
struct QuitMessage; // unit struct
+
struct QuitMessage; // unit struct
 struct MoveMessage {
     x: i32,
     y: i32,
diff --git a/chXX-patterns.html b/chXX-patterns.html
index 405be600f..48319dbe5 100644
--- a/chXX-patterns.html
+++ b/chXX-patterns.html
@@ -80,6 +80,7 @@ any place where a pattern is used.

particularly humble form of pattern.

Multiple bindings

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

+

Filename: src/main.rs

fn main() {
     let (x, y) = (5, 6);
 
@@ -117,9 +118,7 @@ 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;
-}
+
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:

@@ -127,9 +126,7 @@ statement with a type annotation:

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);
-}
+
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.

@@ -267,14 +264,12 @@ match x { }

Ranges are usually used with integers or chars:

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

Guards

diff --git a/print.html b/print.html index 9f9654fa1..819f844aa 100644 --- a/print.html +++ b/print.html @@ -173,6 +173,7 @@ the .rs extension. If you’re using more than one word in your fil an underscore to separate them. For example, you'd use hello_world.rs rather than helloworld.rs.

Now open the main.rs file you just created, and type the following code:

+

Filename: main.rs

fn main() {
     println!("Hello, world!");
 }
@@ -303,6 +304,7 @@ different version control system, or no version control system, by using the
 --vcs flag.

Open up Cargo.toml in your text editor of choice. It should look something like this:

+

Filename: Cargo.toml

[package]
 name = "hello_cargo"
 version = "0.1.0"
@@ -327,6 +329,7 @@ depend on so that Cargo knows to download and compile those too. We won't need
 any other crates for this project, but we will in the guessing game tutorial in
 the next chapter.

Now let's look at src/main.rs:

+

Filename: src/main.rs

fn main() {
     println!("Hello, world!");
 }
@@ -360,6 +363,7 @@ Hello, world!
 

Bam! If all goes well, Hello, world! should print to the terminal once more.

Running cargo build for the first time also causes Cargo to create a new file at the top level called Cargo.lock, which looks like this:

+

Filename: Cargo.lock

[root]
 name = "hello_cargo"
 version = "0.1.0"
@@ -431,6 +435,7 @@ $ cd guessing_game
 

We pass the name of our project to cargo new, then the --bin flag, since we’re going to be making another binary like in Chapter 1.

Take a look at the generated Cargo.toml:

+

Filename: Cargo.toml

[package]
 name = "guessing_game"
 version = "0.1.0"
@@ -442,6 +447,7 @@ authors = ["Your Name <you@example.com>"]
 go ahead and fix that.

And as we saw in the last chapter, cargo new generates a "Hello, world!" for us. Check out src/main.rs:

+

Filename: src/main.rs

fn main() {
     println!("Hello, world!");
 }
@@ -460,6 +466,7 @@ file.

Processing a Guess

Let’s get to it! The first thing we need to do for our guessing game is allow our player to input a guess. Put this in your src/main.rs:

+

Filename: src/main.rs

use std::io;
 
 fn main() {
@@ -631,6 +638,7 @@ programs.

the code using rand, we need to modify our Cargo.toml. Open it up, and add this line at the bottom beneath the [dependencies] section header that Cargo created for you:

+

Filename: Cargo.toml

[dependencies]
 
 rand = "0.3.14"
@@ -701,6 +709,7 @@ that’s all we need to know. Cargo makes it really easy to re-use libraries, so
 Rustaceans are able to write smaller projects which are assembled out of a
 number of sub-packages.

Let’s get on to actually using rand. Here’s our next step:

+

Filename: src/main.rs

extern crate rand;
 
 use std::io;
@@ -772,6 +781,7 @@ You guessed: 5
 

Comparing guesses

Now that we’ve got user input, let’s compare our guess to the secret number. Here’s part of our next step. It won't quite compile yet though:

+

Filename: src/main.rs

extern crate rand;
 
 use std::io;
@@ -861,6 +871,7 @@ be the same type.

Ultimately, we want to convert the String we read as input into a real number type so that we can compare it to the guess numerically. We can do that with two more lines. Here’s our new program:

+

Filename: src/main.rs

extern crate rand;
 
 use std::io;
@@ -941,6 +952,7 @@ the secret number works, as well as guessing a number too small.

change that by adding loops!

Looping

The loop keyword gives us an infinite loop. Let’s add that in:

+

Filename: src/main.rs

extern crate rand;
 
 use std::io;
@@ -1003,6 +1015,7 @@ error: Process didn't exit successfully: `target/debug/guess` (exit code: 101)
 

Ha! quit actually quits. As does any other non-number input. Well, this is suboptimal to say the least. First, let’s actually quit when you win the game:

+

Filename: src/main.rs

extern crate rand;
 
 use std::io;
@@ -1045,6 +1058,7 @@ win. Exiting the loop also means exiting the program, since the loop is the last
 thing in main(). We have another tweak to make: when someone inputs a
 non-number, we don’t want to quit, we want to ignore it. We can do that
 like this:

+

Filename: src/main.rs

extern crate rand;
 
 use std::io;
@@ -1126,6 +1140,7 @@ You win!
 think of what it is? That’s right, we don’t want to print out the secret
 number. It was good for testing, but it kind of ruins the game. Here’s our
 final source:

+

Filename: src/main.rs

extern crate rand;
 
 use std::io;
@@ -1201,6 +1216,7 @@ in. From there, run these commands:

$ cd bindings

Then open src/main.rs and replace its code with the following:

+

Filename: src/main.rs

fn main() {
     let x = 5;
     println!("The value of x is: {}", x);
@@ -1283,6 +1299,7 @@ addition to telling the compiler it should allow this value to be changed, it
 conveys intent to future readers of the code and says that other parts of the
 code will be changing this value.

For example, change the program you just wrote to the following:

+

Filename: src/main.rs

fn main() {
     let mut x = 5;
     println!("The value of x is: {}", x);
@@ -1311,6 +1328,7 @@ that the second binding's value is what you will see when you use the variable
 after the second binding. This can be useful if you’d like to perform a few
 transformations on a value, but have the binding be immutable after those
 transformations have been completed. For example:

+

Filename: src/main.rs

fn main() {
     let x = 5;
 
@@ -1397,6 +1415,7 @@ possible to use an f64 on 32 bit systems, but it will be slower tha
 for better precision is a reasonable initial choice, and you should benchmark
 your code if you suspect floating-point size is a problem in your case.

Here's an example showing floating-point numbers in action:

+

Filename: src/main.rs

fn main() {
     let x = 2.0; // f64
 
@@ -1409,6 +1428,7 @@ your code if you suspect floating-point size is a problem in your case.

Rust supports the usual basic mathematic operations you’d expect for all of these number types: addition, subtraction, multiplication, division, and modulo. This code shows how you'd use each one in a let statement:

+

Filename: src/main.rs

fn main() {
     // addition
     let sum = 5 + 10;
@@ -1432,6 +1452,7 @@ to a single value, which is then bound to a variable.

As in most other programming languages, a boolean type in Rust has two possible values: true and false. The boolean type in Rust is specified with bool. For example:

+

Filename: src/main.rs

fn main() {
     let t = true;
 
@@ -1445,6 +1466,7 @@ section of this chapter.

So far we’ve only worked with numbers, but Rust supports letters too. Rust’s char type is the language's most primitive alphabetic type, and this code shows one way to use it:

+

Filename: src/main.rs

fn main() {
    let c = 'z';
    let z = 'ℤ';
@@ -1464,6 +1486,7 @@ all unicode code points at http://www.unicode.org/charts/.

The Byte Type

You can work with the bytes of data directly. Byte literals can be created from the ASCII characters using b and single quotes:

+

Filename: src/main.rs

fn main() {
     let byte = b'a';
     println!("byte is {}", byte);
@@ -1482,6 +1505,7 @@ general way of grouping together some number of other values with distinct
 types into one compound type.

We create a tuple by writing a comma-separated list of values inside parentheses. Each position in the tuple has a distinct type, as in this example:

+

Filename: src/main.rs

fn main() {
     let tup: (i32, f64, u8) = (500, 6.4, 1);
 }
@@ -1490,6 +1514,7 @@ parentheses. Each position in the tuple has a distinct type, as in this example:
 name tup to the entire tuple, emphasizing the fact that a tuple is considered
 a single compound element. We could then use pattern matching to destructure
 this tuple value, like this:

+

Filename: src/main.rs

fn main() {
     let tup: (i32, f64, u8) = (500, 6.4, 1);
 
@@ -1507,6 +1532,7 @@ the single tuple into three parts.

In addition to destructuring through pattern matching, we can also access a tuple element directly by using a period (.) followed by the index of the value we want to access. For example:

+

Filename: src/main.rs

fn main() {
     let x: (i32, f64, u8) = (500, 6.4, 1);
 
@@ -1527,6 +1553,7 @@ Arrays in Rust are different than arrays in some other languages because arrays
 in Rust have a fixed length-- once declared, they cannot grow or shrink in size.

In Rust, the values going into an array are written as a comma separated list inside square brackets:

+

Filename: src/main.rs

fn main() {
     let a = [1, 2, 3, 4, 5];
 }
@@ -1539,6 +1566,7 @@ and we'll discuss them in more detail in chapter XX.

Accessing Array Elements

An array is a single chunk of memory, allocated on the stack. We can access elements of an array using indexing, like this:

+

Filename: src/main.rs

fn main() {
     let a = [1, 2, 3, 4, 5];
 
@@ -1555,6 +1583,7 @@ values.

Invalid array element access

What happens if you try to access an element of an array past the end of the array? Say we changed our program to:

+

Filename: src/main.rs

fn main() {
     let a = [1, 2, 3, 4, 5];
 
@@ -1591,6 +1620,7 @@ snake case, all letters are lower case, and there are underscores separating
 words. (Rust also uses snake case for the names of variable bindings; we just
 haven't used any variable bindings with enough letters to need underscores
 yet). Here's a program containing an example function definition:

+

Filename: src/main.rs

fn main() {
     println!("Hello, world!");
 
@@ -1630,6 +1660,7 @@ message is printed.

Function Arguments

Functions can also take arguments. The following rewritten version of another_function() shows what arguments look like in Rust:

+

Filename: src/main.rs

fn main() {
     another_function(5);
 }
@@ -1654,6 +1685,7 @@ definitions means the compiler almost never needs you to use them elsewhere in
 the code in order to figure out what you mean.

When you want a function to have multiple arguments, just separate them inside the function signature with commas, like this:

+

Filename: src/main.rs

fn main() {
     another_function(5, 6);
 }
@@ -1691,6 +1723,7 @@ instructions that perform some action and do not return a value. Expressions
 evaluate to a resulting value. Let's look at some examples.

Let bindings are statements. They instruct the program to create a binding name and assign a value to it. let y = 6; in this example is a statement:

+

Filename: src/main.rs

fn main() {
     let y = 6;
 }
@@ -1699,6 +1732,7 @@ name and assign a value to it. let y = 6; in this example is a stat
 statement as well.

Statements do not return values themselves. Therefore, you can’t assign a let binding to another binding, as this code tries to do:

+

Filename: src/main.rs

fn main() {
     let x = (let y = 6);
 }
@@ -1728,6 +1762,7 @@ Consider a simple math operation, like this:

can be part of statements-- in the previous example that had the statement let y = 6;, 6 is an expression that evaluates to the value 6. Calling a function is an expression. Calling a macro is an expression. The block that we use to create new scopes, {}, is an expression, for example:

+

Filename: src/main.rs

fn main() {
     let x = 5;
 
@@ -1759,6 +1794,7 @@ return values, but we do declare their type, after an arrow (->)
 the "return value of the function” is synonymous with “the value of the final
 expression in the block of the body of a function.” Here's an example of a
 function that returns a value:

+

Filename: src/main.rs

fn five() -> i32 {
     5
 }
@@ -1789,6 +1825,7 @@ use the return value of a function to initialize a binding.

arguments and defines the type of the return value, but the body of the function is a lonely 5 with no semicolon because it is an expression whose value we want to return. Let's look at another example:

+

Filename: src/main.rs

fn main() {
     let x = plus_one(5);
 
@@ -1802,6 +1839,7 @@ fn plus_one(x: i32) -> i32 {
 

Running this code will print The value of x is: 6. What happens if we put a semicolon at the end of the line containing x + 1, changing it from an expression to a statement?

+

Filename: src/main.rs

fn main() {
     let x = plus_one(5);
 
@@ -1849,11 +1887,13 @@ include // on each line, like this:

// explain what’s going on.

Comments can also be placed at the end of lines of code:

+

Filename: src/main.rs

fn main() {
     let lucky_number = 7; // I’m feeling lucky today.
 }
 

But you’ll more often see them above, like so:

+

Filename: src/main.rs

fn main() {
     // I’m feeling lucky today.
     let lucky_number = 7;
@@ -1877,6 +1917,7 @@ $ cd branches
 

Write this sample program using if and save it in the branches directory in src/main.rs:

+

Filename: src/main.rs

fn main() {
     let number = 3;
 
@@ -1912,6 +1953,7 @@ condition was false
 

It’s also worth noting that condition here must be a bool. To see what happens if the condition isn't a bool, try running this code:

+

Filename: src/main.rs

fn main() {
     let number = 3;
 
@@ -1937,6 +1979,7 @@ Could not compile `branches`.
 not automatically try to convert non-boolean types to a boolean here, unlike
 languages like Ruby or JavaScript. We must be explicit and always give if a
 boolean as its condition. If your intention is for the if code block to be run if a number is not equal to 0, for example, we would change the if expression to read:

+

Filename: src/main.rs

fn main() {
     let number = 3;
 
@@ -1949,6 +1992,7 @@ languages like Ruby or JavaScript. We must be explicit and always give if<
 

Multiple Conditions with else if

We can have multiple coniditions by combining if and else in an else if expression. For example:

+

Filename: src/main.rs

fn main() {
     let number = 5;
 
@@ -1980,6 +2024,7 @@ for these cases.

The last detail you need to learn about if is that it’s an expression. That means that we can use it on the right hand side of a let binding, for instance:

+

Filename: src/main.rs

fn main() {
     let condition = true;
     let number = if condition {
@@ -2004,6 +2049,7 @@ expression depends on which block of code executes. This means that the value
 that results from both arms of the if must be the same type; in the previous
 example, they were both i32 integers. But what happens if the types are
 mismatched, as in the following example?

+

Filename: src/main.rs

fn main() {
     let condition = true;
 
@@ -2052,6 +2098,7 @@ in.

forever or until we explicitly tell it to stop.

For an example, change the src/main.rs file in your loops directory to look like this:

+

Filename: src/main.rs

fn main() {
     loop {
         println!("again!");
@@ -2087,6 +2134,7 @@ that now if you'd like!

for it, called a while loop. Here's an example using while: this program loops three times, counting down each time. Finally, after the loop, it prints another message, then exits:

+

Filename: src/main.rs

fn main() {
     let mut number = 3;
 
@@ -2105,6 +2153,7 @@ this code; otherwise, exit the loop.

Looping Through a Collection with for

We could use this while construct to loop over the elements of a collection, like an array. For example:

+

Filename: src/main.rs

fn main() {
     let a = [10, 20, 30, 40, 50];
     let mut index = 0;
@@ -2137,6 +2186,7 @@ perform the conditional check on every element on every iteration through the
 loop.

As a more efficient alternative, we can use a for loop and execute some code for each item in a collection. A for loop looks like this:

+

Filename: src/main.rs

fn main() {
     let a = [10, 20, 30, 40, 50];
 
@@ -2164,6 +2214,7 @@ is a type provided by the standard library that generates numbers starting from
 one number and ending before another number. Here's what the countdown would
 look like with a for loop, and using another method we haven't yet talked
 about, .rev(), to reverse the range:

+

Filename: src/main.rs

fn main() {
     for number in (1..4).rev() {
         println!("{}!", number);
@@ -2428,6 +2479,7 @@ is Copy, but (i32, String) is not.
 
 

Ownership and functions

Passing a value to a function has similar semantics as assigning it:

+

Filename: src/main.rs

fn main() {
     let s = String::from("hello");
 
@@ -2449,6 +2501,7 @@ fn makes_copy(some_integer: i32) {
 

Passing a binding to a function will move or copy, just like assignment. Here’s the same example, but with some annotations showing where things go into and out of scope:

+

Filename: src/main.rs

fn main() {
     let s = String::from("hello");  // s goes into scope.
 
@@ -2477,6 +2530,7 @@ would throw a compile-time error. These static checks protect us from mistakes.
 Try adding code to main that uses s and x to see where you can use them
 and where the ownership rules prevent you from doing so.

Returning values can also transfer ownership:

+

Filename: src/main.rs

fn main() {
     let s1 = gives_ownership();
 
@@ -2497,6 +2551,7 @@ fn takes_and_gives_back(a_string: String) -> String {
 }
 

With similiar annotations:

+

Filename: src/main.rs

fn main() {
     let s1 = gives_ownership();         // gives_ownership moves its return
                                         // value into s1.
@@ -2533,6 +2588,7 @@ a value but not take ownership? It’s quite annoying that anything we pass in
 also needs to be passed back if we want to use it again, in addition to any
 data resulting from the body of the function that we might want to return as
 well. It's possible to return multiple values, using a tuple, like this:

+

Filename: src/main.rs

fn main() {
     let s1 = String::from("hello");
 
@@ -2553,6 +2609,7 @@ next section is about.

References and Borrowing

At the end of the last section, we had some example Rust that wasn’t very good. Here it is again:

+

Filename: src/main.rs

fn main() {
     let s1 = String::from("hello");
 
@@ -2571,6 +2628,7 @@ fn calculate_length(s: String) -> (String, usize) {
 function so that we can still use it there, since it was moved when we called
 calculate_length().

There is a better way. It looks like this:

+

Filename: src/main.rs

fn main() {
     let s1 = String::from("hello");
 
@@ -2623,6 +2681,7 @@ with real life, if a person owns something, you can borrow it from them. When
 you’re done, you have to give it back.

Speaking of which, what if you try to modify something you borrow from me? Try this code out. Spoiler alert: it doesn’t work!

+

Filename: src/main.rs

fn main() {
     let s = String::from("hello");
 
@@ -2644,6 +2703,7 @@ fn change(some_string: &String) {
 allowed to modify something we have a reference to.

Mutable references

We can fix this bug! Just a small tweak:

+

Filename: src/main.rs

fn main() {
     let mut s = String::from("hello");
 
@@ -2657,6 +2717,7 @@ fn change(some_string: &mut String) {
 

First, we had to change s to be mut. Then we had to create a mutable reference with &mut s and accept a mutable reference with some_string: &mut String.

Mutable references have one big restriction, though. This code fails:

+

Filename: src/main.rs

let mut s = String::from("hello");
 
 let r1 = &mut s;
@@ -2718,6 +2779,7 @@ contrast, the compiler guarantees that references will never be dangling: if we
 have a reference to something, the compiler will ensure that it will not go
 out of scope before the reference does.

Let’s try to create a dangling reference:

+

Filename: src/main.rs

fn main() {
     let reference_to_nothing = dangle();
 }
@@ -2795,6 +2857,7 @@ then the whole string is a word, so the whole thing should be returned.

ownership, so this is fine. But what should we return? We don’t really have a way to talk about part of a string. We could return the index of the end of the word, though. Let’s try that:

+

Filename: src/main.rs

fn first_word(s: &String) -> usize {
     let bytes = s.as_bytes();
 
@@ -2837,6 +2900,7 @@ of the string, using s.len().

it’s only a meaningful number in the context of the &String. In other words, because it’s a separate value from the String, there’s no guarantee that it will still be valid in the future. Consider this:

+

Filename: src/main.rs

# fn first_word(s: &String) -> usize {
 #     let bytes = s.as_bytes();
 #
@@ -2848,7 +2912,7 @@ that it will still be valid in the future. Consider this:

# # s.len() # } - +# fn main() { let mut s = String::from("hello world"); @@ -2912,6 +2976,7 @@ let slice = &s[0..len]; let slice = &s[..];

With this in mind, let’s re-write first_word() to return a slice:

+

Filename: src/main.rs

fn first_word(s: &String) -> &str {
     let bytes = s.as_bytes();
 
@@ -2933,6 +2998,7 @@ slice and the number of elements in the slice.

We now have a straightforward API that’s much harder to mess up.

But what about our error condition from before? Slices also fix that. Using the slice version of first_word() will throw an error:

+

Filename: src/main.rs

fn main() {
     let mut s = String::from("hello world");
 
@@ -2983,6 +3049,7 @@ a string slice that’s the full length of a String, so we haven’
 the ability to talk about full Strings. And additionally, we can take
 string slices of string literals too, so this function is more useful, but
 with no loss of functionality:

+

Filename: src/main.rs

# fn first_word(s: &str) -> &str {
 #     let bytes = s.as_bytes();
 #
@@ -3042,6 +3109,7 @@ $ cd points
 

Here’s a short program which calculates the distance between two points. Put it into your src/main.rs:

+

Filename: src/main.rs

fn main() {
     let x1 = 0.0;
     let y1 = 5.0;
@@ -3105,6 +3173,7 @@ in our program itself. It would be nicer if we had a way to group (x1, y1)
 and (x2, y2) together.

We’ve already discussed one way to do that: tuples. Here’s a version of our program which uses tuples:

+

Filename: src/main.rs

fn main() {
     let p1 = (0.0, 5.0);
 
@@ -3164,6 +3233,7 @@ let x = p1.x;
 

Let’s convert our program to use our Point struct. Here’s what it looks like now:

+

Filename: src/main.rs

#[derive(Debug,Copy,Clone)]
 struct Point {
     x: f64,
@@ -3206,6 +3276,7 @@ struct so that we can see its value while we are debugging our code.

So far, we’ve been printing values using {} in a println! macro. If we try that with a struct, however, by default, we'll get an error. Say we have the following program:

+

Filename: src/main.rs

struct Point {
     x: f64,
     y: f64,
@@ -3244,6 +3315,7 @@ implementation of Debug formatting to easily get this ability for t
 defined. To ask println! to use Debug formatting with our Point, we add
 the annotation to derive the trait and include :? in the print string, like
 this:

+

Filename: src/main.rs

#[derive(Debug)]
 struct Point {
     x: f64,
@@ -3520,7 +3592,7 @@ possibilities is that they are just like different kinds of struct definitions
 that you already know, except without the struct keyword and they are grouped
 together under the Message type. These structs could hold the same data that
 these enum variants hold:

-
struct QuitMessage; // unit struct
+
struct QuitMessage; // unit struct
 struct MoveMessage {
     x: i32,
     y: i32,
@@ -4038,6 +4110,7 @@ any place where a pattern is used.

particularly humble form of pattern.

Multiple bindings

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

+

Filename: src/main.rs

fn main() {
     let (x, y) = (5, 6);
 
@@ -4075,9 +4148,7 @@ 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;
-}
+
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:

@@ -4085,9 +4156,7 @@ statement with a type annotation:

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);
-}
+
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.

@@ -4225,14 +4294,12 @@ match x { }

Ranges are usually used with integers or chars:

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

Guards