mirror of
https://github.com/rust-lang/book.git
synced 2026-11-30 19:54:34 -06:00
Change single quotes in prose to italics or double quotes
This commit is contained in:
@@ -166,7 +166,7 @@ Cargo is Rust’s build system and package manager, and Rustaceans use Cargo to
|
||||
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
|
||||
|
||||
@@ -33,7 +33,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`:
|
||||
|
||||
```rust
|
||||
@@ -88,7 +88,7 @@ use std::io;
|
||||
|
||||
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’][prelude]. If it’s not in the
|
||||
by default into every program, [the *prelude*][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.
|
||||
|
||||
@@ -119,14 +119,14 @@ let mut guess = String::new();
|
||||
|
||||
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:
|
||||
|
||||
```rust,ignore
|
||||
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
|
||||
@@ -149,10 +149,10 @@ bound to: `String::new()`.
|
||||
|
||||
[string]: ../std/string/struct.String.html
|
||||
|
||||
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
|
||||
@@ -194,7 +194,7 @@ 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
|
||||
@@ -308,9 +308,9 @@ print it back out.
|
||||
|
||||
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][randcrate]. 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][randcrate]. 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.
|
||||
|
||||
[randcrate]: https://crates.io/crates/rand
|
||||
@@ -398,9 +398,9 @@ 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
|
||||
@@ -449,7 +449,7 @@ 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.
|
||||
|
||||
@@ -549,7 +549,7 @@ The `cmp()` method can be called on anything that can be compared, and it takes
|
||||
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`][match]
|
||||
statement to determine exactly what kind of `Ordering` it is. `Ordering` is an
|
||||
[`enum`][enum], short for ‘enumeration’, which looks like this:
|
||||
[`enum`][enum], short for *enumeration*, which looks like this:
|
||||
|
||||
```rust
|
||||
enum Foo {
|
||||
@@ -567,7 +567,7 @@ namespace for a particular `enum` variant.
|
||||
|
||||
The [`Ordering`][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:
|
||||
|
||||
@@ -599,7 +599,7 @@ 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
|
||||
@@ -656,7 +656,7 @@ let guess: u32 = guess.trim().parse()
|
||||
```
|
||||
|
||||
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
|
||||
@@ -671,8 +671,8 @@ guess.trim().parse()
|
||||
Here, `guess` refers to the old `guess`, the one that was a `String` with our
|
||||
input in it. The `trim()` method on `String`s 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][parse] parses a string into some kind of
|
||||
@@ -878,8 +878,8 @@ let guess: u32 = match guess.trim().parse() {
|
||||
};
|
||||
```
|
||||
|
||||
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
|
||||
|
||||
@@ -151,7 +151,7 @@ depends on the tradeoffs you want to make in your situation.
|
||||
|
||||
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
|
||||
|
||||
@@ -214,7 +214,7 @@ fn main() {
|
||||
|
||||
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`.
|
||||
|
||||
@@ -39,7 +39,7 @@ All `if` expressions start with `if`, which is followed by a condition. In this
|
||||
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.
|
||||
|
||||
@@ -211,7 +211,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 {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
## 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.
|
||||
|
||||
@@ -48,8 +48,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:
|
||||
|
||||
- When `s` comes ‘into scope’, it is valid.
|
||||
- It remains so until it ‘goes out of scope’.
|
||||
- When `s` comes "into scope", it is valid.
|
||||
- It remains so until it "goes out of scope".
|
||||
|
||||
At this point, things are similar to other programming languages. Now let’s
|
||||
build on top of this understanding by introducing the `String` type.
|
||||
@@ -106,8 +106,8 @@ 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
|
||||
@@ -249,12 +249,12 @@ 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’.
|
||||
> 1. Each value in Rust has a variable binding that’s called its *owner*.
|
||||
> 2. There can only be one owner at a time.
|
||||
> 3. 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
|
||||
|
||||
@@ -46,7 +46,7 @@ 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)
|
||||
@@ -85,7 +85,7 @@ 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.
|
||||
|
||||
@@ -248,7 +248,7 @@ 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:
|
||||
|
||||
```bash
|
||||
|
||||
@@ -78,7 +78,7 @@ modules and namespaces in depth yet, but you can think of the `powi()` function
|
||||
as being scoped inside of another name. In this case, the name is `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.
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
## 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:
|
||||
|
||||
```rust
|
||||
let s1 = "hello";
|
||||
@@ -12,8 +12,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
|
||||
@@ -37,7 +37,7 @@ 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:
|
||||
|
||||
```rust
|
||||
#[derive(Debug,Copy,Clone)]
|
||||
@@ -134,7 +134,7 @@ assert_eq!(8.200609733428363, p1.distance(&p2));
|
||||
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 `&mut`s to match the signature. In other words, these are the same:
|
||||
|
||||
@@ -5,7 +5,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:
|
||||
@@ -60,7 +60,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
|
||||
|
||||
@@ -67,13 +67,13 @@ figure out which style is better, but it’s mostly a judgement call.
|
||||
|
||||
## 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:
|
||||
|
||||
```rust
|
||||
fn main() {
|
||||
|
||||
@@ -46,8 +46,8 @@ 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:
|
||||
|
||||
```rust
|
||||
fn plus_one(x: i32) -> i32 {
|
||||
@@ -109,7 +109,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.
|
||||
|
||||
Reference in New Issue
Block a user