diff --git a/ch03-00-common-programming-concepts-in-rust.html b/ch03-00-common-programming-concepts-in-rust.html index 57c823dfa..ff66aebda 100644 --- a/ch03-00-common-programming-concepts-in-rust.html +++ b/ch03-00-common-programming-concepts-in-rust.html @@ -77,6 +77,7 @@ these common concepts.

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.

+

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 @@ -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.

+
diff --git a/ch03-01-variables-and-mutability.html b/ch03-01-variables-and-mutability.html index 6232f763b..cff3af9c6 100644 --- a/ch03-01-variables-and-mutability.html +++ b/ch03-01-variables-and-mutability.html @@ -106,17 +106,18 @@ your program isn't safely doing what you want it to do yet; they do not 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.

-###### Extended Error Explanations +
+

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:

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:

+

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:

$ rustc --explain E0384
 This error occurs when an attempt is made to reassign an immutable variable.
 For example:
@@ -129,8 +130,9 @@ fn main(){
 ```
 

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.

+hesitate to look up the error code. The compiler is your friend, and it's +there to help.

+

The error tells us that the cause of the error is re-assignment of immutable variable, because we tried to assign a second value to the immutable x variable.

diff --git a/ch04-01-ownership.html b/ch04-01-ownership.html index f3fa79f08..6556d13aa 100644 --- a/ch04-01-ownership.html +++ b/ch04-01-ownership.html @@ -84,13 +84,14 @@ able to naturally develop code that is both safe and efficient. Keep at it!

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.

-
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 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.

+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. @@ -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 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.

+

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.

+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.

+

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.

+

Ownership Rules

First, let's take a look at the rules. Keep these in mind as we go through the diff --git a/ch05-01-method-syntax.html b/ch05-01-method-syntax.html index 3ac1e9308..7d9161f51 100644 --- a/ch05-01-method-syntax.html +++ b/ch05-01-method-syntax.html @@ -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 impl block, rather than make future users of our code search for capabilities of Rectangle all over the place.

+

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 @@ -164,6 +165,7 @@ 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.

+

Methods with More Arguments

Let's practice some more with methods by implementing a second method on our diff --git a/print.html b/print.html index 7e0645483..bc85a1974 100644 --- a/print.html +++ b/print.html @@ -1270,6 +1270,7 @@ these common concepts.

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.

+

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 @@ -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.

+

Variables and Mutability

We mentioned in Chapter 2 that by default, variables are immutable. @@ -1318,17 +1320,18 @@ your program isn't safely doing what you want it to do yet; they do not 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.

-###### Extended Error Explanations +
+

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:

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:

+

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:

$ rustc --explain E0384
 This error occurs when an attempt is made to reassign an immutable variable.
 For example:
@@ -1341,8 +1344,9 @@ fn main(){
 ```
 

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.

+hesitate to look up the error code. The compiler is your friend, and it's +there to help.

+

The error tells us that the cause of the error is re-assignment of immutable variable, because we tried to assign a second value to the immutable x variable.

@@ -2372,13 +2376,14 @@ able to naturally develop code that is both safe and efficient. Keep at it!

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.

-
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 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.

+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. @@ -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 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.

+

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.

+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.

+

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.

+

Ownership Rules

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 impl block, rather than make future users of our code search for capabilities of Rectangle all over the place.

+

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 @@ -3611,6 +3618,7 @@ 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.

+

Methods with More Arguments

Let's practice some more with methods by implementing a second method on our