mirror of
https://github.com/rust-lang/book.git
synced 2026-11-30 21:41:18 -06:00
Use blockquotes to make boxes in online version too
This commit is contained in:
@@ -12,13 +12,13 @@ program, and learning them early will give you a strong core to start from.
|
|||||||
|
|
||||||
<!-- PROD: START BOX -->
|
<!-- PROD: START BOX -->
|
||||||
|
|
||||||
Keep in mind as we get into variables and functions that the Rust language has
|
> Keep in mind as we get into variables and functions that the Rust language has
|
||||||
a set of *keywords* that have been reserved for use by the language only, much
|
> a set of *keywords* that have been reserved for use by the language only, much
|
||||||
like other languages do. This means you cannot use these words as names of
|
> like other languages do. This means you cannot use these words as names of
|
||||||
variables or functions, for example. Most of these have special meaning and we
|
> variables or functions, for example. Most of these have special meaning and we
|
||||||
will be using them to do various things in our Rust programs; a few have no
|
> will be using them to do various things in our Rust programs; a few have no
|
||||||
current functionality associated but have been reserved for functionality that
|
> current functionality associated but have been reserved for functionality that
|
||||||
might be in the Rust language in the future. You can find a list of the
|
> might be in the Rust language in the future. You can find a list of the
|
||||||
keywords in Appendix XX.
|
> keywords in Appendix XX.
|
||||||
|
|
||||||
<!-- PROD: END BOX -->
|
<!-- PROD: END BOX -->
|
||||||
|
|||||||
@@ -48,38 +48,39 @@ that you're not a good programmer! Experienced Rustaceans still get compiler
|
|||||||
errors. The Rust compiler is trying to help your program be the very best.
|
errors. The Rust compiler is trying to help your program be the very best.
|
||||||
|
|
||||||
<!-- PROD: START BOX -->
|
<!-- PROD: START BOX -->
|
||||||
###### Extended Error Explanations
|
|
||||||
|
|
||||||
Now that you've seen a Rust error, let's take a moment to look at one
|
> #### Extended Error Explanations
|
||||||
particularly useful aspect of errors. Rust encourages you to seek further
|
>
|
||||||
information on the kind of error you've received with output like this:
|
> Now that you've seen a Rust error, let's take a moment to look at one
|
||||||
|
> particularly useful aspect of errors. Rust encourages you to seek further
|
||||||
```bash
|
> information on the kind of error you've received with output like this:
|
||||||
error: re-assignment of immutable variable `x` [--explain E0384]
|
>
|
||||||
```
|
> ```bash
|
||||||
|
> error: re-assignment of immutable variable `x` [--explain E0384]
|
||||||
This tells us that if we pass the `--explain` flag to `rustc` with the provided
|
> ```
|
||||||
error code, we can see an extended explanation which will try to explain common
|
>
|
||||||
causes of and solutions to that kind of error. Not every error has a longer
|
> This tells us that if we pass the `--explain` flag to `rustc` with the
|
||||||
explanation, but many do. Here’s a portion of the explanation for the `E0384`
|
> provided error code, we can see an extended explanation which will try to
|
||||||
error we received:
|
> explain common causes of and solutions to that kind of error. Not every error
|
||||||
|
> has a longer explanation, but many do. Here’s a portion of the explanation
|
||||||
````bash
|
> for the `E0384` error we received:
|
||||||
$ rustc --explain E0384
|
>
|
||||||
This error occurs when an attempt is made to reassign an immutable variable.
|
> ````bash
|
||||||
For example:
|
> $ rustc --explain E0384
|
||||||
|
> This error occurs when an attempt is made to reassign an immutable variable.
|
||||||
```
|
> For example:
|
||||||
fn main(){
|
>
|
||||||
let x = 3;
|
> ```
|
||||||
x = 5; // error, reassignment of immutable variable
|
> fn main(){
|
||||||
}
|
> let x = 3;
|
||||||
```
|
> x = 5; // error, reassignment of immutable variable
|
||||||
````
|
> }
|
||||||
|
> ```
|
||||||
These explanations can really help if you’re stuck on an error, so don't
|
> ````
|
||||||
hesitate to look up the error code. The compiler is your friend, and it's there
|
>
|
||||||
to help.
|
> These explanations can really help if you’re stuck on an error, so don't
|
||||||
|
> hesitate to look up the error code. The compiler is your friend, and it's
|
||||||
|
> there to help.
|
||||||
|
|
||||||
<!-- PROD: END BOX -->
|
<!-- PROD: END BOX -->
|
||||||
|
|
||||||
|
|||||||
+52
-52
@@ -21,58 +21,58 @@ through some examples, focusing on a very common data structure: strings.
|
|||||||
|
|
||||||
<!-- PROD: START BOX -->
|
<!-- PROD: START BOX -->
|
||||||
|
|
||||||
###### The Stack and the Heap
|
> #### The Stack and the Heap
|
||||||
|
>
|
||||||
In many programming languages, we don't have to think about the stack and the
|
> In many programming languages, we don't have to think about the stack and the
|
||||||
heap very often. But in a systems programming language like Rust, whether a
|
> heap very often. But in a systems programming language like Rust, whether a
|
||||||
value is on the stack or the heap has more of an effect on how the language
|
> value is on the stack or the heap has more of an effect on how the language
|
||||||
behaves and why we have to make certain decisions. We're going to be describing
|
> behaves and why we have to make certain decisions. We're going to be
|
||||||
parts of ownership in relation to the stack and the heap, so here is a brief
|
> describing parts of ownership in relation to the stack and the heap, so here
|
||||||
explanation.
|
> is a brief explanation.
|
||||||
|
>
|
||||||
Both the stack and the heap are parts of memory that is available to your code
|
> Both the stack and the heap are parts of memory that is available to your code
|
||||||
to use at runtime, but they are structured in different ways. The stack stores
|
> to use at runtime, but they are structured in different ways. The stack stores
|
||||||
values in the order it gets them and removes the values in the opposite order.
|
> values in the order it gets them and removes the values in the opposite order.
|
||||||
This is referred to as *last in, first out*. Think of a stack of plates: when
|
> This is referred to as *last in, first out*. Think of a stack of plates: when
|
||||||
you add more plates, you put them on top of the pile, and when you need a
|
> you add more plates, you put them on top of the pile, and when you need a
|
||||||
plate, you take one off the top. Adding or removing plates from the middle or
|
> plate, you take one off the top. Adding or removing plates from the middle or
|
||||||
bottom wouldn't work as well! Adding data is called *pushing onto the stack*
|
> bottom wouldn't work as well! Adding data is called *pushing onto the stack*
|
||||||
and removing data is called *popping off the stack*.
|
> and removing data is called *popping off the stack*.
|
||||||
|
>
|
||||||
The stack is fast because of the way it accesses the data: it never has to look
|
> The stack is fast because of the way it accesses the data: it never has to
|
||||||
around for a place to put new data or a place to get data from; that place is
|
> look around for a place to put new data or a place to get data from; that
|
||||||
always the top. Another property that makes the stack fast is that all data on
|
> place is always the top. Another property that makes the stack fast is that
|
||||||
the stack must take up a known, fixed size.
|
> all data on the stack must take up a known, fixed size.
|
||||||
|
>
|
||||||
For data with a size unknown to us at compile time, or a size that might
|
> For data with a size unknown to us at compile time, or a size that might
|
||||||
change, we can store data on the heap instead. The heap is less organized: when
|
> change, we can store data on the heap instead. The heap is less organized:
|
||||||
we put data on the heap, we ask for some amount of space. The operating system
|
> when we put data on the heap, we ask for some amount of space. The operating
|
||||||
finds an empty spot somewhere in the heap that is big enough, marks it as being
|
> system finds an empty spot somewhere in the heap that is big enough, marks it
|
||||||
in use, and returns to us a pointer to that location. This process is called
|
> as being in use, and returns to us a pointer to that location. This process
|
||||||
*allocating on the heap*, and sometimes we just say "allocating" for short.
|
> is called *allocating on the heap*, and sometimes we just say "allocating"
|
||||||
Pushing values onto the stack is not considered allocating. Since the pointer
|
> for short. Pushing values onto the stack is not considered allocating. Since
|
||||||
is a known, fixed size, we can store the pointer on the stack, but when we want
|
> the pointer is a known, fixed size, we can store the pointer on the stack,
|
||||||
the actual data, we have to follow the pointer.
|
> but when we want the actual data, we have to follow the pointer.
|
||||||
|
>
|
||||||
Think of being seated at a restaurant. When you enter, you say how many people
|
> Think of being seated at a restaurant. When you enter, you say how many people
|
||||||
are in your group, and the staff finds an empty table that would fit everyone
|
> are in your group, and the staff finds an empty table that would fit everyone
|
||||||
and leads you there. If someone in your group comes late, they can ask where
|
> and leads you there. If someone in your group comes late, they can ask where
|
||||||
you have been seated to find you.
|
> you have been seated to find you.
|
||||||
|
>
|
||||||
Accessing data in the heap is slower because we have to follow a pointer to
|
> Accessing data in the heap is slower because we have to follow a pointer to
|
||||||
get there. Allocating a large amount of space can also take time.
|
> get there. Allocating a large amount of space can also take time.
|
||||||
|
>
|
||||||
When our code calls a function, the values passed into the function (including,
|
> When our code calls a function, the values passed into the function
|
||||||
potentially, pointers to data on the heap) and the function's local variables
|
> (including, potentially, pointers to data on the heap) and the function's
|
||||||
get pushed onto the stack. When the function is over, those values get popped
|
> local variables get pushed onto the stack. When the function is over, those
|
||||||
off the stack.
|
> values get popped off the stack.
|
||||||
|
>
|
||||||
Keeping track of what parts of code are using what data on the heap, minimizing
|
> Keeping track of what parts of code are using what data on the heap,
|
||||||
the amount of duplicate data on the heap, and cleaning up unused data on the
|
> minimizing the amount of duplicate data on the heap, and cleaning up unused
|
||||||
heap so that we don't run out of space are all problems that ownership
|
> data on the heap so that we don't run out of space are all problems that
|
||||||
addresses. Once you understand ownership, you won't need to think about the
|
> ownership addresses. Once you understand ownership, you won't need to think
|
||||||
stack and the heap very often, but knowing that managing heap data is why
|
> about the stack and the heap very often, but knowing that managing heap data
|
||||||
ownership exists can help explain why it works the way it does.
|
> is why ownership exists can help explain why it works the way it does.
|
||||||
|
|
||||||
<!-- PROD: END BOX -->
|
<!-- PROD: END BOX -->
|
||||||
|
|
||||||
|
|||||||
@@ -71,49 +71,49 @@ of our code search for capabilities of `Rectangle` all over the place.
|
|||||||
|
|
||||||
<!-- PROD: START BOX -->
|
<!-- PROD: START BOX -->
|
||||||
|
|
||||||
#### Where's the `->` operator?
|
> #### Where's the `->` operator?
|
||||||
|
>
|
||||||
In languages like C++, there are two different operators for calling methods:
|
> In languages like C++, there are two different operators for calling methods:
|
||||||
`.` if you're calling a method on the object directly, and `->` if you're
|
> `.` if you're calling a method on the object directly, and `->` if you're
|
||||||
calling the method on a pointer to the object and thus need to dereference the
|
> calling the method on a pointer to the object and thus need to dereference the
|
||||||
pointer first. In other words, if `object` is a pointer, `object->something()`
|
> pointer first. In other words, if `object` is a pointer, `object->something()`
|
||||||
is like `(*object).something()`.
|
> is like `(*object).something()`.
|
||||||
|
>
|
||||||
Rust doesn't have an equivalent to the `->` operator; instead, Rust has a
|
> Rust doesn't have an equivalent to the `->` operator; instead, Rust has a
|
||||||
feature called *automatic referencing and dereferencing*. Calling methods is
|
> feature called *automatic referencing and dereferencing*. Calling methods is
|
||||||
one of the few places in Rust that has behavior like this.
|
> one of the few places in Rust that has behavior like this.
|
||||||
|
>
|
||||||
Here’s how it works: when you call a method with `object.something()`, Rust
|
> Here’s how it works: when you call a method with `object.something()`, Rust
|
||||||
will automatically add in `&`, `&mut`, or `*` so that `object` matches the
|
> will automatically add in `&`, `&mut`, or `*` so that `object` matches the
|
||||||
signature of the method. In other words, these are the same:
|
> signature of the method. In other words, these are the same:
|
||||||
|
>
|
||||||
```rust
|
> ```rust
|
||||||
# #[derive(Debug,Copy,Clone)]
|
> # #[derive(Debug,Copy,Clone)]
|
||||||
# struct Point {
|
> # struct Point {
|
||||||
# x: f64,
|
> # x: f64,
|
||||||
# y: f64,
|
> # y: f64,
|
||||||
# }
|
> # }
|
||||||
#
|
> #
|
||||||
# impl Point {
|
> # impl Point {
|
||||||
# fn distance(&self, other: &Point) -> f64 {
|
> # fn distance(&self, other: &Point) -> f64 {
|
||||||
# let x_squared = f64::powi(other.x - self.x, 2);
|
> # let x_squared = f64::powi(other.x - self.x, 2);
|
||||||
# let y_squared = f64::powi(other.y - self.y, 2);
|
> # let y_squared = f64::powi(other.y - self.y, 2);
|
||||||
#
|
> #
|
||||||
# f64::sqrt(x_squared + y_squared)
|
> # f64::sqrt(x_squared + y_squared)
|
||||||
# }
|
> # }
|
||||||
# }
|
> # }
|
||||||
# let p1 = Point { x: 0.0, y: 0.0 };
|
> # let p1 = Point { x: 0.0, y: 0.0 };
|
||||||
# let p2 = Point { x: 5.0, y: 6.5 };
|
> # let p2 = Point { x: 5.0, y: 6.5 };
|
||||||
p1.distance(&p2);
|
> p1.distance(&p2);
|
||||||
(&p1).distance(&p2);
|
> (&p1).distance(&p2);
|
||||||
```
|
> ```
|
||||||
|
>
|
||||||
The first one looks much, much cleaner. This automatic referencing behavior
|
> The first one looks much, much cleaner. This automatic referencing behavior
|
||||||
works because methods have a clear receiver — the type of `self`. Given the
|
> works because methods have a clear receiver — the type of `self`. Given the
|
||||||
receiver and name of a method, Rust can figure out definitively whether the
|
> receiver and name of a method, Rust can figure out definitively whether the
|
||||||
method is just reading (so needs `&self`), mutating (so `&mut self`), or
|
> method is just reading (so needs `&self`), mutating (so `&mut self`), or
|
||||||
consuming (so `self`). The fact that Rust makes borrowing implicit for method
|
> consuming (so `self`). The fact that Rust makes borrowing implicit for method
|
||||||
receivers is a big part of making ownership ergonomic in practice.
|
> receivers is a big part of making ownership ergonomic in practice.
|
||||||
|
|
||||||
<!-- PROD: END BOX -->
|
<!-- PROD: END BOX -->
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user