mirror of
https://github.com/rust-lang/book.git
synced 2026-05-17 22:31:47 -04:00
update 0.8.1 to 0.8.3
This commit is contained in:
@@ -38,7 +38,7 @@ checksum = "ac74c624d6b2d21f425f752262f42188365d7b8ff1aff74c82e45136510a4857"
|
||||
|
||||
[[package]]
|
||||
name = "rand"
|
||||
version = "0.8.1"
|
||||
version = "0.8.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a76330fb486679b4ace3670f117bbc9e16204005c4bde9c4bd372f45bed34f12"
|
||||
dependencies = [
|
||||
|
||||
@@ -7,4 +7,4 @@ edition = "2018"
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
rand = "0.8.1"
|
||||
rand = "0.8.3"
|
||||
|
||||
@@ -38,7 +38,7 @@ checksum = "ac74c624d6b2d21f425f752262f42188365d7b8ff1aff74c82e45136510a4857"
|
||||
|
||||
[[package]]
|
||||
name = "rand"
|
||||
version = "0.8.1"
|
||||
version = "0.8.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a76330fb486679b4ace3670f117bbc9e16204005c4bde9c4bd372f45bed34f12"
|
||||
dependencies = [
|
||||
|
||||
@@ -7,4 +7,4 @@ edition = "2018"
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
rand = "0.8.1"
|
||||
rand = "0.8.3"
|
||||
|
||||
@@ -38,7 +38,7 @@ checksum = "ac74c624d6b2d21f425f752262f42188365d7b8ff1aff74c82e45136510a4857"
|
||||
|
||||
[[package]]
|
||||
name = "rand"
|
||||
version = "0.8.1"
|
||||
version = "0.8.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a76330fb486679b4ace3670f117bbc9e16204005c4bde9c4bd372f45bed34f12"
|
||||
dependencies = [
|
||||
|
||||
@@ -7,4 +7,4 @@ edition = "2018"
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
rand = "0.8.1"
|
||||
rand = "0.8.3"
|
||||
|
||||
@@ -2,7 +2,7 @@ $ cargo build
|
||||
Compiling libc v0.2.51
|
||||
Compiling rand_core v0.4.0
|
||||
Compiling rand_core v0.3.1
|
||||
Compiling rand v0.8.1
|
||||
Compiling rand v0.8.3
|
||||
Compiling guessing_game v0.1.0 (file:///projects/guessing_game)
|
||||
error[E0308]: mismatched types
|
||||
--> src/main.rs:22:21
|
||||
|
||||
@@ -38,7 +38,7 @@ checksum = "ac74c624d6b2d21f425f752262f42188365d7b8ff1aff74c82e45136510a4857"
|
||||
|
||||
[[package]]
|
||||
name = "rand"
|
||||
version = "0.8.1"
|
||||
version = "0.8.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a76330fb486679b4ace3670f117bbc9e16204005c4bde9c4bd372f45bed34f12"
|
||||
dependencies = [
|
||||
|
||||
@@ -7,4 +7,4 @@ edition = "2018"
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
rand = "0.8.1"
|
||||
rand = "0.8.3"
|
||||
|
||||
@@ -38,7 +38,7 @@ checksum = "ac74c624d6b2d21f425f752262f42188365d7b8ff1aff74c82e45136510a4857"
|
||||
|
||||
[[package]]
|
||||
name = "rand"
|
||||
version = "0.8.1"
|
||||
version = "0.8.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a76330fb486679b4ace3670f117bbc9e16204005c4bde9c4bd372f45bed34f12"
|
||||
dependencies = [
|
||||
|
||||
@@ -7,4 +7,4 @@ edition = "2018"
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
rand = "0.8.1"
|
||||
rand = "0.8.3"
|
||||
|
||||
@@ -38,7 +38,7 @@ checksum = "ac74c624d6b2d21f425f752262f42188365d7b8ff1aff74c82e45136510a4857"
|
||||
|
||||
[[package]]
|
||||
name = "rand"
|
||||
version = "0.8.1"
|
||||
version = "0.8.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a76330fb486679b4ace3670f117bbc9e16204005c4bde9c4bd372f45bed34f12"
|
||||
dependencies = [
|
||||
|
||||
@@ -7,4 +7,4 @@ edition = "2018"
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
rand = "0.8.1"
|
||||
rand = "0.8.3"
|
||||
|
||||
@@ -38,7 +38,7 @@ checksum = "ac74c624d6b2d21f425f752262f42188365d7b8ff1aff74c82e45136510a4857"
|
||||
|
||||
[[package]]
|
||||
name = "rand"
|
||||
version = "0.8.1"
|
||||
version = "0.8.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a76330fb486679b4ace3670f117bbc9e16204005c4bde9c4bd372f45bed34f12"
|
||||
dependencies = [
|
||||
|
||||
@@ -7,4 +7,4 @@ edition = "2018"
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
rand = "0.8.1"
|
||||
rand = "0.8.3"
|
||||
|
||||
@@ -38,7 +38,7 @@ checksum = "ac74c624d6b2d21f425f752262f42188365d7b8ff1aff74c82e45136510a4857"
|
||||
|
||||
[[package]]
|
||||
name = "rand"
|
||||
version = "0.8.1"
|
||||
version = "0.8.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a76330fb486679b4ace3670f117bbc9e16204005c4bde9c4bd372f45bed34f12"
|
||||
dependencies = [
|
||||
|
||||
@@ -7,4 +7,4 @@ edition = "2018"
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
rand = "0.8.1"
|
||||
rand = "0.8.3"
|
||||
|
||||
@@ -38,7 +38,7 @@ checksum = "ac74c624d6b2d21f425f752262f42188365d7b8ff1aff74c82e45136510a4857"
|
||||
|
||||
[[package]]
|
||||
name = "rand"
|
||||
version = "0.8.1"
|
||||
version = "0.8.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a76330fb486679b4ace3670f117bbc9e16204005c4bde9c4bd372f45bed34f12"
|
||||
dependencies = [
|
||||
|
||||
@@ -5,4 +5,4 @@ authors = ["Your Name <you@example.com>"]
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
rand = "0.8.1"
|
||||
rand = "0.8.3"
|
||||
|
||||
@@ -38,7 +38,7 @@ checksum = "ac74c624d6b2d21f425f752262f42188365d7b8ff1aff74c82e45136510a4857"
|
||||
|
||||
[[package]]
|
||||
name = "rand"
|
||||
version = "0.8.1"
|
||||
version = "0.8.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a76330fb486679b4ace3670f117bbc9e16204005c4bde9c4bd372f45bed34f12"
|
||||
dependencies = [
|
||||
|
||||
@@ -7,4 +7,4 @@ edition = "2018"
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
rand = "0.8.1"
|
||||
rand = "0.8.3"
|
||||
|
||||
@@ -38,7 +38,7 @@ checksum = "ac74c624d6b2d21f425f752262f42188365d7b8ff1aff74c82e45136510a4857"
|
||||
|
||||
[[package]]
|
||||
name = "rand"
|
||||
version = "0.8.1"
|
||||
version = "0.8.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a76330fb486679b4ace3670f117bbc9e16204005c4bde9c4bd372f45bed34f12"
|
||||
dependencies = [
|
||||
|
||||
@@ -5,4 +5,4 @@ authors = ["Your Name <you@example.com>"]
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
rand = "0.8.1"
|
||||
rand = "0.8.3"
|
||||
|
||||
@@ -38,7 +38,7 @@ checksum = "ac74c624d6b2d21f425f752262f42188365d7b8ff1aff74c82e45136510a4857"
|
||||
|
||||
[[package]]
|
||||
name = "rand"
|
||||
version = "0.8.1"
|
||||
version = "0.8.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a76330fb486679b4ace3670f117bbc9e16204005c4bde9c4bd372f45bed34f12"
|
||||
dependencies = [
|
||||
|
||||
@@ -5,4 +5,4 @@ authors = ["Your Name <you@example.com>"]
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
rand = "0.8.1"
|
||||
rand = "0.8.3"
|
||||
|
||||
@@ -45,7 +45,7 @@ checksum = "ac74c624d6b2d21f425f752262f42188365d7b8ff1aff74c82e45136510a4857"
|
||||
|
||||
[[package]]
|
||||
name = "rand"
|
||||
version = "0.8.1"
|
||||
version = "0.8.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a76330fb486679b4ace3670f117bbc9e16204005c4bde9c4bd372f45bed34f12"
|
||||
dependencies = [
|
||||
|
||||
@@ -5,4 +5,4 @@ authors = ["Your Name <you@example.com>"]
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
rand = "0.8.1"
|
||||
rand = "0.8.3"
|
||||
|
||||
@@ -45,7 +45,7 @@ checksum = "ac74c624d6b2d21f425f752262f42188365d7b8ff1aff74c82e45136510a4857"
|
||||
|
||||
[[package]]
|
||||
name = "rand"
|
||||
version = "0.8.1"
|
||||
version = "0.8.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a76330fb486679b4ace3670f117bbc9e16204005c4bde9c4bd372f45bed34f12"
|
||||
dependencies = [
|
||||
|
||||
@@ -5,4 +5,4 @@ authors = ["Your Name <you@example.com>"]
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
rand = "0.8.1"
|
||||
rand = "0.8.3"
|
||||
|
||||
@@ -14,7 +14,7 @@ correct, the game will print a congratulatory message and exit.
|
||||
|
||||
## Setting Up a New Project
|
||||
|
||||
To set up a new project, go to the *projects* directory that you created in
|
||||
To set up a new project, go to the _projects_ directory that you created in
|
||||
Chapter 1 and make a new project using Cargo, like so:
|
||||
|
||||
```console
|
||||
@@ -26,7 +26,7 @@ The first command, `cargo new`, takes the name of the project (`guessing_game`)
|
||||
as the first argument. The second command changes to the new project’s
|
||||
directory.
|
||||
|
||||
Look at the generated *Cargo.toml* file:
|
||||
Look at the generated _Cargo.toml_ file:
|
||||
|
||||
<span class="filename">Filename: Cargo.toml</span>
|
||||
|
||||
@@ -38,7 +38,7 @@ If the author information that Cargo obtained from your environment is not
|
||||
correct, fix that in the file and save it again.
|
||||
|
||||
As you saw in Chapter 1, `cargo new` generates a “Hello, world!” program for
|
||||
you. Check out the *src/main.rs* file:
|
||||
you. Check out the _src/main.rs_ file:
|
||||
|
||||
<span class="filename">Filename: src/main.rs</span>
|
||||
|
||||
@@ -57,14 +57,14 @@ The `run` command comes in handy when you need to rapidly iterate on a project,
|
||||
as we’ll do in this game, quickly testing each iteration before moving on to
|
||||
the next one.
|
||||
|
||||
Reopen the *src/main.rs* file. You’ll be writing all the code in this file.
|
||||
Reopen the _src/main.rs_ file. You’ll be writing all the code in this file.
|
||||
|
||||
## Processing a Guess
|
||||
|
||||
The first part of the guessing game program will ask for user input, process
|
||||
that input, and check that the input is in the expected form. To start, we’ll
|
||||
allow the player to input a guess. Enter the code in Listing 2-1 into
|
||||
*src/main.rs*.
|
||||
_src/main.rs_.
|
||||
|
||||
<span class="filename">Filename: src/main.rs</span>
|
||||
|
||||
@@ -85,7 +85,7 @@ standard library (which is known as `std`):
|
||||
```
|
||||
|
||||
By default, Rust brings only a few types into the scope of every program in
|
||||
[the *prelude*][prelude]<!-- ignore -->. If a type you want to use isn’t in the
|
||||
[the _prelude_][prelude]<!-- ignore -->. If a type you want to use isn’t in the
|
||||
prelude, you have to bring that type into scope explicitly with a `use`
|
||||
statement. Using the `std::io` library provides you with a number of useful
|
||||
features, including the ability to accept user input.
|
||||
@@ -122,7 +122,7 @@ Next, we’ll create a place to store the user input, like this:
|
||||
|
||||
Now the program is getting interesting! There’s a lot going on in this little
|
||||
line. Notice that this is a `let` statement, which is used to create a
|
||||
*variable*. Here’s another example:
|
||||
_variable_. Here’s another example:
|
||||
|
||||
```rust,ignore
|
||||
let foo = bar;
|
||||
@@ -153,10 +153,10 @@ library that is a growable, UTF-8 encoded bit of text.
|
||||
|
||||
[string]: ../std/string/struct.String.html
|
||||
|
||||
The `::` syntax in the `::new` line indicates that `new` is an *associated
|
||||
function* of the `String` type. An associated function is implemented on a type,
|
||||
The `::` syntax in the `::new` line indicates that `new` is an _associated
|
||||
function_ of the `String` type. An associated function is implemented on a type,
|
||||
in this case `String`, rather than on a particular instance of a `String`. Some
|
||||
languages call this a *static method*.
|
||||
languages call this a _static method_.
|
||||
|
||||
This `new` function creates a new, empty string. You’ll find a `new` function
|
||||
on many types, because it’s a common name for a function that makes a new value
|
||||
@@ -182,8 +182,7 @@ type that represents a handle to the standard input for your terminal.
|
||||
|
||||
The next part of the code, `.read_line(&mut guess)`, calls the
|
||||
[`read_line`][read_line]<!-- ignore --> method on the standard input handle to
|
||||
get input from the user. We’re also passing one argument to `read_line`: `&mut
|
||||
guess`.
|
||||
get input from the user. We’re also passing one argument to `read_line`: `&mut guess`.
|
||||
|
||||
[read_line]: ../std/io/struct.Stdin.html#method.read_line
|
||||
|
||||
@@ -192,7 +191,7 @@ and place that into a string, so it takes that string as an argument. The
|
||||
string argument needs to be mutable so the method can change the string’s
|
||||
content by adding the user input.
|
||||
|
||||
The `&` indicates that this argument is a *reference*, which gives you a way to
|
||||
The `&` indicates that this argument is a _reference_, which gives you a way to
|
||||
let multiple parts of your code access one piece of data without needing to
|
||||
copy that data into memory multiple times. References are a complex feature,
|
||||
and one of Rust’s major advantages is how safe and easy it is to use
|
||||
@@ -232,9 +231,9 @@ as well as specific versions for submodules, such as `io::Result`.
|
||||
[ioresult]: ../std/io/type.Result.html
|
||||
[result]: ../std/result/enum.Result.html
|
||||
|
||||
The `Result` types are [*enumerations*][enums]<!-- ignore -->, often referred
|
||||
to as *enums*. An enumeration is a type that can have a fixed set of values,
|
||||
and those values are called the enum’s *variants*. Chapter 6 will cover enums
|
||||
The `Result` types are [_enumerations_][enums]<!-- ignore -->, often referred
|
||||
to as _enums_. An enumeration is a type that can have a fixed set of values,
|
||||
and those values are called the enum’s _variants_. Chapter 6 will cover enums
|
||||
in more detail.
|
||||
|
||||
[enums]: ch06-00-enums.html
|
||||
@@ -333,15 +332,15 @@ library. However, the Rust team does provide a [`rand` crate][randcrate].
|
||||
### Using a Crate to Get More Functionality
|
||||
|
||||
Remember that a crate is a collection of Rust source code files.
|
||||
The project we’ve been building is a *binary crate*, which is an executable.
|
||||
The `rand` crate is a *library crate*, which contains code intended to be
|
||||
The project we’ve been building is a _binary crate_, which is an executable.
|
||||
The `rand` crate is a _library crate_, which contains code intended to be
|
||||
used in other programs.
|
||||
|
||||
Cargo’s use of external crates is where it really shines. Before we can write
|
||||
code that uses `rand`, we need to modify the *Cargo.toml* file to include the
|
||||
code that uses `rand`, we need to modify the _Cargo.toml_ file to include the
|
||||
`rand` crate as a dependency. Open that file now and add the following line to
|
||||
the bottom beneath the `[dependencies]` section header that Cargo created for
|
||||
you (be sure to use version `0.8.1` or the code examples in this tutorial may not work!):
|
||||
you (be sure to use version `0.8.3` or the code examples in this tutorial may not work!):
|
||||
|
||||
<!-- When updating the version of `rand` used, also update the version of
|
||||
`rand` used in these files so they all match:
|
||||
@@ -355,16 +354,16 @@ you (be sure to use version `0.8.1` or the code examples in this tutorial may no
|
||||
{{#include ../listings/ch02-guessing-game-tutorial/listing-02-02/Cargo.toml:9:}}
|
||||
```
|
||||
|
||||
In the *Cargo.toml* file, everything that follows a header is part of a section
|
||||
In the _Cargo.toml_ file, everything that follows a header is part of a section
|
||||
that continues until another section starts. The `[dependencies]` section is
|
||||
where you tell Cargo which external crates your project depends on and which
|
||||
versions of those crates you require. In this case, we’ll specify the `rand`
|
||||
crate with the semantic version specifier `0.8.1`. Cargo understands [Semantic
|
||||
Versioning][semver]<!-- ignore --> (sometimes called *SemVer*), which is a
|
||||
standard for writing version numbers. The number `0.8.1` is actually shorthand
|
||||
for `^0.8.1`, which means any version that is at least `0.8.1` but below
|
||||
crate with the semantic version specifier `0.8.3`. Cargo understands [Semantic
|
||||
Versioning][semver]<!-- ignore --> (sometimes called _SemVer_), which is a
|
||||
standard for writing version numbers. The number `0.8.3` is actually shorthand
|
||||
for `^0.8.3`, which means any version that is at least `0.8.3` but below
|
||||
`0.9.0`. Cargo considers these versions to have public APIs compatible with
|
||||
version `0.8.1`.
|
||||
version `0.8.3`.
|
||||
|
||||
[semver]: http://semver.org
|
||||
|
||||
@@ -379,7 +378,7 @@ cargo build -->
|
||||
```console
|
||||
$ cargo build
|
||||
Updating crates.io index
|
||||
Downloaded rand v0.8.1
|
||||
Downloaded rand v0.8.3
|
||||
Downloaded libc v0.2.62
|
||||
Downloaded rand_core v0.2.2
|
||||
Downloaded rand_core v0.3.1
|
||||
@@ -388,7 +387,7 @@ $ cargo build
|
||||
Compiling libc v0.2.62
|
||||
Compiling rand_core v0.3.1
|
||||
Compiling rand_core v0.2.2
|
||||
Compiling rand v0.8.1
|
||||
Compiling rand v0.8.3
|
||||
Compiling guessing_game v0.1.0 (file:///projects/guessing_game)
|
||||
Finished dev [unoptimized + debuginfo] target(s) in 2.53s
|
||||
```
|
||||
@@ -400,7 +399,7 @@ You may see different version numbers (but they will all be compatible with
|
||||
the code, thanks to SemVer!), different lines (depending on the operating system), and the lines may be in a different order.
|
||||
|
||||
Now that we have an external dependency, Cargo fetches the latest versions of
|
||||
everything from the *registry*, which is a copy of data from
|
||||
everything from the _registry_, which is a copy of data from
|
||||
[Crates.io][cratesio]. Crates.io is where people in the Rust ecosystem post
|
||||
their open source Rust projects for others to use.
|
||||
|
||||
@@ -415,11 +414,11 @@ them and then compiles the project with the dependencies available.
|
||||
If you immediately run `cargo build` again without making any changes, you
|
||||
won’t get any output aside from the `Finished` line. Cargo knows it has already
|
||||
downloaded and compiled the dependencies, and you haven’t changed anything
|
||||
about them in your *Cargo.toml* file. Cargo also knows that you haven’t changed
|
||||
about them in your _Cargo.toml_ file. Cargo also knows that you haven’t changed
|
||||
anything about your code, so it doesn’t recompile that either. With nothing to
|
||||
do, it simply exits.
|
||||
|
||||
If you open up the *src/main.rs* file, make a trivial change, and then save it
|
||||
If you open up the _src/main.rs_ file, make a trivial change, and then save it
|
||||
and build again, you’ll only see two lines of output:
|
||||
|
||||
<!-- manual-regeneration
|
||||
@@ -434,39 +433,39 @@ $ cargo build
|
||||
```
|
||||
|
||||
These lines show Cargo only updates the build with your tiny change to the
|
||||
*src/main.rs* file. Your dependencies haven’t changed, so Cargo knows it can
|
||||
_src/main.rs_ file. Your dependencies haven’t changed, so Cargo knows it can
|
||||
reuse what it has already downloaded and compiled for those. It just rebuilds
|
||||
your part of the code.
|
||||
|
||||
#### Ensuring Reproducible Builds with the *Cargo.lock* File
|
||||
#### Ensuring Reproducible Builds with the _Cargo.lock_ File
|
||||
|
||||
Cargo has a mechanism that ensures you can rebuild the same artifact every time
|
||||
you or anyone else builds your code: Cargo will use only the versions of the
|
||||
dependencies you specified until you indicate otherwise. For example, what
|
||||
happens if next week version 0.8.1 of the `rand` crate comes out and
|
||||
happens if next week version 0.8.4 of the `rand` crate comes out and
|
||||
contains an important bug fix but also contains a regression that will break
|
||||
your code?
|
||||
|
||||
The answer to this problem is the *Cargo.lock* file, which was created the
|
||||
first time you ran `cargo build` and is now in your *guessing_game* directory.
|
||||
The answer to this problem is the _Cargo.lock_ file, which was created the
|
||||
first time you ran `cargo build` and is now in your _guessing_game_ directory.
|
||||
When you build a project for the first time, Cargo figures out all the
|
||||
versions of the dependencies that fit the criteria and then writes them to
|
||||
the *Cargo.lock* file. When you build your project in the future, Cargo will
|
||||
see that the *Cargo.lock* file exists and use the versions specified there
|
||||
the _Cargo.lock_ file. When you build your project in the future, Cargo will
|
||||
see that the _Cargo.lock_ file exists and use the versions specified there
|
||||
rather than doing all the work of figuring out versions again. This lets you
|
||||
have a reproducible build automatically. In other words, your project will
|
||||
remain at `0.8.1` until you explicitly upgrade, thanks to the *Cargo.lock*
|
||||
remain at `0.8.3` until you explicitly upgrade, thanks to the _Cargo.lock_
|
||||
file.
|
||||
|
||||
#### Updating a Crate to Get a New Version
|
||||
|
||||
When you *do* want to update a crate, Cargo provides another command, `update`,
|
||||
which will ignore the *Cargo.lock* file and figure out all the latest versions
|
||||
that fit your specifications in *Cargo.toml*. If that works, Cargo will write
|
||||
those versions to the *Cargo.lock* file.
|
||||
When you _do_ want to update a crate, Cargo provides another command, `update`,
|
||||
which will ignore the _Cargo.lock_ file and figure out all the latest versions
|
||||
that fit your specifications in _Cargo.toml_. If that works, Cargo will write
|
||||
those versions to the _Cargo.lock_ file.
|
||||
|
||||
But by default, Cargo will only look for versions greater than `0.8.1` and less
|
||||
than `0.9.0`. If the `rand` crate has released two new versions, `0.8.2` and
|
||||
But by default, Cargo will only look for versions greater than `0.8.3` and less
|
||||
than `0.9.0`. If the `rand` crate has released two new versions, `0.8.4` and
|
||||
`0.9.0`, you would see the following if you ran `cargo update`:
|
||||
|
||||
<!-- manual-regeneration
|
||||
@@ -478,14 +477,14 @@ as a guide to creating the hypothetical output shown here -->
|
||||
```console
|
||||
$ cargo update
|
||||
Updating crates.io index
|
||||
Updating rand v0.8.1 -> v0.8.2
|
||||
Updating rand v0.8.3 -> v0.8.4
|
||||
```
|
||||
|
||||
At this point, you would also notice a change in your *Cargo.lock* file noting
|
||||
that the version of the `rand` crate you are now using is `0.8.2`.
|
||||
At this point, you would also notice a change in your _Cargo.lock_ file noting
|
||||
that the version of the `rand` crate you are now using is `0.8.4`.
|
||||
|
||||
If you wanted to use `rand` version `0.9.0` or any version in the `0.9.x`
|
||||
series, you’d have to update the *Cargo.toml* file to look like this instead:
|
||||
series, you’d have to update the _Cargo.toml_ file to look like this instead:
|
||||
|
||||
```toml
|
||||
[dependencies]
|
||||
@@ -507,8 +506,8 @@ from a number of packages.
|
||||
|
||||
### Generating a Random Number
|
||||
|
||||
Now that you’ve added the `rand` crate to *Cargo.toml*, let’s start using
|
||||
`rand`. The next step is to update *src/main.rs*, as shown in Listing 2-3.
|
||||
Now that you’ve added the `rand` crate to _Cargo.toml_, let’s start using
|
||||
`rand`. The next step is to update _src/main.rs_, as shown in Listing 2-3.
|
||||
|
||||
<span class="filename">Filename: src/main.rs</span>
|
||||
|
||||
@@ -530,15 +529,14 @@ operating system. Then we call the `gen_range` method on the random number
|
||||
generator. This method is defined by the `Rng` trait that we brought into
|
||||
scope with the `use rand::Rng` statement. The `gen_range` method takes a
|
||||
range expression as an argument and generates a random number in the range.
|
||||
A range expression takes the form start`..`end. It’s inclusive on the lower
|
||||
A range expression takes the form start`..`end. It’s inclusive on the lower
|
||||
bound but exclusive on the upper bound, so we need to specify `1..101` to
|
||||
request a number between 1 and 100. Alternatively, we could pass the range
|
||||
`1..=100`, which is equivalent.
|
||||
|
||||
> Note: You won’t just know which traits to use and which methods and functions
|
||||
> to call from a crate. Instructions for using a crate are in each crate’s
|
||||
> documentation. Another neat feature of Cargo is that you can run the `cargo
|
||||
> doc --open` command, which will build documentation provided by all of your
|
||||
> documentation. Another neat feature of Cargo is that you can run the `cargo doc --open` command, which will build documentation provided by all of your
|
||||
> dependencies locally and open it in your browser. If you’re interested in
|
||||
> other functionality in the `rand` crate, for example, run `cargo doc --open`
|
||||
> and click `rand` in the sidebar on the left.
|
||||
@@ -614,7 +612,7 @@ in `guess` and `secret_number`.
|
||||
|
||||
[match]: ch06-02-match.html
|
||||
|
||||
A `match` expression is made up of *arms*. An arm consists of a *pattern* and
|
||||
A `match` expression is made up of _arms_. An arm consists of a _pattern_ and
|
||||
the code that should be run if the value given to the beginning of the `match`
|
||||
expression fits that arm’s pattern. Rust takes the value given to `match` and
|
||||
looks through each arm’s pattern in turn. The `match` construct and patterns
|
||||
@@ -630,7 +628,7 @@ expression gets the `Ordering::Greater` value and starts checking each arm’s
|
||||
pattern. It looks at the first arm’s pattern, `Ordering::Less`, and sees that
|
||||
the value `Ordering::Greater` does not match `Ordering::Less`, so it ignores
|
||||
the code in that arm and moves to the next arm. The next arm’s pattern,
|
||||
`Ordering::Greater`, *does* match `Ordering::Greater`! The associated code in
|
||||
`Ordering::Greater`, _does_ match `Ordering::Greater`! The associated code in
|
||||
that arm will execute and print `Too big!` to the screen. The `match`
|
||||
expression ends because it has no need to look at the last arm in this scenario.
|
||||
|
||||
@@ -640,7 +638,7 @@ However, the code in Listing 2-4 won’t compile yet. Let’s try it:
|
||||
{{#include ../listings/ch02-guessing-game-tutorial/listing-02-04/output.txt}}
|
||||
```
|
||||
|
||||
The core of the error states that there are *mismatched types*. Rust has a
|
||||
The core of the error states that there are _mismatched types_. Rust has a
|
||||
strong, static type system. However, it also has type inference. When we wrote
|
||||
`let mut guess = String::new()`, Rust was able to infer that `guess` should be
|
||||
a `String` and didn’t make us write the type. The `secret_number`, on the other
|
||||
@@ -668,7 +666,7 @@ let guess: u32 = guess.trim().parse().expect("Please type a number!");
|
||||
```
|
||||
|
||||
We create a variable named `guess`. But wait, doesn’t the program already have
|
||||
a variable named `guess`? It does, but Rust allows us to *shadow* the previous
|
||||
a variable named `guess`? It does, but Rust allows us to _shadow_ the previous
|
||||
value of `guess` with a new one. This feature is often used in situations in
|
||||
which you want to convert a value from one type to another type. Shadowing lets
|
||||
us reuse the `guess` variable name rather than forcing us to create two unique
|
||||
@@ -845,7 +843,7 @@ match the first arm’s pattern, and the `match` expression will just return the
|
||||
`num` value that `parse` produced and put inside the `Ok` value. That number
|
||||
will end up right where we want it in the new `guess` variable we’re creating.
|
||||
|
||||
If `parse` is *not* able to turn the string into a number, it will return an
|
||||
If `parse` is _not_ able to turn the string into a number, it will return an
|
||||
`Err` value that contains more information about the error. The `Err` value
|
||||
does not match the `Ok(num)` pattern in the first `match` arm, but it does
|
||||
match the `Err(_)` pattern in the second arm. The underscore, `_`, is a
|
||||
@@ -915,5 +913,4 @@ Chapter 4 explores ownership, a feature that makes Rust different from other
|
||||
languages. Chapter 5 discusses structs and method syntax, and Chapter 6
|
||||
explains how enums work.
|
||||
|
||||
[variables-and-mutability]:
|
||||
ch03-01-variables-and-mutability.html#variables-and-mutability
|
||||
[variables-and-mutability]: ch03-01-variables-and-mutability.html#variables-and-mutability
|
||||
|
||||
@@ -4,12 +4,12 @@ In Chapter 12, we built a package that included a binary crate and a library
|
||||
crate. As your project develops, you might find that the library crate
|
||||
continues to get bigger and you want to split up your package further into
|
||||
multiple library crates. In this situation, Cargo offers a feature called
|
||||
*workspaces* that can help manage multiple related packages that are developed
|
||||
_workspaces_ that can help manage multiple related packages that are developed
|
||||
in tandem.
|
||||
|
||||
### Creating a Workspace
|
||||
|
||||
A *workspace* is a set of packages that share the same *Cargo.lock* and output
|
||||
A _workspace_ is a set of packages that share the same _Cargo.lock_ and output
|
||||
directory. Let’s make a project using a workspace—we’ll use trivial code so we
|
||||
can concentrate on the structure of the workspace. There are multiple ways to
|
||||
structure a workspace; we’re going to show one common way. We’ll have a
|
||||
@@ -24,12 +24,12 @@ $ mkdir add
|
||||
$ cd add
|
||||
```
|
||||
|
||||
Next, in the *add* directory, we create the *Cargo.toml* file that will
|
||||
Next, in the _add_ directory, we create the _Cargo.toml_ file that will
|
||||
configure the entire workspace. This file won’t have a `[package]` section or
|
||||
the metadata we’ve seen in other *Cargo.toml* files. Instead, it will start
|
||||
the metadata we’ve seen in other _Cargo.toml_ files. Instead, it will start
|
||||
with a `[workspace]` section that will allow us to add members to the workspace
|
||||
by specifying the path to the package with our binary crate; in this case,
|
||||
that path is *adder*:
|
||||
that path is _adder_:
|
||||
|
||||
<span class="filename">Filename: Cargo.toml</span>
|
||||
|
||||
@@ -38,7 +38,7 @@ that path is *adder*:
|
||||
```
|
||||
|
||||
Next, we’ll create the `adder` binary crate by running `cargo new` within the
|
||||
*add* directory:
|
||||
_add_ directory:
|
||||
|
||||
<!-- manual-regeneration
|
||||
cd listings/ch14-more-about-cargo/output-only-01-adder-crate/add
|
||||
@@ -53,7 +53,7 @@ $ cargo new adder
|
||||
```
|
||||
|
||||
At this point, we can build the workspace by running `cargo build`. The files
|
||||
in your *add* directory should look like this:
|
||||
in your _add_ directory should look like this:
|
||||
|
||||
```text
|
||||
├── Cargo.lock
|
||||
@@ -65,21 +65,21 @@ in your *add* directory should look like this:
|
||||
└── target
|
||||
```
|
||||
|
||||
The workspace has one *target* directory at the top level for the compiled
|
||||
artifacts to be placed into; the `adder` package doesn’t have its own *target*
|
||||
directory. Even if we were to run `cargo build` from inside the *adder*
|
||||
directory, the compiled artifacts would still end up in *add/target* rather
|
||||
than *add/adder/target*. Cargo structures the *target* directory in a workspace
|
||||
The workspace has one _target_ directory at the top level for the compiled
|
||||
artifacts to be placed into; the `adder` package doesn’t have its own _target_
|
||||
directory. Even if we were to run `cargo build` from inside the _adder_
|
||||
directory, the compiled artifacts would still end up in _add/target_ rather
|
||||
than _add/adder/target_. Cargo structures the _target_ directory in a workspace
|
||||
like this because the crates in a workspace are meant to depend on each other.
|
||||
If each crate had its own *target* directory, each crate would have to
|
||||
If each crate had its own _target_ directory, each crate would have to
|
||||
recompile each of the other crates in the workspace to have the artifacts in
|
||||
its own *target* directory. By sharing one *target* directory, the crates can
|
||||
its own _target_ directory. By sharing one _target_ directory, the crates can
|
||||
avoid unnecessary rebuilding.
|
||||
|
||||
### Creating the Second Package in the Workspace
|
||||
|
||||
Next, let’s create another member package in the workspace and call it `add-one`.
|
||||
Change the top-level *Cargo.toml* to specify the *add-one* path in the
|
||||
Change the top-level _Cargo.toml_ to specify the _add-one_ path in the
|
||||
`members` list:
|
||||
|
||||
<span class="filename">Filename: Cargo.toml</span>
|
||||
@@ -102,7 +102,7 @@ $ cargo new add-one --lib
|
||||
Created library `add-one` package
|
||||
```
|
||||
|
||||
Your *add* directory should now have these directories and files:
|
||||
Your _add_ directory should now have these directories and files:
|
||||
|
||||
```text
|
||||
├── Cargo.lock
|
||||
@@ -118,7 +118,7 @@ Your *add* directory should now have these directories and files:
|
||||
└── target
|
||||
```
|
||||
|
||||
In the *add-one/src/lib.rs* file, let’s add an `add_one` function:
|
||||
In the _add-one/src/lib.rs_ file, let’s add an `add_one` function:
|
||||
|
||||
<span class="filename">Filename: add-one/src/lib.rs</span>
|
||||
|
||||
@@ -129,7 +129,7 @@ In the *add-one/src/lib.rs* file, let’s add an `add_one` function:
|
||||
Now that we have another package in the workspace, we can have the `adder`
|
||||
package with our binary depend on the `add-one` package, that has our
|
||||
library. First, we’ll need to add a path dependency on `add-one` to
|
||||
*adder/Cargo.toml*.
|
||||
_adder/Cargo.toml_.
|
||||
|
||||
<span class="filename">Filename: adder/Cargo.toml</span>
|
||||
|
||||
@@ -141,7 +141,7 @@ Cargo doesn’t assume that crates in a workspace will depend on each other, so
|
||||
we need to be explicit about the dependency relationships between the crates.
|
||||
|
||||
Next, let’s use the `add_one` function from the `add-one` crate in the `adder`
|
||||
crate. Open the *adder/src/main.rs* file and add a `use` line at the top to
|
||||
crate. Open the _adder/src/main.rs_ file and add a `use` line at the top to
|
||||
bring the new `add-one` library crate into scope. Then change the `main`
|
||||
function to call the `add_one` function, as in Listing 14-7.
|
||||
|
||||
@@ -152,9 +152,9 @@ function to call the `add_one` function, as in Listing 14-7.
|
||||
```
|
||||
|
||||
<span class="caption">Listing 14-7: Using the `add-one` library crate from the
|
||||
`adder` crate</span>
|
||||
`adder` crate</span>
|
||||
|
||||
Let’s build the workspace by running `cargo build` in the top-level *add*
|
||||
Let’s build the workspace by running `cargo build` in the top-level _add_
|
||||
directory!
|
||||
|
||||
<!-- manual-regeneration
|
||||
@@ -170,7 +170,7 @@ $ cargo build
|
||||
Finished dev [unoptimized + debuginfo] target(s) in 0.68s
|
||||
```
|
||||
|
||||
To run the binary crate from the *add* directory, we can specify which
|
||||
To run the binary crate from the _add_ directory, we can specify which
|
||||
package in the workspace we want to run by using the `-p` argument and the
|
||||
package name with `cargo run`:
|
||||
|
||||
@@ -187,19 +187,19 @@ $ cargo run -p adder
|
||||
Hello, world! 10 plus one is 11!
|
||||
```
|
||||
|
||||
This runs the code in *adder/src/main.rs*, which depends on the `add-one` crate.
|
||||
This runs the code in _adder/src/main.rs_, which depends on the `add-one` crate.
|
||||
|
||||
#### Depending on an External Package in a Workspace
|
||||
|
||||
Notice that the workspace has only one *Cargo.lock* file at the top level of
|
||||
the workspace rather than having a *Cargo.lock* in each crate’s directory. This
|
||||
Notice that the workspace has only one _Cargo.lock_ file at the top level of
|
||||
the workspace rather than having a _Cargo.lock_ in each crate’s directory. This
|
||||
ensures that all crates are using the same version of all dependencies. If we
|
||||
add the `rand` package to the *adder/Cargo.toml* and *add-one/Cargo.toml*
|
||||
add the `rand` package to the _adder/Cargo.toml_ and _add-one/Cargo.toml_
|
||||
files, Cargo will resolve both of those to one version of `rand` and record
|
||||
that in the one *Cargo.lock*. Making all crates in the workspace use the same
|
||||
that in the one _Cargo.lock_. Making all crates in the workspace use the same
|
||||
dependencies means the crates in the workspace will always be compatible with
|
||||
each other. Let’s add the `rand` crate to the `[dependencies]` section in the
|
||||
*add-one/Cargo.toml* file to be able to use the `rand` crate in the `add-one`
|
||||
_add-one/Cargo.toml_ file to be able to use the `rand` crate in the `add-one`
|
||||
crate:
|
||||
|
||||
<!-- When updating the version of `rand` used, also update the version of
|
||||
@@ -214,8 +214,8 @@ crate:
|
||||
{{#include ../listings/ch14-more-about-cargo/no-listing-03-workspace-with-external-dependency/add/add-one/Cargo.toml:7:8}}
|
||||
```
|
||||
|
||||
We can now add `use rand;` to the *add-one/src/lib.rs* file, and building the
|
||||
whole workspace by running `cargo build` in the *add* directory will bring in
|
||||
We can now add `use rand;` to the _add-one/src/lib.rs_ file, and building the
|
||||
whole workspace by running `cargo build` in the _add_ directory will bring in
|
||||
and compile the `rand` crate. We will get one warning because we aren’t
|
||||
referring to the `rand` we brought into scope:
|
||||
|
||||
@@ -228,9 +228,9 @@ copy output below; the output updating script doesn't handle subdirectories in p
|
||||
```console
|
||||
$ cargo build
|
||||
Updating crates.io index
|
||||
Downloaded rand v0.8.1
|
||||
Downloaded rand v0.8.3
|
||||
--snip--
|
||||
Compiling rand v0.8.1
|
||||
Compiling rand v0.8.3
|
||||
Compiling add-one v0.1.0 (file:///projects/add/add-one)
|
||||
warning: unused import: `rand`
|
||||
--> add-one/src/lib.rs:1:5
|
||||
@@ -246,11 +246,11 @@ warning: 1 warning emitted
|
||||
Finished dev [unoptimized + debuginfo] target(s) in 10.18s
|
||||
```
|
||||
|
||||
The top-level *Cargo.lock* now contains information about the dependency of
|
||||
The top-level _Cargo.lock_ now contains information about the dependency of
|
||||
`add-one` on `rand`. However, even though `rand` is used somewhere in the
|
||||
workspace, we can’t use it in other crates in the workspace unless we add
|
||||
`rand` to their *Cargo.toml* files as well. For example, if we add `use rand;`
|
||||
to the *adder/src/main.rs* file for the `adder` package, we’ll get an error:
|
||||
`rand` to their _Cargo.toml_ files as well. For example, if we add `use rand;`
|
||||
to the _adder/src/main.rs_ file for the `adder` package, we’ll get an error:
|
||||
|
||||
<!-- manual-regeneration
|
||||
cd listings/ch14-more-about-cargo/output-only-03-use-rand/add
|
||||
@@ -269,9 +269,9 @@ error[E0432]: unresolved import `rand`
|
||||
| ^^^^ no external crate `rand`
|
||||
```
|
||||
|
||||
To fix this, edit the *Cargo.toml* file for the `adder` package and indicate
|
||||
To fix this, edit the _Cargo.toml_ file for the `adder` package and indicate
|
||||
that `rand` is a dependency for it as well. Building the `adder` package will
|
||||
add `rand` to the list of dependencies for `adder` in *Cargo.lock*, but no
|
||||
add `rand` to the list of dependencies for `adder` in _Cargo.lock_, but no
|
||||
additional copies of `rand` will be downloaded. Cargo has ensured that every
|
||||
crate in every package in the workspace using the `rand` package will be
|
||||
using the same version. Using the same version of `rand` across the workspace
|
||||
@@ -289,7 +289,7 @@ within the `add_one` crate:
|
||||
{{#rustdoc_include ../listings/ch14-more-about-cargo/no-listing-04-workspace-with-tests/add/add-one/src/lib.rs}}
|
||||
```
|
||||
|
||||
Now run `cargo test` in the top-level *add* directory:
|
||||
Now run `cargo test` in the top-level _add_ directory:
|
||||
|
||||
<!-- manual-regeneration
|
||||
cd listings/ch14-more-about-cargo/no-listing-04-workspace-with-tests/add
|
||||
@@ -359,8 +359,7 @@ This output shows `cargo test` only ran the tests for the `add-one` crate and
|
||||
didn’t run the `adder` crate tests.
|
||||
|
||||
If you publish the crates in the workspace to [crates.io](https://crates.io/),
|
||||
each crate in the workspace will need to be published separately. The `cargo
|
||||
publish` command does not have an `--all` flag or a `-p` flag, so you must
|
||||
each crate in the workspace will need to be published separately. The `cargo publish` command does not have an `--all` flag or a `-p` flag, so you must
|
||||
change to each crate’s directory and run `cargo publish` on each crate in the
|
||||
workspace to publish the crates.
|
||||
|
||||
|
||||
Reference in New Issue
Block a user