From 03f849330c9de80c8701f641be94d3c68ac962a6 Mon Sep 17 00:00:00 2001 From: Steve Klabnik Date: Fri, 19 Aug 2016 15:43:15 +0000 Subject: [PATCH] rebuild pages at b6165b3 --- ch01-02-hello-world.html | 2 +- ch02-00-guessing-game-tutorial.html | 42 ++++---- ch03-01-variable-bindings-and-mutability.html | 2 +- ch03-02-data-types.html | 2 +- ch03-05-control-flow.html | 4 +- ch04-01-ownership.html | 14 +-- ch04-02-references-and-borrowing.html | 6 +- ch05-00-structs.html | 2 +- ch05-01-method-syntax.html | 12 +-- ch06-01-option.html | 4 +- chXX-patterns.html | 4 +- chZZ-generics.html | 6 +- print.html | 100 +++++++++--------- 13 files changed, 100 insertions(+), 100 deletions(-) diff --git a/ch01-02-hello-world.html b/ch01-02-hello-world.html index d719c85e8..2fb25beac 100644 --- a/ch01-02-hello-world.html +++ b/ch01-02-hello-world.html @@ -190,7 +190,7 @@ programs.

manage their Rust projects because it makes a lot of tasks easier. For example, Cargo takes care of building your code, downloading the libraries your code depends on, and building those libraries. We call libraries your code needs -‘dependencies’ since your code depends on them.

+dependencies since your code depends on them.

The simplest Rust programs, like the one we've written so far, don’t have any dependencies, so right now, you'd only be using the part of Cargo that can take care of building your code. As you write more complex Rust programs, you’ll diff --git a/ch02-00-guessing-game-tutorial.html b/ch02-00-guessing-game-tutorial.html index 3e0a1a09a..640d7e38f 100644 --- a/ch02-00-guessing-game-tutorial.html +++ b/ch02-00-guessing-game-tutorial.html @@ -91,7 +91,7 @@ authors = ["Your Name <you@example.com>"]

If the authors information that Cargo got from your environment is not correct, go ahead and fix that.

-

And as we saw in the last chapter, cargo new generates a ‘Hello, world!’ for +

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

fn main() {
     println!("Hello, world!");
@@ -131,7 +131,7 @@ fn main() {
 

We’ll need to take user input and then print the result as output. As such, we need the io library from the standard library. Rust only imports a few things -by default into every program, the ‘prelude’. If it’s not in the +by default into every program, the prelude. If it’s not in the prelude, you’ll have to use it directly. Using the std::io library gets you a number of useful io-related things, so that's what we've done here.

fn main() {
@@ -150,11 +150,11 @@ prints a string to the screen.

Now we’re getting interesting! There’s a lot going on in this little line. The first thing to notice is that this is a let statement, which is -used to create what are called ‘variable bindings’. Here's an example:

+used to create what are called variable bindings. Here's an example:

let foo = bar;
 

This will create a new binding named foo, and bind it to the value bar. In -many languages, this is called a ‘variable’, but Rust’s variable bindings have +many languages, this is called a variable, but Rust’s variable bindings have a few tricks up their sleeves.

For example, they’re immutable by default. That’s why our example uses mut: it makes a binding mutable, rather than immutable.

@@ -168,10 +168,10 @@ everything in comments.

bound to: String::new().

String is a string type, provided by the standard library. A String is a growable, UTF-8 encoded bit of text.

-

The ::new() syntax uses :: because this is an ‘associated function’ of +

The ::new() syntax uses :: because this is an associated function of a particular type. That is to say, it’s associated with String itself, rather than a particular instance of a String. Some languages call this a -‘static method’.

+static method.

This function is named new(), because it creates a new, empty String. You’ll find a new() function on many types, as it’s a common name for making a new value of some kind.

@@ -195,7 +195,7 @@ terminal. More specifically, a std::io::St passing one argument to read_line(): &mut guess.

Remember how we bound guess above? We said it was mutable. However, read_line doesn’t take a String as an argument: it takes a &mut String. -The & is the feature of Rust called a ‘reference’, which allows you to have +The & is the feature of Rust called a reference, which allows you to have multiple ways to access one piece of data in order to reduce copying. References are a complex feature, as one of Rust’s major selling points is how safe and easy it is to use references. We don’t need to know a lot of those @@ -274,9 +274,9 @@ print it back out.

Generating a secret number

Next, we need to generate a secret number. Rust does not yet include random number functionality in its standard library. The Rust team does, however, -provide a rand crate. A ‘crate’ is a package of Rust code. -We’ve been building a ‘binary crate’, which is an executable. rand is a -‘library crate’, which contains code that’s intended to be used with other +provide a rand crate. A crate is a package of Rust code. +We’ve been building a binary crate, which is an executable. rand is a +library crate, which contains code that’s intended to be used with other programs.

Using external crates is where Cargo really shines. Before we can write the code using rand, we need to modify our Cargo.toml. Open it up, and @@ -339,9 +339,9 @@ again. This lets you have a repeatable build automatically. In other words, we’ll stay at 0.3.14 until we explicitly upgrade, and so will anyone who we share our code with, thanks to the lock file.

What about when we do want to use v0.3.15? Cargo has another command, -update, which says ‘ignore the Cargo.lock file and figure out all the +update, which says "ignore the Cargo.lock file and figure out all the latest versions that fit what we’ve specified in Cargo.toml. If that works, -write those versions out to the lock file’. But by default, Cargo will only +write those versions out to the lock file". But by default, Cargo will only look for versions larger than 0.3.0 and smaller than 0.4.0. If we want to move to 0.4.x, we’d have to update what is in the Cargo.toml file. When we do, the next time we cargo build, Cargo will update the index and re-evaluate @@ -380,7 +380,7 @@ dependency. This also does the equivalent of a use rand; as well, s call anything in the rand crate by prefixing it with rand::.

Next, we added another use line: use rand::Rng. We’re going to use a method in a moment, and it requires that Rng be in scope to work. The basic -idea is this: methods are defined on something called ‘traits’, and for the +idea is this: methods are defined on something called traits, and for the method to work, it needs the trait to be in scope. For more about the details, read the traits section in Chapter XX.

There are two other lines we added, in the middle:

@@ -465,7 +465,7 @@ use that type:

a reference to the thing you want to compare it to. It returns the Ordering type we imported with the use statement earlier. We use a match statement to determine exactly what kind of Ordering it is. Ordering is an -enum, short for ‘enumeration’, which looks like this:

+enum, short for enumeration, which looks like this:

enum Foo {
     Bar,
     Baz,
@@ -476,7 +476,7 @@ statement to determine exactly what kind of Ordering it is. O
 namespace for a particular enum variant.

The Ordering enum has three possible variants: Less, Equal, and Greater. The match statement takes a value of a type and lets you -create an ‘arm’ for each possible value. An arm is made up of a pattern and the +create an arm for each possible value. An arm is made up of a pattern and the code that we should execute if the pattern matches the value of the type. Since we have three types of Ordering, we have three arms:

match guess.cmp(&secret_number) {
@@ -499,7 +499,7 @@ src/main.rs:23:21: 23:35 note:    found type `&_`
 error: aborting due to previous error
 Could not compile `guessing_game`.
 
-

Whew! This is a big error. The core of it is that we have ‘mismatched types’. +

Whew! This is a big error. The core of it is that we have mismatched types. Rust has a strong, static type system. However, it also has type inference. When we wrote let guess = String::new(), Rust was able to infer that guess should be a String, so it doesn’t make us write out the type. With our @@ -549,7 +549,7 @@ fn main() { .expect("Please type a number!");

Wait a minute, didn't we already have a guess? We do, but Rust allows us -to ‘shadow’ the previous guess with a new one. This is often used in this +to shadow the previous guess with a new one. This is often used in this exact situation, where guess starts as a String, but we want to convert it to a u32. Shadowing lets us re-use the guess name rather than forcing us to come up with two unique names like guess_str and guess or something @@ -560,8 +560,8 @@ else.

Here, guess refers to the old guess, the one that was a String with our input in it. The trim() method on Strings will eliminate any white space at the beginning and end of our string. This is important, as we had to press the -‘return’ key to satisfy read_line(). If we type 5 and hit return, guess -looks like this: 5\n. The \n represents ‘newline’, the enter key. trim() +"return" key to satisfy read_line(). If we type 5 and hit return, guess +looks like this: 5\n. The \n represents "newline", the return key. trim() gets rid of this, leaving our string with only the 5.

The parse() method on strings parses a string into some kind of number. Since it can parse a variety of numbers, we need to give Rust a hint as @@ -741,8 +741,8 @@ fn main() { Err(_) => continue, }; -

This is how you generally move from ‘crash on error’ to ‘actually handle the -error’: by switching from expect() to a match statement. A Result is the +

This is how you generally move from "crash on error" to "actually handle the +error": by switching from expect() to a match statement. A Result is the return type of parse(). Result is an enum like Ordering, but in this case, each variant has some data associated with it. Ok is a success, and Err is a failure. Each contains more information: in this case, the diff --git a/ch03-01-variable-bindings-and-mutability.html b/ch03-01-variable-bindings-and-mutability.html index c1ba36b4d..a9a50a048 100644 --- a/ch03-01-variable-bindings-and-mutability.html +++ b/ch03-01-variable-bindings-and-mutability.html @@ -187,7 +187,7 @@ depends on the tradeoffs you want to make in your situation.

Shadowing

As we saw in the guessing game tutorial, we can declare new bindings with the same name as a previous binding, and the new binding shadows the previous -binding. We say that the first binding is ‘shadowed’ by the second, which means +binding. We say that the first binding is shadowed by the second, which means 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 diff --git a/ch03-02-data-types.html b/ch03-02-data-types.html index 6ced2aa21..be4059661 100644 --- a/ch03-02-data-types.html +++ b/ch03-02-data-types.html @@ -232,7 +232,7 @@ this tuple value, like this:

In this program, we first create a tuple and bind it to the name tup. We then use a pattern with let to take tup and turn it into three separate -bindings, x, y, and z. This is called ‘destructuring’, because it breaks +bindings, x, y, and z. This is called destructuring, because it breaks the single tuple into three parts.

Finally, we print the value of y, which is 6.4.

Tuple Indexing

diff --git a/ch03-05-control-flow.html b/ch03-05-control-flow.html index 864082807..2aba19a9b 100644 --- a/ch03-05-control-flow.html +++ b/ch03-05-control-flow.html @@ -98,7 +98,7 @@ $ cd branches case, our condition is checking if our variable binding number has a value that is less than 5. The block of code we want to execute if the condition is true goes immediately after the condition, inside curly braces. These blocks -are sometimes called ‘arms’. We can optionally also include an else +are sometimes called arms. We can optionally also include an else statement, which we have chosen to do here. else gives the program a block of code to execute should condition evaluate to false.

Try running this code, and you should see output like this:

@@ -229,7 +229,7 @@ single type. If we try to run this, we’ll get an error:

   Compiling branches v0.1.0 (file:///projects/branches)
 src/main.rs:4:18: 8:6 error: if and else have incompatible types:
  expected `_`,
-    found `&‘static str`
+    found `&'static str`
 (expected integral variable,
     found &-ptr) [E0308]
 src/main.rs:4     let number = if condition {
diff --git a/ch04-01-ownership.html b/ch04-01-ownership.html
index 2d3ae61c2..cb70ba849 100644
--- a/ch04-01-ownership.html
+++ b/ch04-01-ownership.html
@@ -68,7 +68,7 @@
 
                 

Ownership

-

Rust’s central feature is called ‘ownership’. It is a feature that is +

Rust’s central feature is called ownership. It is a feature that is straightforward to explain, but has deep implications for the rest of the language.

All programs have to manage the way they use a computer's memory while running. @@ -103,8 +103,8 @@ at which it’s declared until the end of the current scope. That is:

In other words, there are two important points in time here:

At this point, things are similar to other programming languages. Now let’s build on top of this understanding by introducing the String type.

@@ -147,8 +147,8 @@ done with our String.

That first part is done by us: when we call String::from(), its implementation requests the memory it needs. This is pretty much universal in programming languages.

-

The second case, however, is different. In languages with a garbage collector -(‘GC’), the GC will keep track and clean up memory that isn't being used +

The second case, however, is different. In languages with a garbage collector +(GC), the GC will keep track and clean up memory that isn't being used anymore, and we, as the programmer, don’t need to think about it. Without GC, it’s our responsibility to identify when memory is no longer being used and call code to explicitly return it, just as we did to request it. Doing this @@ -257,13 +257,13 @@ alone will free the memory, and we’re done.

This leads us to the Ownership Rules:

    -
  1. Each value in Rust has a variable binding that’s called its ‘owner’.
  2. +
  3. Each value in Rust has a variable binding that’s called its owner.
  4. There can only be one owner at a time.
  5. When the owner goes out of scope, the value will be drop()ped.

Furthermore, there’s a design choice that’s implied by this: Rust will never -automatically create ‘deep’ copies of your data. Therefore, any automatic +automatically create "deep" copies of your data. Therefore, any automatic copying can be assumed to be inexpensive.

Clone

But what if we do want to deeply copy the String’s data and not just the diff --git a/ch04-02-references-and-borrowing.html b/ch04-02-references-and-borrowing.html index 47e6f5e01..549393f52 100644 --- a/ch04-02-references-and-borrowing.html +++ b/ch04-02-references-and-borrowing.html @@ -106,7 +106,7 @@ fn calculate_length(s: &String) -> usize { function return value is gone. Next, note that we pass &s1 into calculate_length(), and in its definition, we take &String rather than String.

-

These &s are called ‘references’, and they allow you to refer to some value +

These &s are called references, and they allow you to refer to some value without taking ownership of it. Here’s a diagram:

DIAGRAM GOES HERE of a &String pointing at a String, with (ptr, len, capacity)

Let’s take a closer look at the function call here:

@@ -135,7 +135,7 @@ a reference as an argument. Let’s add some explanatory annotations:

we don’t drop what a reference points to when the reference goes out of scope. This lets us write functions which take references as arguments instead of the values themselves, so that we won’t need to return them to give back ownership.

-

There’s another word for what references do, and that’s ‘borrowing’. Just like +

There’s another word for what references do, and that’s borrowing. Just like 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 @@ -259,7 +259,7 @@ fn dangle() -> &String { error: aborting due to previous error

This error message refers to a feature we haven’t learned about yet, -‘lifetimes’. The message does contain the key to why this code is a problem, +lifetimes. The message does contain the key to why this code is a problem, though:

this function’s return type contains a borrowed value, but there is no value
 for it to be borrowed from
diff --git a/ch05-00-structs.html b/ch05-00-structs.html
index 30cd1313b..d474db03e 100644
--- a/ch05-00-structs.html
+++ b/ch05-00-structs.html
@@ -132,7 +132,7 @@ modules and namespaces in depth yet, but you can think of the powi()f64, the
 same as the type. The powi() function takes two arguments: the first is a
 number, and the second is the power that it raises that number to. In this
-case, the second number is an integer, hence the ‘i’ in its name. Similarly,
+case, the second number is an integer, hence the i in its name. Similarly,
 sqrt() is a function under the f64 module, which takes the square root of
 its argument.

Why structs?

diff --git a/ch05-01-method-syntax.html b/ch05-01-method-syntax.html index 4d6a649f8..cf62ce952 100644 --- a/ch05-01-method-syntax.html +++ b/ch05-01-method-syntax.html @@ -68,8 +68,8 @@

Method Syntax

-

In the last section on ownership, we made several references to ‘methods’. -Methods look like this:

+

In Chapter 4 when we discussed ownership, we made several references to +methods. Methods look like this:

let s1 = "hello";
 
 // call a method on s1
@@ -77,8 +77,8 @@ let s2 = s1.clone();
 
 println!("{}", s1);
 
-

The call to clone() is attached to s1 with a dot. This is called ‘method -syntax’, and it’s a way to call certain functions with a different style.

+

The call to clone() is attached to s1 with a dot. This is called method +syntax, and it’s a way to call certain functions with a different style.

Why have two ways to call functions? We’ll talk about some deeper reasons related to ownership in a moment, but one big reason is that methods look nicer when chained together:

@@ -95,7 +95,7 @@ x.f().g().h(); methods.

Defining methods

We can define methods with the impl keyword. impl is short for -‘implementation’. Doing so looks like this:

+implementation. Doing so looks like this:

#[derive(Debug,Copy,Clone)]
 struct Point {
     x: f64,
@@ -166,7 +166,7 @@ from using the value of self after the transformation happens.

When we defined distance(), we took both self and the other argument by reference. Yet, we needed a & for p2 but not p1. What gives?

-

This feature is called ‘automatic referencing’, and calling methods is one +

This feature is called automatic referencing, and calling methods is one of the few places in Rust that has behavior like this. Here’s how it works: when you call a method with self.(, Rust will automatically add in &s or &muts to match the signature. In other words, these are the same:

diff --git a/ch06-01-option.html b/ch06-01-option.html index 5356a78a3..2a775fe91 100644 --- a/ch06-01-option.html +++ b/ch06-01-option.html @@ -72,7 +72,7 @@ feature that we talked a little bit about in the previous chapter: generics.

Programming language design is often thought of as which features you include, but it's also about which features you leave out. Rust does not have a feature -that is in many other languages: 'null'. In languages with this feature, +that is in many other languages: null. In languages with this feature, variables can have two states: null or not-null.

The inventor of this concept has this to say:

@@ -117,7 +117,7 @@ let absent_number: Option<i32> = None;

Let's dig in. First, you'll notice that we used the <T> syntax when defining Option<T>: it's a generic enum. Option<T> has two variants: Some, which contains a T, and None, which has no data associated with it. In some -sense, None means 'null', and Some means 'not null'. So why is this any +sense, None means "null", and Some means "not null". So why is this any better than null?

In short, because Option<T> and T are different types. That's a bit too short though. Here's an example:

diff --git a/chXX-patterns.html b/chXX-patterns.html index 750e30091..405be600f 100644 --- a/chXX-patterns.html +++ b/chXX-patterns.html @@ -111,12 +111,12 @@ to y. We could have used two let statements as well:

Type annotations

-

Most of the time, Rust uses ‘type inference’, meaning that it attempts to infer +

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:

+

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

fn main() {
     let x: i32 = 5;
 }
diff --git a/chZZ-generics.html b/chZZ-generics.html
index 5b242248c..fc1cc4593 100644
--- a/chZZ-generics.html
+++ b/chZZ-generics.html
@@ -101,8 +101,8 @@ struct Point<T> {
 

Our previous definition said, "We are defining a struct named Point." This definition says something slightly different: "We are defining a struct named Point with one type parameter T."

-

Let's talk about this term 'type parameter'. We've already seen one other thing -called a 'parameter' in Rust: function parameters:

+

Let's talk about this term type parameter. We've already seen one other thing +called a "parameter" in Rust: function parameters:

fn plus_one(x: i32) -> i32 {
     x + 1
 }
@@ -149,7 +149,7 @@ let same = Point { x: 5.0, y: 5.0 };
 

Here, instead of a single parameter, T, we have two: T and OtherT. Type parameters have the same naming convention as other types: CamelCase. However, you'll often see short, one-letter names used for types. T is very -common, because it's short for 'type', but you can name them something longer +common, because it's short for "type", but you can name them something longer if you'd like. In this version of Point, we say that x has the type T, and y has the type OtherT. This lets us give them two different types, but they don't have to be.

diff --git a/print.html b/print.html index 7899f344e..9f9654fa1 100644 --- a/print.html +++ b/print.html @@ -265,7 +265,7 @@ programs.

manage their Rust projects because it makes a lot of tasks easier. For example, Cargo takes care of building your code, downloading the libraries your code depends on, and building those libraries. We call libraries your code needs -‘dependencies’ since your code depends on them.

+dependencies since your code depends on them.

The simplest Rust programs, like the one we've written so far, don’t have any dependencies, so right now, you'd only be using the part of Cargo that can take care of building your code. As you write more complex Rust programs, you’ll @@ -440,7 +440,7 @@ authors = ["Your Name <you@example.com>"]

If the authors information that Cargo got from your environment is not correct, go ahead and fix that.

-

And as we saw in the last chapter, cargo new generates a ‘Hello, world!’ for +

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

fn main() {
     println!("Hello, world!");
@@ -480,7 +480,7 @@ fn main() {
 

We’ll need to take user input and then print the result as output. As such, we need the io library from the standard library. Rust only imports a few things -by default into every program, the ‘prelude’. If it’s not in the +by default into every program, the prelude. If it’s not in the prelude, you’ll have to use it directly. Using the std::io library gets you a number of useful io-related things, so that's what we've done here.

fn main() {
@@ -499,11 +499,11 @@ prints a string to the screen.

Now we’re getting interesting! There’s a lot going on in this little line. The first thing to notice is that this is a let statement, which is -used to create what are called ‘variable bindings’. Here's an example:

+used to create what are called variable bindings. Here's an example:

let foo = bar;
 

This will create a new binding named foo, and bind it to the value bar. In -many languages, this is called a ‘variable’, but Rust’s variable bindings have +many languages, this is called a variable, but Rust’s variable bindings have a few tricks up their sleeves.

For example, they’re immutable by default. That’s why our example uses mut: it makes a binding mutable, rather than immutable.

@@ -517,10 +517,10 @@ everything in comments.

bound to: String::new().

String is a string type, provided by the standard library. A String is a growable, UTF-8 encoded bit of text.

-

The ::new() syntax uses :: because this is an ‘associated function’ of +

The ::new() syntax uses :: because this is an associated function of a particular type. That is to say, it’s associated with String itself, rather than a particular instance of a String. Some languages call this a -‘static method’.

+static method.

This function is named new(), because it creates a new, empty String. You’ll find a new() function on many types, as it’s a common name for making a new value of some kind.

@@ -544,7 +544,7 @@ terminal. More specifically, a std::io::St passing one argument to read_line(): &mut guess.

Remember how we bound guess above? We said it was mutable. However, read_line doesn’t take a String as an argument: it takes a &mut String. -The & is the feature of Rust called a ‘reference’, which allows you to have +The & is the feature of Rust called a reference, which allows you to have multiple ways to access one piece of data in order to reduce copying. References are a complex feature, as one of Rust’s major selling points is how safe and easy it is to use references. We don’t need to know a lot of those @@ -623,9 +623,9 @@ print it back out.

Generating a secret number

Next, we need to generate a secret number. Rust does not yet include random number functionality in its standard library. The Rust team does, however, -provide a rand crate. A ‘crate’ is a package of Rust code. -We’ve been building a ‘binary crate’, which is an executable. rand is a -‘library crate’, which contains code that’s intended to be used with other +provide a rand crate. A crate is a package of Rust code. +We’ve been building a binary crate, which is an executable. rand is a +library crate, which contains code that’s intended to be used with other programs.

Using external crates is where Cargo really shines. Before we can write the code using rand, we need to modify our Cargo.toml. Open it up, and @@ -688,9 +688,9 @@ again. This lets you have a repeatable build automatically. In other words, we’ll stay at 0.3.14 until we explicitly upgrade, and so will anyone who we share our code with, thanks to the lock file.

What about when we do want to use v0.3.15? Cargo has another command, -update, which says ‘ignore the Cargo.lock file and figure out all the +update, which says "ignore the Cargo.lock file and figure out all the latest versions that fit what we’ve specified in Cargo.toml. If that works, -write those versions out to the lock file’. But by default, Cargo will only +write those versions out to the lock file". But by default, Cargo will only look for versions larger than 0.3.0 and smaller than 0.4.0. If we want to move to 0.4.x, we’d have to update what is in the Cargo.toml file. When we do, the next time we cargo build, Cargo will update the index and re-evaluate @@ -729,7 +729,7 @@ dependency. This also does the equivalent of a use rand; as well, s call anything in the rand crate by prefixing it with rand::.

Next, we added another use line: use rand::Rng. We’re going to use a method in a moment, and it requires that Rng be in scope to work. The basic -idea is this: methods are defined on something called ‘traits’, and for the +idea is this: methods are defined on something called traits, and for the method to work, it needs the trait to be in scope. For more about the details, read the traits section in Chapter XX.

There are two other lines we added, in the middle:

@@ -814,7 +814,7 @@ use that type:

a reference to the thing you want to compare it to. It returns the Ordering type we imported with the use statement earlier. We use a match statement to determine exactly what kind of Ordering it is. Ordering is an -enum, short for ‘enumeration’, which looks like this:

+enum, short for enumeration, which looks like this:

enum Foo {
     Bar,
     Baz,
@@ -825,7 +825,7 @@ statement to determine exactly what kind of Ordering it is. O
 namespace for a particular enum variant.

The Ordering enum has three possible variants: Less, Equal, and Greater. The match statement takes a value of a type and lets you -create an ‘arm’ for each possible value. An arm is made up of a pattern and the +create an arm for each possible value. An arm is made up of a pattern and the code that we should execute if the pattern matches the value of the type. Since we have three types of Ordering, we have three arms:

match guess.cmp(&secret_number) {
@@ -848,7 +848,7 @@ src/main.rs:23:21: 23:35 note:    found type `&_`
 error: aborting due to previous error
 Could not compile `guessing_game`.
 
-

Whew! This is a big error. The core of it is that we have ‘mismatched types’. +

Whew! This is a big error. The core of it is that we have mismatched types. Rust has a strong, static type system. However, it also has type inference. When we wrote let guess = String::new(), Rust was able to infer that guess should be a String, so it doesn’t make us write out the type. With our @@ -898,7 +898,7 @@ fn main() { .expect("Please type a number!");

Wait a minute, didn't we already have a guess? We do, but Rust allows us -to ‘shadow’ the previous guess with a new one. This is often used in this +to shadow the previous guess with a new one. This is often used in this exact situation, where guess starts as a String, but we want to convert it to a u32. Shadowing lets us re-use the guess name rather than forcing us to come up with two unique names like guess_str and guess or something @@ -909,8 +909,8 @@ else.

Here, guess refers to the old guess, the one that was a String with our input in it. The trim() method on Strings will eliminate any white space at the beginning and end of our string. This is important, as we had to press the -‘return’ key to satisfy read_line(). If we type 5 and hit return, guess -looks like this: 5\n. The \n represents ‘newline’, the enter key. trim() +"return" key to satisfy read_line(). If we type 5 and hit return, guess +looks like this: 5\n. The \n represents "newline", the return key. trim() gets rid of this, leaving our string with only the 5.

The parse() method on strings parses a string into some kind of number. Since it can parse a variety of numbers, we need to give Rust a hint as @@ -1090,8 +1090,8 @@ fn main() { Err(_) => continue, };

-

This is how you generally move from ‘crash on error’ to ‘actually handle the -error’: by switching from expect() to a match statement. A Result is the +

This is how you generally move from "crash on error" to "actually handle the +error": by switching from expect() to a match statement. A Result is the return type of parse(). Result is an enum like Ordering, but in this case, each variant has some data associated with it. Ok is a success, and Err is a failure. Each contains more information: in this case, the @@ -1306,7 +1306,7 @@ depends on the tradeoffs you want to make in your situation.

Shadowing

As we saw in the guessing game tutorial, we can declare new bindings with the same name as a previous binding, and the new binding shadows the previous -binding. We say that the first binding is ‘shadowed’ by the second, which means +binding. We say that the first binding is shadowed by the second, which means 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 @@ -1500,7 +1500,7 @@ this tuple value, like this:

In this program, we first create a tuple and bind it to the name tup. We then use a pattern with let to take tup and turn it into three separate -bindings, x, y, and z. This is called ‘destructuring’, because it breaks +bindings, x, y, and z. This is called destructuring, because it breaks the single tuple into three parts.

Finally, we print the value of y, which is 6.4.

Tuple Indexing

@@ -1891,7 +1891,7 @@ $ cd branches case, our condition is checking if our variable binding number has a value that is less than 5. The block of code we want to execute if the condition is true goes immediately after the condition, inside curly braces. These blocks -are sometimes called ‘arms’. We can optionally also include an else +are sometimes called arms. We can optionally also include an else statement, which we have chosen to do here. else gives the program a block of code to execute should condition evaluate to false.

Try running this code, and you should see output like this:

@@ -2022,7 +2022,7 @@ single type. If we try to run this, we’ll get an error:

   Compiling branches v0.1.0 (file:///projects/branches)
 src/main.rs:4:18: 8:6 error: if and else have incompatible types:
  expected `_`,
-    found `&‘static str`
+    found `&'static str`
 (expected integral variable,
     found &-ptr) [E0308]
 src/main.rs:4     let number = if condition {
@@ -2180,7 +2180,7 @@ enables Rust to make memory safety guarantees without needing a garbage
 collector. We’ll also talk about several related features: borrowing, slices,
 and how Rust lays things out in memory.

Ownership

-

Rust’s central feature is called ‘ownership’. It is a feature that is +

Rust’s central feature is called ownership. It is a feature that is straightforward to explain, but has deep implications for the rest of the language.

All programs have to manage the way they use a computer's memory while running. @@ -2215,8 +2215,8 @@ at which it’s declared until the end of the current scope. That is:

In other words, there are two important points in time here:

At this point, things are similar to other programming languages. Now let’s build on top of this understanding by introducing the String type.

@@ -2259,8 +2259,8 @@ done with our String.

That first part is done by us: when we call String::from(), its implementation requests the memory it needs. This is pretty much universal in programming languages.

-

The second case, however, is different. In languages with a garbage collector -(‘GC’), the GC will keep track and clean up memory that isn't being used +

The second case, however, is different. In languages with a garbage collector +(GC), the GC will keep track and clean up memory that isn't being used anymore, and we, as the programmer, don’t need to think about it. Without GC, it’s our responsibility to identify when memory is no longer being used and call code to explicitly return it, just as we did to request it. Doing this @@ -2369,13 +2369,13 @@ alone will free the memory, and we’re done.

This leads us to the Ownership Rules:

    -
  1. Each value in Rust has a variable binding that’s called its ‘owner’.
  2. +
  3. Each value in Rust has a variable binding that’s called its owner.
  4. There can only be one owner at a time.
  5. When the owner goes out of scope, the value will be drop()ped.

Furthermore, there’s a design choice that’s implied by this: Rust will never -automatically create ‘deep’ copies of your data. Therefore, any automatic +automatically create "deep" copies of your data. Therefore, any automatic copying can be assumed to be inexpensive.

Clone

But what if we do want to deeply copy the String’s data and not just the @@ -2589,7 +2589,7 @@ fn calculate_length(s: &String) -> usize { function return value is gone. Next, note that we pass &s1 into calculate_length(), and in its definition, we take &String rather than String.

-

These &s are called ‘references’, and they allow you to refer to some value +

These &s are called references, and they allow you to refer to some value without taking ownership of it. Here’s a diagram:

DIAGRAM GOES HERE of a &String pointing at a String, with (ptr, len, capacity)

Let’s take a closer look at the function call here:

@@ -2618,7 +2618,7 @@ a reference as an argument. Let’s add some explanatory annotations:

we don’t drop what a reference points to when the reference goes out of scope. This lets us write functions which take references as arguments instead of the values themselves, so that we won’t need to return them to give back ownership.

-

There’s another word for what references do, and that’s ‘borrowing’. Just like +

There’s another word for what references do, and that’s borrowing. Just like 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 @@ -2742,7 +2742,7 @@ fn dangle() -> &String { error: aborting due to previous error

This error message refers to a feature we haven’t learned about yet, -‘lifetimes’. The message does contain the key to why this code is a problem, +lifetimes. The message does contain the key to why this code is a problem, though:

this function’s return type contains a borrowed value, but there is no value
 for it to be borrowed from
@@ -3090,7 +3090,7 @@ modules and namespaces in depth yet, but you can think of the powi()f64, the
 same as the type. The powi() function takes two arguments: the first is a
 number, and the second is the power that it raises that number to. In this
-case, the second number is an integer, hence the ‘i’ in its name. Similarly,
+case, the second number is an integer, hence the i in its name. Similarly,
 sqrt() is a function under the f64 module, which takes the square root of
 its argument.

Why structs?

@@ -3265,8 +3265,8 @@ Point 1: Point { x: 0, y: 5 }

You’ll see this repeated later with other types. We’ll cover traits fully in Chapter XX.

Method Syntax

-

In the last section on ownership, we made several references to ‘methods’. -Methods look like this:

+

In Chapter 4 when we discussed ownership, we made several references to +methods. Methods look like this:

let s1 = "hello";
 
 // call a method on s1
@@ -3274,8 +3274,8 @@ let s2 = s1.clone();
 
 println!("{}", s1);
 
-

The call to clone() is attached to s1 with a dot. This is called ‘method -syntax’, and it’s a way to call certain functions with a different style.

+

The call to clone() is attached to s1 with a dot. This is called method +syntax, and it’s a way to call certain functions with a different style.

Why have two ways to call functions? We’ll talk about some deeper reasons related to ownership in a moment, but one big reason is that methods look nicer when chained together:

@@ -3292,7 +3292,7 @@ x.f().g().h(); methods.

Defining methods

We can define methods with the impl keyword. impl is short for -‘implementation’. Doing so looks like this:

+implementation. Doing so looks like this:

#[derive(Debug,Copy,Clone)]
 struct Point {
     x: f64,
@@ -3363,7 +3363,7 @@ from using the value of self after the transformation happens.

When we defined distance(), we took both self and the other argument by reference. Yet, we needed a & for p2 but not p1. What gives?

-

This feature is called ‘automatic referencing’, and calling methods is one +

This feature is called automatic referencing, and calling methods is one of the few places in Rust that has behavior like this. Here’s how it works: when you call a method with self.(, Rust will automatically add in &s or &muts to match the signature. In other words, these are the same:

@@ -3535,7 +3535,7 @@ useful: Option.

feature that we talked a little bit about in the previous chapter: generics.

Programming language design is often thought of as which features you include, but it's also about which features you leave out. Rust does not have a feature -that is in many other languages: 'null'. In languages with this feature, +that is in many other languages: null. In languages with this feature, variables can have two states: null or not-null.

The inventor of this concept has this to say:

@@ -3580,7 +3580,7 @@ let absent_number: Option<i32> = None;

Let's dig in. First, you'll notice that we used the <T> syntax when defining Option<T>: it's a generic enum. Option<T> has two variants: Some, which contains a T, and None, which has no data associated with it. In some -sense, None means 'null', and Some means 'not null'. So why is this any +sense, None means "null", and Some means "not null". So why is this any better than null?

In short, because Option<T> and T are different types. That's a bit too short though. Here's an example:

@@ -3916,8 +3916,8 @@ struct Point<T> {

Our previous definition said, "We are defining a struct named Point." This definition says something slightly different: "We are defining a struct named Point with one type parameter T."

-

Let's talk about this term 'type parameter'. We've already seen one other thing -called a 'parameter' in Rust: function parameters:

+

Let's talk about this term type parameter. We've already seen one other thing +called a "parameter" in Rust: function parameters:

fn plus_one(x: i32) -> i32 {
     x + 1
 }
@@ -3964,7 +3964,7 @@ let same = Point { x: 5.0, y: 5.0 };
 

Here, instead of a single parameter, T, we have two: T and OtherT. Type parameters have the same naming convention as other types: CamelCase. However, you'll often see short, one-letter names used for types. T is very -common, because it's short for 'type', but you can name them something longer +common, because it's short for "type", but you can name them something longer if you'd like. In this version of Point, we say that x has the type T, and y has the type OtherT. This lets us give them two different types, but they don't have to be.

@@ -4069,12 +4069,12 @@ to y. We could have used two let statements as well:

Type annotations

-

Most of the time, Rust uses ‘type inference’, meaning that it attempts to infer +

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:

+

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

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