mirror of
https://github.com/rust-lang/book.git
synced 2026-11-30 20:34:52 -06:00
Ignore some links for print now that we have a script to remove!!!
This commit is contained in:
@@ -29,9 +29,9 @@ Rust is installed now. Great!
|
|||||||
|
|
||||||
### Installing on Windows
|
### Installing on Windows
|
||||||
|
|
||||||
If you're on Windows, please go to [rustup.rs](https://rustup.rs/) and follow
|
On Windows, go to [https://rustup.rs](https://rustup.rs/)<!-- ignore --> and
|
||||||
the instructions to download rustup-init.exe. Run that and follow the rest of
|
follow the instructions to download rustup-init.exe. Run that and follow the
|
||||||
the instructions.
|
rest of the instructions it gives you.
|
||||||
|
|
||||||
The rest of the Windows-specific commands in the book will assume that you are
|
The rest of the Windows-specific commands in the book will assume that you are
|
||||||
using `cmd` as your shell. If you use a different shell, you may be able to run
|
using `cmd` as your shell. If you use a different shell, you may be able to run
|
||||||
@@ -74,11 +74,11 @@ If you don't and you're on Windows, check that Rust is in your `%PATH%` system
|
|||||||
variable.
|
variable.
|
||||||
|
|
||||||
If it still isn't working, there are a number of places where you can get help.
|
If it still isn't working, there are a number of places where you can get help.
|
||||||
The easiest is [the #rust IRC channel on irc.mozilla.org][irc], which you can
|
The easiest is [the #rust IRC channel on irc.mozilla.org][irc]<!-- ignore -->,
|
||||||
access through [Mibbit][mibbit]. Go to that address, and you'll be chatting with
|
which you can access through [Mibbit][mibbit]. Go to that address, and you'll
|
||||||
other Rustaceans (a silly nickname we call ourselves) who can help you out.
|
be chatting with other Rustaceans (a silly nickname we call ourselves) who can
|
||||||
Other great resources include [the user’s forum][users] and [Stack
|
help you out. Other great resources include [the user’s forum][users] and
|
||||||
Overflow][stackoverflow].
|
[Stack Overflow][stackoverflow].
|
||||||
|
|
||||||
[irc]: irc://irc.mozilla.org/#rust
|
[irc]: irc://irc.mozilla.org/#rust
|
||||||
[mibbit]: http://chat.mibbit.com/?server=irc.mozilla.org&channel=%23rust
|
[mibbit]: http://chat.mibbit.com/?server=irc.mozilla.org&channel=%23rust
|
||||||
|
|||||||
@@ -249,11 +249,11 @@ authors = ["Your Name <you@example.com>"]
|
|||||||
[dependencies]
|
[dependencies]
|
||||||
```
|
```
|
||||||
|
|
||||||
This file is in the *[TOML]* (Tom's Obvious, Minimal Language) format. TOML is
|
This file is in the [*TOML*][toml]<!-- ignore --> (Tom's Obvious, Minimal
|
||||||
similar to INI but has some extra goodies and is used as Cargo’s
|
Language) format. TOML is similar to INI but has some extra goodies and is used
|
||||||
configuration format.
|
as Cargo’s configuration format.
|
||||||
|
|
||||||
[TOML]: https://github.com/toml-lang/toml
|
[toml]: https://github.com/toml-lang/toml
|
||||||
|
|
||||||
The first line, `[package]`, is a section heading that indicates that the
|
The first line, `[package]`, is a section heading that indicates that the
|
||||||
following statements are configuring a package. As we add more information to
|
following statements are configuring a package. As we add more information to
|
||||||
|
|||||||
@@ -106,10 +106,10 @@ functionality we need to import the `io` (input/output) library from the
|
|||||||
standard library (which is known as `std`).
|
standard library (which is known as `std`).
|
||||||
|
|
||||||
By default, Rust only imports a few things into every program in [the
|
By default, Rust only imports a few things into every program in [the
|
||||||
*prelude*][prelude]. If it’s not in the prelude, you’ll have to import it into
|
*prelude*][prelude]<!-- ignore -->. If it’s not in the prelude, you’ll have to
|
||||||
your program explicitly with a `use` statement. Using the `std::io` library
|
import it into your program explicitly with a `use` statement. Using the
|
||||||
gets you a number of useful `io`-related things, including the functionality to
|
`std::io` library gets you a number of useful `io`-related things, including
|
||||||
accept user input.
|
the functionality to accept user input.
|
||||||
|
|
||||||
[prelude]: https://doc.rust-lang.org/std/prelude/
|
[prelude]: https://doc.rust-lang.org/std/prelude/
|
||||||
|
|
||||||
@@ -167,7 +167,7 @@ So now we know that `let mut guess` will introduce a mutable binding named
|
|||||||
bound to: `String::new()`.
|
bound to: `String::new()`.
|
||||||
|
|
||||||
`String` is a string type, provided by the standard library. A
|
`String` is a string type, provided by the standard library. A
|
||||||
[`String`][string] is a growable, UTF-8 encoded bit of text.
|
[`String`][string]<!-- ignore --> is a growable, UTF-8 encoded bit of text.
|
||||||
|
|
||||||
[string]: ../std/string/struct.String.html
|
[string]: ../std/string/struct.String.html
|
||||||
|
|
||||||
@@ -195,15 +195,16 @@ Remember how we said `use std::io;` on the first line of the program? We’re no
|
|||||||
calling an associated function on it. If we didn’t `use std::io`, we could
|
calling an associated function on it. If we didn’t `use std::io`, we could
|
||||||
have written this line as `std::io::stdin()`.
|
have written this line as `std::io::stdin()`.
|
||||||
|
|
||||||
This particular function returns an instance of [`std::io::Stdin`][iostdin],
|
This function returns an instance of [`std::io::Stdin`][iostdin]<!-- ignore -->,
|
||||||
which is a type that represents a handle to the standard input for your
|
which is a type that represents a handle to the standard input for your
|
||||||
terminal.
|
terminal.
|
||||||
|
|
||||||
[iostdin]: ../std/io/struct.Stdin.html
|
[iostdin]: ../std/io/struct.Stdin.html
|
||||||
|
|
||||||
The next part, `.read_line(&mut guess)`, calls the [`read_line()`][read_line]
|
The next part, `.read_line(&mut guess)`, calls the
|
||||||
method on the standard input handle to get input from the user. We’re also
|
[`read_line()`][read_line]<!-- ignore --> method on the standard input handle
|
||||||
passing one argument to `read_line()`: `&mut guess`.
|
to 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
|
[read_line]: ../std/io/struct.Stdin.html#method.read_line
|
||||||
|
|
||||||
@@ -245,35 +246,36 @@ calls. Now let's see what this line does.
|
|||||||
|
|
||||||
We mentioned that `read_line()` puts what the user types into the string we
|
We mentioned that `read_line()` puts what the user types into the string we
|
||||||
pass it, but it also returns a value: in this case, an
|
pass it, but it also returns a value: in this case, an
|
||||||
[`io::Result`][ioresult]. Rust has a number of types named `Result` in its
|
[`io::Result`][ioresult]<!-- ignore -->. Rust has a number of types named
|
||||||
standard library: a generic [`Result`][result], and then specific versions for
|
`Result` in its standard library: a generic [`Result`][result]<!-- ignore -->,
|
||||||
sub-libraries, like `io::Result`.
|
and then specific versions for sub-libraries, like `io::Result`.
|
||||||
|
|
||||||
[ioresult]: ../std/io/type.Result.html
|
[ioresult]: ../std/io/type.Result.html
|
||||||
[result]: ../std/result/enum.Result.html
|
[result]: ../std/result/enum.Result.html
|
||||||
|
|
||||||
The `Result` types are [enums], which is short for *enumeration*. An
|
The `Result` types are [enums][enums]<!-- ignore -->, which is short for
|
||||||
enumeration is a type that can have a fixed set of values, which are called the
|
*enumeration*. An enumeration is a type that can have a fixed set of values,
|
||||||
`enum`'s *variants*. We will be covering enums in more detail in Chapter XX.
|
which are called the `enum`'s *variants*. We will be covering enums in more
|
||||||
|
detail in Chapter XX.
|
||||||
|
|
||||||
|
[enums]: ch06-00-enums.html
|
||||||
|
|
||||||
For `Result`, the variants are `Ok` or `Err`. `Ok` means the operation was
|
For `Result`, the variants are `Ok` or `Err`. `Ok` means the operation was
|
||||||
successful, and inside the `Ok` variant is the successfully generated value.
|
successful, and inside the `Ok` variant is the successfully generated value.
|
||||||
`Err` means the operation failed, and the `Err` contains information about how
|
`Err` means the operation failed, and the `Err` contains information about how
|
||||||
or why the operation failed.
|
or why the operation failed.
|
||||||
|
|
||||||
[enums]: ch06-00-enums.html
|
|
||||||
|
|
||||||
The purpose of these `Result` types is to encode error handling information.
|
The purpose of these `Result` types is to encode error handling information.
|
||||||
Values of the `Result` type, like any type, have methods defined on them. In
|
Values of the `Result` type, like any type, have methods defined on them. In
|
||||||
this case, `io::Result` has an [`expect()` method][expect] that we can call. If
|
this case, `io::Result` has an [`expect()` method][expect]<!-- ignore --> that
|
||||||
this instance of `io::Result` is an `Err` value, `expect()` will cause our
|
we can call. If this instance of `io::Result` is an `Err` value, `expect()`
|
||||||
program to crash and display the message that we passed as an argument to
|
will cause our program to crash and display the message that we passed as an
|
||||||
`expect()`. In this case, if the `read_line()` method returns an `Err`, it would
|
argument to `expect()`. In this case, if the `read_line()` method returns an
|
||||||
likely be the result of an error coming from the underlying operating system.
|
`Err`, it would likely be the result of an error coming from the underlying
|
||||||
If this instance of `io::Result` is an `Ok` value, `expect()` will take the
|
operating system. If this instance of `io::Result` is an `Ok` value, `expect()`
|
||||||
return value that `Ok` is holding out of the `Ok` and return just that value to
|
will take the return value that `Ok` is holding out of the `Ok` and return just
|
||||||
us so that we can use it. In this case, that value will be what the user
|
that value to us so that we can use it. In this case, that value will be what
|
||||||
entered into standard input.
|
the user entered into standard input.
|
||||||
|
|
||||||
[expect]: ../std/result/enum.Result.html#method.expect
|
[expect]: ../std/result/enum.Result.html#method.expect
|
||||||
|
|
||||||
@@ -370,10 +372,10 @@ that goes until another section starts. Cargo uses the `[dependencies]` section
|
|||||||
to know what external crates your project depends on and what versions of those
|
to know what external crates your project depends on and what versions of those
|
||||||
crates you require. In this case, we’ve specified the `rand` crate with the
|
crates you require. In this case, we’ve specified the `rand` crate with the
|
||||||
semantic version specifier `0.3.14`. Cargo understands [Semantic
|
semantic version specifier `0.3.14`. Cargo understands [Semantic
|
||||||
Versioning][semver] (sometimes called *semver*), which is a standard for
|
Versioning][semver]<!-- ignore --> (sometimes called *semver*), which is a
|
||||||
writing version numbers. A bare number like above is actually shorthand for
|
standard for writing version numbers. A bare number like above is actually
|
||||||
`^0.3.14`, which means "any version that has a public API compatible with
|
shorthand for `^0.3.14`, which means "any version that has a public API
|
||||||
version 0.3.14".
|
compatible with version 0.3.14".
|
||||||
|
|
||||||
[semver]: http://semver.org
|
[semver]: http://semver.org
|
||||||
|
|
||||||
@@ -477,11 +479,11 @@ The next time we `cargo build`, assuming that the `rand` crate version `0.4.0`
|
|||||||
has been released, Cargo will update the crates index and re-evaluate our
|
has been released, Cargo will update the crates index and re-evaluate our
|
||||||
`rand` requirements according to the new version we have specified.
|
`rand` requirements according to the new version we have specified.
|
||||||
|
|
||||||
There’s a lot more to say about [Cargo][doccargo] and [its
|
There’s a lot more to say about [Cargo][doccargo]<!-- ignore --> and [its
|
||||||
ecosystem][doccratesio] that we will get into in Chapter XX, but for now,
|
ecosystem][doccratesio]<!-- ignore --> that we will get into in Chapter XX, but
|
||||||
that’s all we need to know. Cargo makes it really easy to re-use libraries, so
|
for now, that’s all we need to know. Cargo makes it really easy to re-use
|
||||||
Rustaceans are able to write smaller projects which are assembled out of a
|
libraries, so Rustaceans are able to write smaller projects which are assembled
|
||||||
number of sub-packages.
|
out of a number of sub-packages.
|
||||||
|
|
||||||
[doccargo]: http://doc.crates.io
|
[doccargo]: http://doc.crates.io
|
||||||
[doccratesio]: http://doc.crates.io/crates-io.html
|
[doccratesio]: http://doc.crates.io/crates-io.html
|
||||||
@@ -638,8 +640,9 @@ match guess.cmp(&secret_number) {
|
|||||||
The `cmp()` method compares two values, and can be called on anything that can
|
The `cmp()` method compares two values, and can be called on anything that can
|
||||||
be compared. It takes a reference to the thing you want to compare it to, so
|
be compared. It takes a reference to the thing you want to compare it to, so
|
||||||
here it's comparing our `guess` to our `secret_number`, and it returns a
|
here it's comparing our `guess` to our `secret_number`, and it returns a
|
||||||
variant of the `Ordering` enum. We use a [`match`][match] statement to decide
|
variant of the `Ordering` enum. We use a [`match`][match]<!-- ignore -->
|
||||||
what to do next based on which variant of `Ordering` we got back.
|
statement to decide what to do next based on which variant of `Ordering` we got
|
||||||
|
back.
|
||||||
|
|
||||||
[match]: match.html
|
[match]: match.html
|
||||||
|
|
||||||
@@ -754,15 +757,15 @@ if we type `5` and hit return, `guess` looks like this: `5\n`. The `\n`
|
|||||||
represents "newline", the return key. The `trim()` method gets rid of this,
|
represents "newline", the return key. The `trim()` method gets rid of this,
|
||||||
leaving our string with only the `5`.
|
leaving our string with only the `5`.
|
||||||
|
|
||||||
The [`parse()` method on strings][parse] parses a string into some kind of
|
The [`parse()` method on strings][parse]<!-- ignore --> parses a string into
|
||||||
number. Since this method can parse a variety of number types, we need to tell
|
some kind of number. Since this method can parse a variety of number types, we
|
||||||
Rust the exact type of number we want with `let guess: u32`. The colon (`:`)
|
need to tell Rust the exact type of number we want with `let guess: u32`. The
|
||||||
after `guess` tells Rust we’re going to annotate its type. Rust has a few
|
colon (`:`) after `guess` tells Rust we’re going to annotate its type. Rust has
|
||||||
built-in number types, but we’ve chosen `u32`, an unsigned, thirty-two bit
|
a few built-in number types, but we’ve chosen `u32`, an unsigned, thirty-two
|
||||||
integer. It’s a good default choice for a small positive number. Additionally,
|
bit integer. It’s a good default choice for a small positive number.
|
||||||
our `u32` annotation here and the comparison with `secret_number` means that
|
Additionally, our `u32` annotation here and the comparison with `secret_number`
|
||||||
Rust will infer that `secret_number` should be a `u32` as well. So now the
|
means that Rust will infer that `secret_number` should be a `u32` as well. So
|
||||||
comparison will be between two values of the same type!
|
now the comparison will be between two values of the same type!
|
||||||
|
|
||||||
[parse]: ../std/primitive.str.html#method.parse
|
[parse]: ../std/primitive.str.html#method.parse
|
||||||
|
|
||||||
|
|||||||
@@ -261,9 +261,9 @@ copying can be assumed to be inexpensive.
|
|||||||
|
|
||||||
But what if we _do_ want to deeply copy the `String`’s data and not just the
|
But what if we _do_ want to deeply copy the `String`’s data and not just the
|
||||||
`String` itself? There’s a common method for that: `clone()`. We will discuss
|
`String` itself? There’s a common method for that: `clone()`. We will discuss
|
||||||
methods in the section on [`structs` in Chapter XX][structs], but they’re a
|
methods in the section on [`structs` in Chapter XX][structs]<!-- ignore -->,
|
||||||
common enough feature in many programming languages that you have probably seen
|
but they’re a common enough feature in many programming languages that you have
|
||||||
them before.
|
probably seen them before.
|
||||||
|
|
||||||
[structs]: ch05-01-structs.html
|
[structs]: ch05-01-structs.html
|
||||||
|
|
||||||
|
|||||||
@@ -173,13 +173,13 @@ s.push_str(" world!");
|
|||||||
assert_eq!("Hello, world!", s);
|
assert_eq!("Hello, world!", s);
|
||||||
```
|
```
|
||||||
|
|
||||||
Because [`push_str()`] has the following signature:
|
Because [`push_str()`][pushstr]<!-- ignore --> has the following signature:
|
||||||
|
|
||||||
```rust,ignore
|
```rust,ignore
|
||||||
fn push_str(&mut self, string: &str) {
|
fn push_str(&mut self, string: &str) {
|
||||||
```
|
```
|
||||||
|
|
||||||
[`push_str()`]: ../collections/string/struct.String.html#method.push_str
|
[push_str]: ../collections/string/struct.String.html#method.push_str
|
||||||
|
|
||||||
This automatic referencing behavior works because methods have a clear receiver
|
This automatic referencing behavior works because methods have a clear receiver
|
||||||
— the type of `self` — and in most cases it’s clear given the receiver and name
|
— the type of `self` — and in most cases it’s clear given the receiver and name
|
||||||
|
|||||||
@@ -98,8 +98,8 @@ let loopback = IpAddr::V6(String::from("::1"));
|
|||||||
```
|
```
|
||||||
|
|
||||||
You can put any kind of data inside of an enum variant, including another enum!
|
You can put any kind of data inside of an enum variant, including another enum!
|
||||||
The `IpAddr` enum is [in the standard library][IpAddr], but it embeds two
|
The `IpAddr` enum is [in the standard library][IpAddr]<!-- ignore -->, but it
|
||||||
different structs inside of its variants:
|
embeds two different structs inside of its variants:
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
struct Ipv4Addr {
|
struct Ipv4Addr {
|
||||||
|
|||||||
@@ -40,10 +40,10 @@ enum Option<T> {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
This enum is [provided by the standard library][option], and is so useful that
|
This enum is [provided by the standard library][option]<!-- ignore -->, and is
|
||||||
it's even in the prelude; you don't need to import it explicitly. Furthermore,
|
so useful that it's even in the prelude; you don't need to import it
|
||||||
so are its variants: you can say `Some` and `None` directly, without prefixing
|
explicitly. Furthermore, so are its variants: you can say `Some` and `None`
|
||||||
them with `Option::`.
|
directly, without prefixing them with `Option::`.
|
||||||
|
|
||||||
[option]: ../std/option/enum.Option.html
|
[option]: ../std/option/enum.Option.html
|
||||||
|
|
||||||
@@ -100,10 +100,11 @@ deliberate design decision for Rust to limit null's pervasiveness and increase
|
|||||||
the safety of Rust code.
|
the safety of Rust code.
|
||||||
|
|
||||||
So, how _do_ you get a `T` from an `Option<T>`? The `Option<T>` enum has a
|
So, how _do_ you get a `T` from an `Option<T>`? The `Option<T>` enum has a
|
||||||
large number of methods that you can check out in [its documentation], and
|
large number of methods that you can check out in
|
||||||
becoming familiar with them will be extremely useful in your journey with Rust.
|
[its documentation][docs]<!-- ignore -->, and becoming familiar with them will
|
||||||
|
be extremely useful in your journey with Rust.
|
||||||
|
|
||||||
[its documentation]: ../std/option/enum.Option.html
|
[docs]: ../std/option/enum.Option.html
|
||||||
|
|
||||||
But we want a deeper understanding than that. If we didn't have those methods
|
But we want a deeper understanding than that. If we didn't have those methods
|
||||||
defined for us already, what would we do? And more generally, how do we get
|
defined for us already, what would we do? And more generally, how do we get
|
||||||
|
|||||||
Reference in New Issue
Block a user