Skip to content

Commit

Permalink
rebuild GitHub Pages from generated-book
Browse files Browse the repository at this point in the history
  • Loading branch information
chriskrycho committed Oct 8, 2024
1 parent 9550acc commit a92b897
Show file tree
Hide file tree
Showing 35 changed files with 2,023 additions and 1,616 deletions.
3 changes: 1 addition & 2 deletions appendix-06-translation.html
Original file line number Diff line number Diff line change
Expand Up @@ -188,9 +188,8 @@ <h2 id="appendix-f-translations-of-the-book"><a class="header" href="#appendix-f
<li><a href="https://github.com/nunojesus/rust-book-pt-pt">Português</a> (PT)</li>
<li><a href="https://github.com/KaiserY/trpl-zh-cn">简体中文</a></li>
<li><a href="https://github.com/rust-tw/book-tw">正體中文</a></li>
<li><a href="https://github.com/pavloslav/rust-book-uk-ua">Українська</a></li>
<li><a href="https://rust-lang-ua.github.io/rustbook_ukrainian">Українська</a></li>
<li><a href="https://github.com/thecodix/book">Español</a>, <a href="https://github.com/ManRR/rust-book-es">alternate</a></li>
<li><a href="https://github.com/EmanueleGurini/book_it">Italiano</a></li>
<li><a href="https://github.com/rust-lang-ru/book">Русский</a></li>
<li><a href="https://github.com/rinthel/rust-lang-book-ko">한국어</a></li>
<li><a href="https://github.com/rust-lang-ja/book-ja">日本語</a></li>
Expand Down
Binary file added ch02/.DS_Store
Binary file not shown.
16 changes: 8 additions & 8 deletions ch03-02-data-types.html
Original file line number Diff line number Diff line change
Expand Up @@ -437,14 +437,14 @@ <h4 id="the-array-type"><a class="header" href="#the-array-type">The Array Type<
<pre><pre class="playground"><code class="language-rust edition2021">fn main() {
let a = [1, 2, 3, 4, 5];
}</code></pre></pre>
<p>Arrays are useful when you want your data allocated on the stack rather than
the heap (we will discuss the stack and the heap more in <a href="ch04-01-what-is-ownership.html#the-stack-and-the-heap">Chapter
4</a><!-- ignore -->) or when you want to ensure you always have a
fixed number of elements. An array isn’t as flexible as the vector type,
though. A <em>vector</em> is a similar collection type provided by the standard
library that <em>is</em> allowed to grow or shrink in size. If you’re unsure whether
to use an array or a vector, chances are you should use a vector. <a href="ch08-01-vectors.html">Chapter
8</a><!-- ignore --> discusses vectors in more detail.</p>
<p>Arrays are useful when you want your data allocated on the stack, the same as
the other types we have seen so far, rather than the heap (we will discuss the
stack and the heap more in <a href="ch04-01-what-is-ownership.html#the-stack-and-the-heap">Chapter 4</a><!-- ignore -->) or when
you want to ensure you always have a fixed number of elements. An array isn’t as
flexible as the vector type, though. A <em>vector</em> is a similar collection type
provided by the standard library that <em>is</em> allowed to grow or shrink in size. If
you’re unsure whether to use an array or a vector, chances are you should use a
vector. <a href="ch08-01-vectors.html">Chapter 8</a><!-- ignore --> discusses vectors in more detail.</p>
<p>However, arrays are more useful when you know the number of elements will not
need to change. For example, if you were using the names of the month in a
program, you would probably use an array rather than a vector because you know
Expand Down
57 changes: 45 additions & 12 deletions ch04-01-what-is-ownership.html
Original file line number Diff line number Diff line change
Expand Up @@ -280,15 +280,16 @@ <h3 id="variable-scope"><a class="header" href="#variable-scope">Variable Scope<
hardcoded into the text of our program. The variable is valid from the point at
which it’s declared until the end of the current <em>scope</em>. Listing 4-1 shows a
program with comments annotating where the variable <code>s</code> would be valid.</p>
<figure class="listing">
<pre><pre class="playground"><code class="language-rust edition2021"><span class="boring">fn main() {
</span> { // s is not valid here, it’s not yet declared
let s = "hello"; // s is valid from this point forward

// do stuff with s
} // this scope is now over, and s is no longer valid
<span class="boring">}</span></code></pre></pre>
<p><span class="caption">Listing 4-1: A variable and the scope in which it is
valid</span></p>
<figcaption>Listing 4-1: A variable and the scope in which it is valid</figcaption>
</figure>
<p>In other words, there are two important points in time here:</p>
<ul>
<li>When <code>s</code> comes <em>into</em> scope, it is valid.</li>
Expand Down Expand Up @@ -398,12 +399,13 @@ <h3 id="memory-and-allocation"><a class="header" href="#memory-and-allocation">M
<h4 id="variables-and-data-interacting-with-move"><a class="header" href="#variables-and-data-interacting-with-move">Variables and Data Interacting with Move</a></h4>
<p>Multiple variables can interact with the same data in different ways in Rust.
Let’s look at an example using an integer in Listing 4-2.</p>
<figure class="listing">
<pre><pre class="playground"><code class="language-rust edition2021"><span class="boring">fn main() {
</span> let x = 5;
let y = x;
<span class="boring">}</span></code></pre></pre>
<p><span class="caption">Listing 4-2: Assigning the integer value of variable <code>x</code>
to <code>y</code></span></p>
<figcaption>Listing 4-2: Assigning the integer value of variable <code>x</code> to <code>y</code></figcaption>
</figure>
<p>We can probably guess what this is doing: “bind the value <code>5</code> to <code>x</code>; then make
a copy of the value in <code>x</code> and bind it to <code>y</code>.” We now have two variables, <code>x</code>
and <code>y</code>, and both equal <code>5</code>. This is indeed what is happening, because integers
Expand Down Expand Up @@ -511,6 +513,33 @@ <h4 id="variables-and-data-interacting-with-move"><a class="header" href="#varia
<p>In addition, there’s a design choice that’s implied by this: Rust will never
automatically create “deep” copies of your data. Therefore, any <em>automatic</em>
copying can be assumed to be inexpensive in terms of runtime performance.</p>
<h4 id="scope-and-assignment"><a class="header" href="#scope-and-assignment">Scope and Assignment</a></h4>
<p>The inverse of this is true for the relationship between scoping, ownership, and
memory being freed via the <code>drop</code> function as well. When you assign a completely
new value to an existing variable, Rust will call <code>drop</code> and free the original
value’s memory immediately. Consider this code, for example:</p>
<pre><pre class="playground"><code class="language-rust edition2021"><span class="boring">fn main() {
</span> let mut s = String::from("hello");
s = String::from("ahoy");

println!("{s}, world!");
<span class="boring">}</span></code></pre></pre>
<p>We initially declare a variable <code>s</code> and bind it to a <code>String</code> with the value
<code>"hello"</code>. Then we immediately create a new <code>String</code> with the value <code>"ahoy"</code> and
assign it to <code>s</code>. At this point, nothing is referring to the original value on
the heap at all.</p>
<p><img alt="One table s representing the string value on the stack, pointing to
the second piece of string data (ahoy) on the heap, with the original string
data (hello) grayed out because it cannot be accessed anymore."
src="img/trpl04-05.svg"
class="center"
style="width: 50%;"
/></p>
<p><span class="caption">Figure 4-5: Representation in memory after the initial
value has been replaced in its entirety.</span></p>
<p>The original string thus immediately goes out of scope. Rust will run the <code>drop</code>
function on it and its memory will be freed right away. When we print the value
at the end, it will be <code>"ahoy, world!"</code>.</p>
<!-- Old heading. Do not remove or links may break. -->
<p><a id="ways-variables-and-data-interact-clone"></a></p>
<h4 id="variables-and-data-interacting-with-clone"><a class="header" href="#variables-and-data-interacting-with-clone">Variables and Data Interacting with Clone</a></h4>
Expand Down Expand Up @@ -576,7 +605,8 @@ <h3 id="ownership-and-functions"><a class="header" href="#ownership-and-function
assigning a value to a variable. Passing a variable to a function will move or
copy, just as assignment does. Listing 4-3 has an example with some annotations
showing where variables go into and out of scope.</p>
<p><span class="filename">Filename: src/main.rs</span></p>
<figure class="listing">
<span class="file-name">Filename: src/main.rs</span>
<pre><pre class="playground"><code class="language-rust edition2021">fn main() {
let s = String::from("hello"); // s comes into scope

Expand All @@ -600,8 +630,8 @@ <h3 id="ownership-and-functions"><a class="header" href="#ownership-and-function
fn makes_copy(some_integer: i32) { // some_integer comes into scope
println!("{some_integer}");
} // Here, some_integer goes out of scope. Nothing special happens.</code></pre></pre>
<p><span class="caption">Listing 4-3: Functions with ownership and scope
annotated</span></p>
<figcaption>Listing 4-3: Functions with ownership and scope annotated</figcaption>
</figure>
<p>If we tried to use <code>s</code> after the call to <code>takes_ownership</code>, Rust would throw a
compile-time error. These static checks protect us from mistakes. Try adding
code to <code>main</code> that uses <code>s</code> and <code>x</code> to see where you can use them and where
Expand All @@ -610,7 +640,8 @@ <h3 id="return-values-and-scope"><a class="header" href="#return-values-and-scop
<p>Returning values can also transfer ownership. Listing 4-4 shows an example of a
function that returns some value, with similar annotations as those in Listing
4-3.</p>
<p><span class="filename">Filename: src/main.rs</span></p>
<figure class="listing">
<span class="file-name">Filename: src/main.rs</span>
<pre><pre class="playground"><code class="language-rust edition2021">fn main() {
let s1 = gives_ownership(); // gives_ownership moves its return
// value into s1
Expand Down Expand Up @@ -640,8 +671,8 @@ <h3 id="return-values-and-scope"><a class="header" href="#return-values-and-scop

a_string // a_string is returned and moves out to the calling function
}</code></pre></pre>
<p><span class="caption">Listing 4-4: Transferring ownership of return
values</span></p>
<figcaption>Listing 4-4: Transferring ownership of return values</figcaption>
</figure>
<p>The ownership of a variable follows the same pattern every time: assigning a
value to another variable moves it. When a variable that includes data on the
heap goes out of scope, the value will be cleaned up by <code>drop</code> unless ownership
Expand All @@ -652,7 +683,8 @@ <h3 id="return-values-and-scope"><a class="header" href="#return-values-and-scop
be passed back if we want to use it again, in addition to any data resulting
from the body of the function that we might want to return as well.</p>
<p>Rust does let us return multiple values using a tuple, as shown in Listing 4-5.</p>
<p><span class="filename">Filename: src/main.rs</span></p>
<figure class="listing">
<span class="file-name">Filename: src/main.rs</span>
<pre><pre class="playground"><code class="language-rust edition2021">fn main() {
let s1 = String::from("hello");

Expand All @@ -666,7 +698,8 @@ <h3 id="return-values-and-scope"><a class="header" href="#return-values-and-scop

(s, length)
}</code></pre></pre>
<p><span class="caption">Listing 4-5: Returning ownership of parameters</span></p>
<figcaption>Listing 4-5: Returning ownership of parameters</figcaption>
</figure>
<p>But this is too much ceremony and a lot of work for a concept that should be
common. Luckily for us, Rust has a feature for using a value without
transferring ownership, called <em>references</em>.</p>
Expand Down
32 changes: 22 additions & 10 deletions ch04-02-references-and-borrowing.html
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,8 @@ <h2 id="references-and-borrowing"><a class="header" href="#references-and-borrow
particular type for the life of that reference.</p>
<p>Here is how you would define and use a <code>calculate_length</code> function that has a
reference to an object as a parameter instead of taking ownership of the value:</p>
<p><span class="filename">Filename: src/main.rs</span></p>
<figure class="listing">
<span class="file-name">Filename: src/main.rs</span>
<pre><pre class="playground"><code class="language-rust edition2021">fn main() {
let s1 = String::from("hello");

Expand All @@ -203,15 +204,16 @@ <h2 id="references-and-borrowing"><a class="header" href="#references-and-borrow
fn calculate_length(s: &amp;String) -&gt; usize {
s.len()
}</code></pre></pre>
</figure>
<p>First, notice that all the tuple code in the variable declaration and the
function return value is gone. Second, note that we pass <code>&amp;s1</code> into
<code>calculate_length</code> and, in its definition, we take <code>&amp;String</code> rather than
<code>String</code>. These ampersands represent <em>references</em>, and they allow you to refer
to some value without taking ownership of it. Figure 4-5 depicts this concept.</p>
to some value without taking ownership of it. Figure 4-6 depicts this concept.</p>
<p><img alt="Three tables: the table for s contains only a pointer to the table
for s1. The table for s1 contains the stack data for s1 and points to the
string data on the heap." src="img/trpl04-05.svg" class="center" /></p>
<p><span class="caption">Figure 4-5: A diagram of <code>&amp;String s</code> pointing at <code>String s1</code></span></p>
string data on the heap." src="img/trpl04-06.svg" class="center" /></p>
<p><span class="caption">Figure 4-6: A diagram of <code>&amp;String s</code> pointing at <code>String s1</code></span></p>
<section class="note" aria-role="note">
<p>Note: The opposite of referencing by using <code>&amp;</code> is <em>dereferencing</em>, which is
accomplished with the dereference operator, <code>*</code>. We’ll see some uses of the
Expand Down Expand Up @@ -258,7 +260,8 @@ <h2 id="references-and-borrowing"><a class="header" href="#references-and-borrow
to give it back. You don’t own it.</p>
<p>So, what happens if we try to modify something we’re borrowing? Try the code in
Listing 4-6. Spoiler alert: it doesn’t work!</p>
<p><span class="filename">Filename: src/main.rs</span></p>
<figure class="listing">
<span class="file-name">Filename: src/main.rs</span>
<pre><code class="language-rust ignore does_not_compile">fn main() {
let s = String::from("hello");

Expand All @@ -268,7 +271,8 @@ <h2 id="references-and-borrowing"><a class="header" href="#references-and-borrow
fn change(some_string: &amp;String) {
some_string.push_str(", world");
}</code></pre>
<p><span class="caption">Listing 4-6: Attempting to modify a borrowed value</span></p>
<figcaption>Listing 4-6: Attempting to modify a borrowed value</figcaption>
</figure>
<p>Here’s the error:</p>
<pre><code class="language-console">$ cargo run
Compiling ownership v0.1.0 (file:///projects/ownership)
Expand All @@ -291,7 +295,8 @@ <h2 id="references-and-borrowing"><a class="header" href="#references-and-borrow
<h3 id="mutable-references"><a class="header" href="#mutable-references">Mutable References</a></h3>
<p>We can fix the code from Listing 4-6 to allow us to modify a borrowed value
with just a few small tweaks that use, instead, a <em>mutable reference</em>:</p>
<p><span class="filename">Filename: src/main.rs</span></p>
<figure class="listing">
<span class="file-name">Filename: src/main.rs</span>
<pre><pre class="playground"><code class="language-rust edition2021">fn main() {
let mut s = String::from("hello");

Expand All @@ -301,13 +306,15 @@ <h3 id="mutable-references"><a class="header" href="#mutable-references">Mutable
fn change(some_string: &amp;mut String) {
some_string.push_str(", world");
}</code></pre></pre>
</figure>
<p>First we change <code>s</code> to be <code>mut</code>. Then we create a mutable reference with <code>&amp;mut s</code> where we call the <code>change</code> function, and update the function signature to
accept a mutable reference with <code>some_string: &amp;mut String</code>. This makes it very
clear that the <code>change</code> function will mutate the value it borrows.</p>
<p>Mutable references have one big restriction: if you have a mutable reference to
a value, you can have no other references to that value. This code that
attempts to create two mutable references to <code>s</code> will fail:</p>
<p><span class="filename">Filename: src/main.rs</span></p>
<figure class="listing">
<span class="file-name">Filename: src/main.rs</span>
<pre><code class="language-rust ignore does_not_compile"><span class="boring">fn main() {
</span> let mut s = String::from("hello");

Expand All @@ -316,6 +323,7 @@ <h3 id="mutable-references"><a class="header" href="#mutable-references">Mutable

println!("{}, {}", r1, r2);
<span class="boring">}</span></code></pre>
</figure>
<p>Here’s the error:</p>
<pre><code class="language-console">$ cargo run
Compiling ownership v0.1.0 (file:///projects/ownership)
Expand Down Expand Up @@ -432,7 +440,8 @@ <h3 id="dangling-references"><a class="header" href="#dangling-references">Dangl
reference to the data does.</p>
<p>Let’s try to create a dangling reference to see how Rust prevents them with a
compile-time error:</p>
<p><span class="filename">Filename: src/main.rs</span></p>
<figure class="listing">
<span class="file-name">Filename: src/main.rs</span>
<pre><code class="language-rust ignore does_not_compile">fn main() {
let reference_to_nothing = dangle();
}
Expand All @@ -442,6 +451,7 @@ <h3 id="dangling-references"><a class="header" href="#dangling-references">Dangl

&amp;s
}</code></pre>
</figure>
<p>Here’s the error:</p>
<pre><code class="language-console">$ cargo run
Compiling ownership v0.1.0 (file:///projects/ownership)
Expand Down Expand Up @@ -480,7 +490,8 @@ <h3 id="dangling-references"><a class="header" href="#dangling-references">Dangl
</code></pre>
<p>Let’s take a closer look at exactly what’s happening at each stage of our
<code>dangle</code> code:</p>
<p><span class="filename">Filename: src/main.rs</span></p>
<figure class="listing">
<span class="file-name">Filename: src/main.rs</span>
<pre><code class="language-rust ignore does_not_compile"><span class="boring">fn main() {
</span><span class="boring"> let reference_to_nothing = dangle();
</span><span class="boring">}
Expand All @@ -492,6 +503,7 @@ <h3 id="dangling-references"><a class="header" href="#dangling-references">Dangl
&amp;s // we return a reference to the String, s
} // Here, s goes out of scope, and is dropped. Its memory goes away.
// Danger!</code></pre>
</figure>
<p>Because <code>s</code> is created inside <code>dangle</code>, when the code of <code>dangle</code> is finished,
<code>s</code> will be deallocated. But we tried to return a reference to it. That means
this reference would be pointing to an invalid <code>String</code>. That’s no good! Rust
Expand Down
Loading

0 comments on commit a92b897

Please sign in to comment.