mirror of
https://github.com/rust-lang/book.git
synced 2026-11-30 20:24:32 -06:00
Revert places that use bind as a verb`
This commit is contained in:
@@ -147,9 +147,9 @@ used to create *variables*. Here's another example:
|
||||
let foo = bar;
|
||||
```
|
||||
|
||||
This will create a new variable named `foo`, and initialize it with the value `bar`.
|
||||
In Rust, variables are immutable by default. To make our variable mutable, our
|
||||
example uses `mut` before the variable name.
|
||||
This will create a new variable named `foo`, and bind this name to the value
|
||||
`bar`. In Rust, variables are immutable by default. To make a variable mutable,
|
||||
use `mut` before the variable name:
|
||||
|
||||
```rust
|
||||
let foo = 5; // immutable.
|
||||
@@ -161,7 +161,7 @@ let mut bar = 5; // mutable
|
||||
|
||||
So now we know that `let mut guess` will introduce a mutable variable named
|
||||
`guess`, but we have to look at the other side of the `=` for the value it’s
|
||||
initialized with: `String::new`. `String` is a string type, provided by the standard
|
||||
bound to: `String::new`. `String` is a string type, provided by the standard
|
||||
library. A [`String`][string]<!-- ignore --> is a growable, UTF-8 encoded bit
|
||||
of text.
|
||||
|
||||
@@ -177,7 +177,7 @@ You’ll find a `new` function on many types, as it’s a common name for making
|
||||
a new value of some kind.
|
||||
|
||||
So to summarize, the `let mut guess = String::new();` line has created a
|
||||
mutable variable that is initialized with a new, empty instance of a `String`.
|
||||
mutable variable that is currently bound to a new, empty instance of a `String`.
|
||||
Whew!
|
||||
|
||||
Let’s move forward:
|
||||
@@ -744,7 +744,7 @@ Shadowing lets us re-use the `guess` variable name rather than forcing us to
|
||||
come up with two unique variables, like `guess_str` and `guess` or something
|
||||
(we'll cover shadowing in more detail in Chapter 3).
|
||||
|
||||
We initialize the second `guess` variable (a number of type `u32`) to the result
|
||||
We bind the second `guess` variable (a number of type `u32`) to the result
|
||||
of calling `.trim().parse()` on the first `guess` variable (a `String`).
|
||||
The `trim` method on `String`s will eliminate any whitespace at the
|
||||
beginning and end. Our `u32` can only contain numerical characters, but we have
|
||||
|
||||
@@ -7,8 +7,8 @@ offer. We still have the option to make our variables mutable, though. Let's
|
||||
explore how and why Rust encourages us to favor immutability, and why we might
|
||||
want to opt out of that.
|
||||
|
||||
Variables being immutable means once they're initialized, you can't
|
||||
change their value. To illustrate this, let's generate a new project in your
|
||||
Variables being immutable means once a value is bound, you can't
|
||||
change that value. To illustrate this, let's generate a new project in your
|
||||
projects directory called `variables` by using `cargo new --bin variables`.
|
||||
|
||||
Then, in your new `variables` directory, open `src/main.rs` and replace its code
|
||||
@@ -129,7 +129,7 @@ The value of x is: 5
|
||||
The value of x is: 6
|
||||
```
|
||||
|
||||
Using `mut`, we are allowed to change the value of `x` from `5` to
|
||||
Using `mut`, we are allowed to change the value that `x` binds to from `5` to
|
||||
`6`. In some cases you'll want to make a variable mutable because it makes the
|
||||
code easier to understand than an implementation that only uses immutable
|
||||
variables. In cases where you're using large data structures, mutating an
|
||||
@@ -159,7 +159,7 @@ fn main() {
|
||||
}
|
||||
```
|
||||
|
||||
This program first initializes `x` to a value of `5`. Then, it shadows `x` by
|
||||
This program first binds `x` to a value of `5`. Then, it shadows `x` by
|
||||
repeating `let x =`, taking the original value and adding `1` so that the value
|
||||
of `x` is then `6`. The third `let` statement also shadows `x`, taking the
|
||||
previous value and multiplying it by `2` to give `x` a final value of `12`. If
|
||||
|
||||
@@ -153,7 +153,7 @@ fn main() {
|
||||
```
|
||||
|
||||
Each expression in these statements uses a mathematical operator and evaluates
|
||||
to a single value, which is then assigned to a variable.
|
||||
to a single value, which is then bound to a variable.
|
||||
|
||||
#### The Boolean Type
|
||||
|
||||
@@ -222,10 +222,9 @@ fn main() {
|
||||
}
|
||||
```
|
||||
|
||||
The variable `tup` contains the entire tuple,
|
||||
it's a single compound element. To get the individual
|
||||
values out of a tuple, we can use pattern matching to destructure a tuple
|
||||
value, like this:
|
||||
The variable `tup` binds to the entire tuple, since a tuple is considered a
|
||||
single compound element. To get the individual values out of a tuple, we can
|
||||
use pattern matching to destructure a tuple value, like this:
|
||||
|
||||
Filename: src/main.rs
|
||||
|
||||
@@ -239,8 +238,8 @@ fn main() {
|
||||
}
|
||||
```
|
||||
|
||||
In this program, we first create a tuple and assign it to the variable `tup`. We then
|
||||
use a pattern with `let` to take `tup` and turn it into three separate
|
||||
In this program, we first create a tuple and bind it to the variable `tup`. We
|
||||
then use a pattern with `let` to take `tup` and turn it into three separate
|
||||
variables, `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`.
|
||||
|
||||
@@ -178,7 +178,7 @@ error: Could not compile `functions`.
|
||||
```
|
||||
|
||||
The `let y = 6` statement does not return a value, so there isn't anything for
|
||||
`x` to initialize to. This is different than in other languages like C and Ruby where
|
||||
`x` to bind to. This is different than in other languages like C and Ruby where
|
||||
the assignment returns the value of the assignment. In those languages, we can
|
||||
write `x = y = 6` and have both `x` and `y` have the value `6`; that is not the
|
||||
case in Rust.
|
||||
@@ -224,7 +224,7 @@ The expression:
|
||||
}
|
||||
```
|
||||
|
||||
is a block that, in this case, evaluates to `4`, and then gets assigned to
|
||||
is a block that, in this case, evaluates to `4`, and then gets bound to
|
||||
`y` as part of the `let` statement.
|
||||
|
||||
Note that the line containing `x + 1` does not have a semicolon at the end,
|
||||
|
||||
@@ -182,7 +182,7 @@ fn main() {
|
||||
}
|
||||
```
|
||||
|
||||
The `number` variable will be initialized to a value based on the outcome of the `if`
|
||||
The `number` variable will be bound to a value based on the outcome of the `if`
|
||||
expression. Let’s run this to see what happens:
|
||||
|
||||
```bash
|
||||
|
||||
@@ -238,8 +238,8 @@ let y = x;
|
||||
```
|
||||
|
||||
We can probably guess what this is doing based on our experience with other
|
||||
languages: “Assign the value `5` to `x`, then make a copy of the value in `x` and
|
||||
assign it to `y`.” We now have two variables, `x` and `y`, and both equal `5`.
|
||||
languages: “Bind the value `5` to `x`, then make a copy of the value in `x` and
|
||||
bind it to `y`.” We now have two variables, `x` and `y`, and both equal `5`.
|
||||
This is indeed what is happening since integers are simple values with a known,
|
||||
fixed size, and these two `5` values are pushed onto the stack.
|
||||
|
||||
@@ -252,7 +252,7 @@ let s2 = s1;
|
||||
|
||||
This looks very similar to the previous code, so we might assume that the way
|
||||
it works would be the same: that the second line would make a copy of the value
|
||||
in `s1` and assign it to `s2`. This isn't quite what happens.
|
||||
in `s1` and bind it to `s2`. This isn't quite what happens.
|
||||
|
||||
To explain this more thoroughly, let’s take a look at what `String` looks like
|
||||
under the covers in Figure 4-1. A `String` is made up of three parts, shown on
|
||||
@@ -262,7 +262,8 @@ is the memory that holds the contents, and this is on the heap.
|
||||
|
||||
<img alt="String in memory" src="img/trpl04-01.svg" class="center" style="width: 50%;" />
|
||||
|
||||
Figure 4-1: Representation in memory of a `String` variable `s1` holding the value "hello"
|
||||
Figure 4-1: Representation in memory of a `String` holding the value "hello"
|
||||
bound to `s1`
|
||||
|
||||
The length is how much memory, in bytes, the contents of the `String` is
|
||||
currently using. The capacity is the total amount of memory, in bytes, that the
|
||||
|
||||
+19
-19
@@ -86,12 +86,12 @@ fn value_in_cents(coin: Coin) -> i32 {
|
||||
}
|
||||
```
|
||||
|
||||
Another useful feature of match arms is that they can create variables for parts
|
||||
of the values that match the pattern. From 1999 through 2008, the U.S. printed
|
||||
quarters with different designs for each of the 50 states on one side. The other
|
||||
coins did not get state designs, so only quarters have this extra attribute. We
|
||||
can add this information to our `enum` by changing the `Quarter` variant to have
|
||||
a `State` value:
|
||||
Another useful feature of match arms is that they can create variables that bind
|
||||
to values in the matching pattern. Here's an example use case: from 1999 through
|
||||
2008, the U.S. printed quarters with different designs for each of the 50 states
|
||||
on one side. The other coins did not get state designs, so only quarters have
|
||||
this extra attribute. We can add this information to our `enum` by changing the
|
||||
`Quarter` variant to have a `State` value:
|
||||
|
||||
```rust
|
||||
#[derive(Debug)] // So we can inspect the state in a minute
|
||||
@@ -115,9 +115,9 @@ to call out the name of the state so that if it's one our friend doesn't have
|
||||
yet, they can add it to their collection.
|
||||
|
||||
In the match statement to do this, the quarter case now has a variable, `state`,
|
||||
that contains the value of the state of that quarter. The variable will only get
|
||||
created if the coin matches the `Quarter` pattern. Then we can use the variable
|
||||
in the code for that arm:
|
||||
that binds to the value of the state for that quarter. The variable will only
|
||||
get created if the coin matches the `Quarter` pattern. Then we can use the
|
||||
variable in the code for that arm:
|
||||
|
||||
```rust
|
||||
# #[derive(Debug)]
|
||||
@@ -146,13 +146,13 @@ fn value_in_cents(coin: Coin) -> i32 {
|
||||
}
|
||||
```
|
||||
|
||||
If we were to call `value_in_cents(Coin::Quarter(UsState::Alaska))`, `coin` will
|
||||
be `Coin::Quarter(UsState::Alaska)`. When we compare that value with each of the
|
||||
match arms, none of them match until we reach `Coin::Quarter(state)`. At that
|
||||
point, the variable `state` will have the value `UsState::Alaska`. We can then
|
||||
use that variable in the `println!`, thus getting the inner state value out of
|
||||
the `Coin` enum variant for `Quarter` and enabling us to print "State quarter
|
||||
from Alaska!".
|
||||
If we were to call `value_in_cents(Coin::Quarter(UsState::Alaska))`, `coin`
|
||||
will be `Coin::Quarter(UsState::Alaska)`. When we compare that value with each
|
||||
of the match arms, none of the patterns will match until we reach
|
||||
`Coin::Quarter(state)`. At that point, the variable `state` will bind to the
|
||||
value `UsState::Alaska`. We can then use that variable in the `println!`, thus
|
||||
getting the inner state value out of the `Coin` enum variant for `Quarter` and
|
||||
enabling us to print "State quarter from Alaska!".
|
||||
|
||||
Remember the `Option<T>` type from the previous section, and that we wanted to
|
||||
be able to get the inner `T` value out of the `Some` case? This will be very
|
||||
@@ -193,7 +193,7 @@ Some(i) => Some(i + 1),
|
||||
```
|
||||
|
||||
Does `Some(5)` match `Some(i)`? Why yes it does! We have the same variant. The
|
||||
`i` initializes to the value inside of the `Some`, so `i` has the value `5`. Then we
|
||||
`i` binds to the value inside of the `Some`, so `i` has the value `5`. Then we
|
||||
execute the code in that match arm: take `i`, which is `5`, add one to it, and
|
||||
create a new `Some` value with our total inside.
|
||||
|
||||
@@ -209,8 +209,8 @@ return the `None` value that is on the right side of the `=>`. We don't
|
||||
check any other arms since we found one that matched.
|
||||
|
||||
Combining `match` and enums together is extremely powerful. You'll see this
|
||||
pattern a lot in Rust code: `match` against an enum, initialize a variable to the data
|
||||
inside, and then execute code based on it. It's a bit tricky at first, but
|
||||
pattern a lot in Rust code: `match` against an enum, bind a variable to the data
|
||||
inside, and then execute code using the variable. It's a bit tricky at first, but
|
||||
once you get used to it, you'll wish you had it in languages that don't support
|
||||
it. It's consistently a user favorite.
|
||||
|
||||
|
||||
@@ -164,7 +164,7 @@ map.insert(1, "hello");
|
||||
let e = map.entry(2);
|
||||
```
|
||||
|
||||
Here, `e` is initialized with a special enum, `Entry`. An `Entry` represents a
|
||||
Here, the value bound to `e` is a special enum, `Entry`. An `Entry` represents a
|
||||
value that might or might not exist. Let's say that we want to see if the key
|
||||
`2` has a value associated with it. If it doesn't, we want to insert the value
|
||||
"world". In both cases, we want to return the resulting value that now goes
|
||||
|
||||
+13
-11
@@ -1,9 +1,10 @@
|
||||
# Patterns
|
||||
|
||||
We've actually used patterns a few times so far: they're used in `let`
|
||||
statements, in function arguments, and in the `match` expression. Patterns have a
|
||||
lot more abilities than we have demonstrated so far, so we'll cover some of the most commonly used ones in this section. Any of these abilities work in
|
||||
any place where a pattern is used.
|
||||
statements, in function arguments, and in the `match` expression. Patterns have
|
||||
a lot more abilities than we have demonstrated so far, so we'll cover some of
|
||||
the most commonly used ones in this section. Any of these abilities work in any
|
||||
place where a pattern is used.
|
||||
|
||||
## `let` statements
|
||||
|
||||
@@ -13,8 +14,8 @@ A basic `let` statement has this form:
|
||||
let PATTERN = EXPRESSION;
|
||||
```
|
||||
|
||||
We've seen statements like `let x = 5;` with a variable in the `PATTERN`
|
||||
slot; a variable is just a particularly humble form of pattern.
|
||||
We've seen statements like `let x = 5;` with a variable name in the `PATTERN`
|
||||
slot; a variable name is just a particularly humble form of pattern.
|
||||
|
||||
Let’s try a more complex pattern. Change our example program to this:
|
||||
|
||||
@@ -51,8 +52,9 @@ And here’s the value:
|
||||
(5, 6)
|
||||
```
|
||||
|
||||
As you can see, the two line up visually, and so `let` initializes `x` to `5`
|
||||
and `y` to `6`. We could have used two `let` statements as well:
|
||||
As you can see, the two line up visually, and so `let` binds the variable `x`
|
||||
to the value `5` and `y` to `6`. We could have used two `let` statements as
|
||||
well:
|
||||
|
||||
```rust
|
||||
fn main() {
|
||||
@@ -131,8 +133,8 @@ This prints `one or two`.
|
||||
|
||||
## ref and ref mut
|
||||
|
||||
Usually, when you match against a pattern, variables are initialized to a value.
|
||||
This means you'll end up moving the value out:
|
||||
Usually, when you match against a pattern, variables are bound to a value.
|
||||
This means you'll end up moving the value into the `match`:
|
||||
|
||||
```rust,ignore
|
||||
let name = Some(String::from("Bors"));
|
||||
@@ -146,7 +148,7 @@ match name {
|
||||
println!("name is: {:?}", name);
|
||||
```
|
||||
|
||||
If you'd prefer to initialize `name` to a reference, use the `ref` keyword:
|
||||
If you'd prefer to bind `name` to a reference, use the `ref` keyword:
|
||||
|
||||
```rust
|
||||
let name = Some(String::from("Bors"));
|
||||
@@ -204,7 +206,7 @@ struct Point {
|
||||
let origin = Point { x: 0, y: 0 };
|
||||
|
||||
match origin {
|
||||
Point { x, y } => { }, // x and y are initialized here
|
||||
Point { x, y } => { }, // variables x and y are created here
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
Reference in New Issue
Block a user