mirror of
https://github.com/rust-lang/book.git
synced 2026-11-30 20:24:32 -06:00
rebuild pages at 64a3a20
This commit is contained in:
@@ -77,6 +77,7 @@ these common concepts.</p>
|
||||
comments, and control flow. These foundations will be in every Rust
|
||||
program, and learning them early will give you a strong core to start from.</p>
|
||||
<!-- PROD: START BOX -->
|
||||
<blockquote>
|
||||
<p>Keep in mind as we get into variables and functions that the Rust language has
|
||||
a set of <em>keywords</em> 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
|
||||
@@ -85,6 +86,7 @@ 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.</p>
|
||||
</blockquote>
|
||||
<!-- PROD: END BOX -->
|
||||
|
||||
</div>
|
||||
|
||||
@@ -106,17 +106,18 @@ your program isn't safely doing what you want it to do yet; they do <em>not</em>
|
||||
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.</p>
|
||||
<!-- PROD: START BOX -->
|
||||
###### Extended Error Explanations
|
||||
<blockquote>
|
||||
<h4>Extended Error Explanations</h4>
|
||||
<p>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:</p>
|
||||
<pre><code class="language-bash">error: re-assignment of immutable variable `x` [--explain E0384]
|
||||
</code></pre>
|
||||
<p>This tells us that if we pass the <code>--explain</code> flag to <code>rustc</code> 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 <code>E0384</code>
|
||||
error we received:</p>
|
||||
<p>This tells us that if we pass the <code>--explain</code> flag to <code>rustc</code> 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 <code>E0384</code> error we received:</p>
|
||||
<pre><code class="language-bash">$ rustc --explain E0384
|
||||
This error occurs when an attempt is made to reassign an immutable variable.
|
||||
For example:
|
||||
@@ -129,8 +130,9 @@ fn main(){
|
||||
```
|
||||
</code></pre>
|
||||
<p>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.</p>
|
||||
hesitate to look up the error code. The compiler is your friend, and it's
|
||||
there to help.</p>
|
||||
</blockquote>
|
||||
<!-- PROD: END BOX -->
|
||||
<p>The error tells us that the cause of the error is <code>re-assignment of immutable variable</code>, because we tried to assign a second value to the immutable <code>x</code>
|
||||
variable.</p>
|
||||
|
||||
+28
-26
@@ -84,13 +84,14 @@ able to naturally develop code that is both safe and efficient. Keep at it!</p>
|
||||
features that make Rust unique. In this chapter, we'll learn ownership by going
|
||||
through some examples, focusing on a very common data structure: strings.</p>
|
||||
<!-- PROD: START BOX -->
|
||||
<h6>The Stack and the Heap</h6>
|
||||
<blockquote>
|
||||
<h4>The Stack and the Heap</h4>
|
||||
<p>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.</p>
|
||||
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.</p>
|
||||
<p>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.
|
||||
@@ -99,35 +100,36 @@ 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 <em>pushing onto the stack</em>
|
||||
and removing data is called <em>popping off the stack</em>.</p>
|
||||
<p>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.</p>
|
||||
<p>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.</p>
|
||||
<p>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
|
||||
<em>allocating on the heap</em>, 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.</p>
|
||||
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 <em>allocating on the heap</em>, 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.</p>
|
||||
<p>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.</p>
|
||||
<p>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.</p>
|
||||
<p>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.</p>
|
||||
<p>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.</p>
|
||||
<p>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.</p>
|
||||
<p>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.</p>
|
||||
</blockquote>
|
||||
<!-- PROD: END BOX -->
|
||||
<h3>Ownership Rules</h3>
|
||||
<p>First, let's take a look at the rules. Keep these in mind as we go through the
|
||||
|
||||
@@ -127,6 +127,7 @@ signature, is for organization. We've put all the things we can do with an
|
||||
instance of a type together in one <code>impl</code> block, rather than make future users
|
||||
of our code search for capabilities of <code>Rectangle</code> all over the place.</p>
|
||||
<!-- PROD: START BOX -->
|
||||
<blockquote>
|
||||
<h4>Where's the <code>-></code> operator?</h4>
|
||||
<p>In languages like C++, there are two different operators for calling methods:
|
||||
<code>.</code> if you're calling a method on the object directly, and <code>-></code> if you're
|
||||
@@ -164,6 +165,7 @@ receiver and name of a method, Rust can figure out definitively whether the
|
||||
method is just reading (so needs <code>&self</code>), mutating (so <code>&mut self</code>), or
|
||||
consuming (so <code>self</code>). The fact that Rust makes borrowing implicit for method
|
||||
receivers is a big part of making ownership ergonomic in practice.</p>
|
||||
</blockquote>
|
||||
<!-- PROD: END BOX -->
|
||||
<h3>Methods with More Arguments</h3>
|
||||
<p>Let's practice some more with methods by implementing a second method on our
|
||||
|
||||
+42
-34
@@ -1270,6 +1270,7 @@ these common concepts.</p>
|
||||
comments, and control flow. These foundations will be in every Rust
|
||||
program, and learning them early will give you a strong core to start from.</p>
|
||||
<!-- PROD: START BOX -->
|
||||
<blockquote>
|
||||
<p>Keep in mind as we get into variables and functions that the Rust language has
|
||||
a set of <em>keywords</em> 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
|
||||
@@ -1278,6 +1279,7 @@ 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.</p>
|
||||
</blockquote>
|
||||
<!-- PROD: END BOX -->
|
||||
<h2>Variables and Mutability</h2>
|
||||
<p>We mentioned in Chapter 2 that by default, variables are <em>immutable</em>.
|
||||
@@ -1318,17 +1320,18 @@ your program isn't safely doing what you want it to do yet; they do <em>not</em>
|
||||
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.</p>
|
||||
<!-- PROD: START BOX -->
|
||||
###### Extended Error Explanations
|
||||
<blockquote>
|
||||
<h4>Extended Error Explanations</h4>
|
||||
<p>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:</p>
|
||||
<pre><code class="language-bash">error: re-assignment of immutable variable `x` [--explain E0384]
|
||||
</code></pre>
|
||||
<p>This tells us that if we pass the <code>--explain</code> flag to <code>rustc</code> 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 <code>E0384</code>
|
||||
error we received:</p>
|
||||
<p>This tells us that if we pass the <code>--explain</code> flag to <code>rustc</code> 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 <code>E0384</code> error we received:</p>
|
||||
<pre><code class="language-bash">$ rustc --explain E0384
|
||||
This error occurs when an attempt is made to reassign an immutable variable.
|
||||
For example:
|
||||
@@ -1341,8 +1344,9 @@ fn main(){
|
||||
```
|
||||
</code></pre>
|
||||
<p>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.</p>
|
||||
hesitate to look up the error code. The compiler is your friend, and it's
|
||||
there to help.</p>
|
||||
</blockquote>
|
||||
<!-- PROD: END BOX -->
|
||||
<p>The error tells us that the cause of the error is <code>re-assignment of immutable variable</code>, because we tried to assign a second value to the immutable <code>x</code>
|
||||
variable.</p>
|
||||
@@ -2372,13 +2376,14 @@ able to naturally develop code that is both safe and efficient. Keep at it!</p>
|
||||
features that make Rust unique. In this chapter, we'll learn ownership by going
|
||||
through some examples, focusing on a very common data structure: strings.</p>
|
||||
<!-- PROD: START BOX -->
|
||||
<h6>The Stack and the Heap</h6>
|
||||
<blockquote>
|
||||
<h4>The Stack and the Heap</h4>
|
||||
<p>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.</p>
|
||||
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.</p>
|
||||
<p>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.
|
||||
@@ -2387,35 +2392,36 @@ 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 <em>pushing onto the stack</em>
|
||||
and removing data is called <em>popping off the stack</em>.</p>
|
||||
<p>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.</p>
|
||||
<p>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.</p>
|
||||
<p>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
|
||||
<em>allocating on the heap</em>, 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.</p>
|
||||
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 <em>allocating on the heap</em>, 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.</p>
|
||||
<p>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.</p>
|
||||
<p>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.</p>
|
||||
<p>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.</p>
|
||||
<p>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.</p>
|
||||
<p>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.</p>
|
||||
<p>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.</p>
|
||||
</blockquote>
|
||||
<!-- PROD: END BOX -->
|
||||
<h3>Ownership Rules</h3>
|
||||
<p>First, let's take a look at the rules. Keep these in mind as we go through the
|
||||
@@ -3574,6 +3580,7 @@ signature, is for organization. We've put all the things we can do with an
|
||||
instance of a type together in one <code>impl</code> block, rather than make future users
|
||||
of our code search for capabilities of <code>Rectangle</code> all over the place.</p>
|
||||
<!-- PROD: START BOX -->
|
||||
<blockquote>
|
||||
<h4>Where's the <code>-></code> operator?</h4>
|
||||
<p>In languages like C++, there are two different operators for calling methods:
|
||||
<code>.</code> if you're calling a method on the object directly, and <code>-></code> if you're
|
||||
@@ -3611,6 +3618,7 @@ receiver and name of a method, Rust can figure out definitively whether the
|
||||
method is just reading (so needs <code>&self</code>), mutating (so <code>&mut self</code>), or
|
||||
consuming (so <code>self</code>). The fact that Rust makes borrowing implicit for method
|
||||
receivers is a big part of making ownership ergonomic in practice.</p>
|
||||
</blockquote>
|
||||
<!-- PROD: END BOX -->
|
||||
<h3>Methods with More Arguments</h3>
|
||||
<p>Let's practice some more with methods by implementing a second method on our
|
||||
|
||||
Reference in New Issue
Block a user