mirror of
https://github.com/rust-lang/book.git
synced 2026-11-30 20:24:32 -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 -->
|
||||
|
||||
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
|
||||
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
|
||||
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
|
||||
might be in the Rust language in the future. You can find a list of the
|
||||
keywords in Appendix XX.
|
||||
> 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
|
||||
> 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
|
||||
> 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
|
||||
> might be in the Rust language in the future. You can find a list of the
|
||||
> keywords in Appendix XX.
|
||||
|
||||
<!-- 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.
|
||||
|
||||
<!-- PROD: START BOX -->
|
||||
###### Extended Error Explanations
|
||||
|
||||
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
|
||||
information on the kind of error you've received with output like this:
|
||||
|
||||
```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
|
||||
explanation, but many do. Here’s a portion of the explanation for the `E0384`
|
||||
error we received:
|
||||
|
||||
````bash
|
||||
$ 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
|
||||
}
|
||||
```
|
||||
````
|
||||
|
||||
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.
|
||||
> #### Extended Error Explanations
|
||||
>
|
||||
> 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
|
||||
> information on the kind of error you've received with output like this:
|
||||
>
|
||||
> ```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 explanation, but many do. Here’s a portion of the explanation
|
||||
> for the `E0384` error we received:
|
||||
>
|
||||
> ````bash
|
||||
> $ 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
|
||||
> }
|
||||
> ```
|
||||
> ````
|
||||
>
|
||||
> 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 -->
|
||||
|
||||
|
||||
+52
-52
@@ -21,58 +21,58 @@ through some examples, focusing on a very common data structure: strings.
|
||||
|
||||
<!-- PROD: START BOX -->
|
||||
|
||||
###### The Stack and the Heap
|
||||
|
||||
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
|
||||
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
|
||||
parts of ownership in relation to the stack and the heap, so here is a brief
|
||||
explanation.
|
||||
|
||||
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
|
||||
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
|
||||
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
|
||||
bottom wouldn't work as well! Adding data is called *pushing onto 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
|
||||
around for a place to put new data or a place to get data from; that place is
|
||||
always the top. Another property that makes the stack fast is that 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
|
||||
change, we can store data on the heap instead. The heap is less organized: when
|
||||
we put data on the heap, we ask for some amount of space. The operating system
|
||||
finds an empty spot somewhere in the heap that is big enough, marks it as being
|
||||
in use, and returns to us a pointer to that location. This process is called
|
||||
*allocating on the heap*, and sometimes we just say "allocating" for short.
|
||||
Pushing values onto the stack is not considered allocating. Since the pointer
|
||||
is a known, fixed size, we can store the pointer on the stack, 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
|
||||
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
|
||||
you have been seated to find you.
|
||||
|
||||
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.
|
||||
|
||||
When our code calls a function, the values passed into the function (including,
|
||||
potentially, pointers to data on the heap) and the function's local variables
|
||||
get pushed onto the stack. When the function is over, those values get popped
|
||||
off the stack.
|
||||
|
||||
Keeping track of what parts of code are using what data on the heap, minimizing
|
||||
the amount of duplicate data on the heap, and cleaning up unused data on the
|
||||
heap so that we don't run out of space are all problems that ownership
|
||||
addresses. Once you understand ownership, you won't need to think about the
|
||||
stack and the heap very often, but knowing that managing heap data is why
|
||||
ownership exists can help explain why it works the way it does.
|
||||
> #### The Stack and the Heap
|
||||
>
|
||||
> 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
|
||||
> 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 parts of ownership in relation to the stack and the heap, so here
|
||||
> is a brief explanation.
|
||||
>
|
||||
> 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
|
||||
> 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
|
||||
> 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
|
||||
> bottom wouldn't work as well! Adding data is called *pushing onto 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 around for a place to put new data or a place to get data from; that
|
||||
> place is always the top. Another property that makes the stack fast is that
|
||||
> 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
|
||||
> change, we can store data on the heap instead. The heap is less organized:
|
||||
> when we put data on the heap, we ask for some amount of space. The operating
|
||||
> system finds an empty spot somewhere in the heap that is big enough, marks it
|
||||
> as being in use, and returns to us a pointer to that location. This process
|
||||
> is called *allocating on the heap*, and sometimes we just say "allocating"
|
||||
> for short. Pushing values onto the stack is not considered allocating. Since
|
||||
> the pointer is a known, fixed size, we can store the pointer on the stack,
|
||||
> 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
|
||||
> 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
|
||||
> you have been seated to find you.
|
||||
>
|
||||
> 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.
|
||||
>
|
||||
> When our code calls a function, the values passed into the function
|
||||
> (including, potentially, pointers to data on the heap) and the function's
|
||||
> local variables get pushed onto the stack. When the function is over, those
|
||||
> values get popped off the stack.
|
||||
>
|
||||
> Keeping track of what parts of code are using what data on the heap,
|
||||
> minimizing the amount of duplicate data on the heap, and cleaning up unused
|
||||
> data on the heap so that we don't run out of space are all problems that
|
||||
> ownership addresses. Once you understand ownership, you won't need to think
|
||||
> about the stack and the heap very often, but knowing that managing heap data
|
||||
> is why ownership exists can help explain why it works the way it does.
|
||||
|
||||
<!-- PROD: END BOX -->
|
||||
|
||||
|
||||
@@ -71,49 +71,49 @@ of our code search for capabilities of `Rectangle` all over the place.
|
||||
|
||||
<!-- PROD: START BOX -->
|
||||
|
||||
#### Where's the `->` operator?
|
||||
|
||||
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
|
||||
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()`
|
||||
is like `(*object).something()`.
|
||||
|
||||
Rust doesn't have an equivalent to the `->` operator; instead, Rust has a
|
||||
feature called *automatic referencing and dereferencing*. 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 `object.something()`, Rust
|
||||
will automatically add in `&`, `&mut`, or `*` so that `object` matches the
|
||||
signature of the method. In other words, these are the same:
|
||||
|
||||
```rust
|
||||
# #[derive(Debug,Copy,Clone)]
|
||||
# struct Point {
|
||||
# x: f64,
|
||||
# y: f64,
|
||||
# }
|
||||
#
|
||||
# impl Point {
|
||||
# fn distance(&self, other: &Point) -> f64 {
|
||||
# let x_squared = f64::powi(other.x - self.x, 2);
|
||||
# let y_squared = f64::powi(other.y - self.y, 2);
|
||||
#
|
||||
# f64::sqrt(x_squared + y_squared)
|
||||
# }
|
||||
# }
|
||||
# let p1 = Point { x: 0.0, y: 0.0 };
|
||||
# let p2 = Point { x: 5.0, y: 6.5 };
|
||||
p1.distance(&p2);
|
||||
(&p1).distance(&p2);
|
||||
```
|
||||
|
||||
The first one looks much, much cleaner. This automatic referencing behavior
|
||||
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
|
||||
method is just reading (so needs `&self`), mutating (so `&mut self`), or
|
||||
consuming (so `self`). The fact that Rust makes borrowing implicit for method
|
||||
receivers is a big part of making ownership ergonomic in practice.
|
||||
> #### Where's the `->` operator?
|
||||
>
|
||||
> 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
|
||||
> 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()`
|
||||
> is like `(*object).something()`.
|
||||
>
|
||||
> Rust doesn't have an equivalent to the `->` operator; instead, Rust has a
|
||||
> feature called *automatic referencing and dereferencing*. 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 `object.something()`, Rust
|
||||
> will automatically add in `&`, `&mut`, or `*` so that `object` matches the
|
||||
> signature of the method. In other words, these are the same:
|
||||
>
|
||||
> ```rust
|
||||
> # #[derive(Debug,Copy,Clone)]
|
||||
> # struct Point {
|
||||
> # x: f64,
|
||||
> # y: f64,
|
||||
> # }
|
||||
> #
|
||||
> # impl Point {
|
||||
> # fn distance(&self, other: &Point) -> f64 {
|
||||
> # let x_squared = f64::powi(other.x - self.x, 2);
|
||||
> # let y_squared = f64::powi(other.y - self.y, 2);
|
||||
> #
|
||||
> # f64::sqrt(x_squared + y_squared)
|
||||
> # }
|
||||
> # }
|
||||
> # let p1 = Point { x: 0.0, y: 0.0 };
|
||||
> # let p2 = Point { x: 5.0, y: 6.5 };
|
||||
> p1.distance(&p2);
|
||||
> (&p1).distance(&p2);
|
||||
> ```
|
||||
>
|
||||
> The first one looks much, much cleaner. This automatic referencing behavior
|
||||
> 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
|
||||
> method is just reading (so needs `&self`), mutating (so `&mut self`), or
|
||||
> consuming (so `self`). The fact that Rust makes borrowing implicit for method
|
||||
> receivers is a big part of making ownership ergonomic in practice.
|
||||
|
||||
<!-- PROD: END BOX -->
|
||||
|
||||
|
||||
Reference in New Issue
Block a user