Browse Source

update original

pull/1401/merge
funkill2 9 months ago
parent
commit
08e64a0f12
  1. 1
      rustbook-en/listings/ch02-guessing-game-tutorial/listing-02-03/src/main.rs
  2. 3
      rustbook-en/listings/ch02-guessing-game-tutorial/listing-02-04/src/main.rs
  3. 3
      rustbook-en/listings/ch02-guessing-game-tutorial/listing-02-05/src/main.rs
  4. 3
      rustbook-en/listings/ch02-guessing-game-tutorial/listing-02-06/src/main.rs
  5. 3
      rustbook-en/listings/ch02-guessing-game-tutorial/no-listing-03-convert-string-to-number/src/main.rs
  6. 3
      rustbook-en/listings/ch02-guessing-game-tutorial/no-listing-04-looping/src/main.rs
  7. 3
      rustbook-en/listings/ch02-guessing-game-tutorial/no-listing-05-quitting/src/main.rs
  8. 27
      rustbook-en/packages/mdbook-trpl/src/listing/mod.rs
  9. 28
      rustbook-en/packages/mdbook-trpl/src/listing/tests.rs
  10. 20
      rustbook-en/src/ch01-01-installation.md
  11. 12
      rustbook-en/src/ch07-04-bringing-paths-into-scope-with-the-use-keyword.md
  12. 5
      rustbook-en/src/ch16-02-message-passing.md
  13. 13
      rustbook-en/src/ch21-01-single-threaded.md

1
rustbook-en/listings/ch02-guessing-game-tutorial/listing-02-03/src/main.rs

@ -1,5 +1,6 @@ @@ -1,5 +1,6 @@
// ANCHOR: all
use std::io;
// ANCHOR: ch07-04
use rand::Rng;

3
rustbook-en/listings/ch02-guessing-game-tutorial/listing-02-04/src/main.rs

@ -1,8 +1,9 @@ @@ -1,8 +1,9 @@
// ANCHOR: here
use rand::Rng;
use std::cmp::Ordering;
use std::io;
use rand::Rng;
fn main() {
// --snip--
// ANCHOR_END: here

3
rustbook-en/listings/ch02-guessing-game-tutorial/listing-02-05/src/main.rs

@ -1,7 +1,8 @@ @@ -1,7 +1,8 @@
use rand::Rng;
use std::cmp::Ordering;
use std::io;
use rand::Rng;
fn main() {
println!("Guess the number!");

3
rustbook-en/listings/ch02-guessing-game-tutorial/listing-02-06/src/main.rs

@ -1,7 +1,8 @@ @@ -1,7 +1,8 @@
use rand::Rng;
use std::cmp::Ordering;
use std::io;
use rand::Rng;
fn main() {
println!("Guess the number!");

3
rustbook-en/listings/ch02-guessing-game-tutorial/no-listing-03-convert-string-to-number/src/main.rs

@ -1,7 +1,8 @@ @@ -1,7 +1,8 @@
use rand::Rng;
use std::cmp::Ordering;
use std::io;
use rand::Rng;
fn main() {
println!("Guess the number!");

3
rustbook-en/listings/ch02-guessing-game-tutorial/no-listing-04-looping/src/main.rs

@ -1,7 +1,8 @@ @@ -1,7 +1,8 @@
use rand::Rng;
use std::cmp::Ordering;
use std::io;
use rand::Rng;
fn main() {
println!("Guess the number!");

3
rustbook-en/listings/ch02-guessing-game-tutorial/no-listing-05-quitting/src/main.rs

@ -1,7 +1,8 @@ @@ -1,7 +1,8 @@
use rand::Rng;
use std::cmp::Ordering;
use std::io;
use rand::Rng;
fn main() {
println!("Guess the number!");

27
rustbook-en/packages/mdbook-trpl/src/listing/mod.rs

@ -221,7 +221,13 @@ struct Listing { @@ -221,7 +221,13 @@ struct Listing {
impl Listing {
fn opening_html(&self) -> String {
let figure = String::from("<figure class=\"listing\">\n");
let id_attribute = self
.number
.as_ref()
.map(|number| format!(" id=\"listing-{number}\""))
.unwrap_or_default();
let figure = format!("<figure class=\"listing\"{id_attribute}>\n");
match self.file_name.as_ref() {
Some(file_name) => format!(
@ -233,16 +239,21 @@ impl Listing { @@ -233,16 +239,21 @@ impl Listing {
fn closing_html(&self, trailing: &str) -> String {
match (&self.number, &self.caption) {
(Some(number), Some(caption)) => format!(
r#"<figcaption>Listing {number}: {caption}</figcaption>
(Some(number), caption) => {
let caption_text = caption
.as_ref()
.map(|caption| format!(": {}", caption))
.unwrap_or_default();
let listing_a_tag = format!(
"<a href=\"#listing-{number}\">Listing {number}</a>"
);
format!(
r#"<figcaption>{listing_a_tag}{caption_text}</figcaption>
</figure>{trailing}"#
),
)
}
(None, Some(caption)) => format!(
r#"<figcaption>{caption}</figcaption>
</figure>{trailing}"#
),
(Some(number), None) => format!(
r#"<figcaption>Listing {number}</figcaption>
</figure>{trailing}"#
),
(None, None) => format!("</figure>{trailing}"),

28
rustbook-en/packages/mdbook-trpl/src/listing/tests.rs

@ -18,15 +18,15 @@ fn main() {} @@ -18,15 +18,15 @@ fn main() {}
assert_eq!(
&result.unwrap(),
r#"<figure class="listing">
r##"<figure class="listing" id="listing-1-2">
<span class="file-name">Filename: src/main.rs</span>
````rust
fn main() {}
````
<figcaption>Listing 1-2: A write-up which <em>might</em> include inline Markdown like <code>code</code> etc.</figcaption>
</figure>"#
<figcaption><a href="#listing-1-2">Listing 1-2</a>: A write-up which <em>might</em> include inline Markdown like <code>code</code> etc.</figcaption>
</figure>"##
);
}
@ -80,7 +80,7 @@ fn get_a_box_of<T>(t: T) -> Box<T> { @@ -80,7 +80,7 @@ fn get_a_box_of<T>(t: T) -> Box<T> {
assert_eq!(
&result.unwrap(),
r#"<figure class="listing">
r##"<figure class="listing" id="listing-34-5">
````rust
fn get_a_box_of<T>(t: T) -> Box<T> {
@ -88,8 +88,8 @@ fn get_a_box_of<T>(t: T) -> Box<T> { @@ -88,8 +88,8 @@ fn get_a_box_of<T>(t: T) -> Box<T> {
}
````
<figcaption>Listing 34-5: This has a <code>Box&lt;T&gt;</code> in it.</figcaption>
</figure>"#
<figcaption><a href="#listing-34-5">Listing 34-5</a>: This has a <code>Box&lt;T&gt;</code> in it.</figcaption>
</figure>"##
);
}
@ -115,9 +115,9 @@ Save the file and go back to your terminal window"#, @@ -115,9 +115,9 @@ Save the file and go back to your terminal window"#,
assert!(result.is_ok());
assert_eq!(
result.unwrap(),
r#"Now open the *main.rs* file you just created and enter the code in Listing 1-1.
r##"Now open the *main.rs* file you just created and enter the code in Listing 1-1.
<figure class="listing">
<figure class="listing" id="listing-1-1">
<span class="file-name">Filename: main.rs</span>
````rust
@ -126,10 +126,10 @@ fn main() { @@ -126,10 +126,10 @@ fn main() {
}
````
<figcaption>Listing 1-1: A program that prints <code>Hello, world!</code></figcaption>
<figcaption><a href="#listing-1-1">Listing 1-1</a>: A program that prints <code>Hello, world!</code></figcaption>
</figure>
Save the file and go back to your terminal window"#
Save the file and go back to your terminal window"##
);
}
@ -153,18 +153,18 @@ This is the closing."#, @@ -153,18 +153,18 @@ This is the closing."#,
assert!(result.is_ok());
assert_eq!(
result.unwrap(),
r#"This is the opening.
r##"This is the opening.
<figure class="listing">
<figure class="listing" id="listing-1-1">
````rust
fn main() {}
````
<figcaption>Listing 1-1: This is the caption</figcaption>
<figcaption><a href="#listing-1-1">Listing 1-1</a>: This is the caption</figcaption>
</figure>
This is the closing."#
This is the closing."##
);
}

20
rustbook-en/src/ch01-01-installation.md

@ -142,6 +142,26 @@ integrated development environments (IDEs) have built-in support for Rust. You @@ -142,6 +142,26 @@ integrated development environments (IDEs) have built-in support for Rust. You
can always find a fairly current list of many editors and IDEs on [the tools
page][tools] on the Rust website.
### Working Offline with This Book
In several examples, we will use Rust packages beyond the standard library. To
work through those examples, you will either need to have an internet connection
or to have downloaded those dependencies ahead of time. To download the
dependencies ahead of time, you can run the following commands. (We’ll explain
what `cargo` is and what each of these commands does in detail later.)
```console
$ cargo new get-dependencies
$ cd get-dependencies
$ cargo add rand@0.8.5 trpl@0.2.0
```
This will cache the downloads for these packages so you will not need to
download them later. Once you have run this command, you do not need to keep the
`get-dependencies` folder. If you have run this command, you can use the
`--offline` flag with all `cargo` commands in the rest of the book to use these
cached versions instead of attempting to use the network.
[otherinstall]: https://forge.rust-lang.org/infra/other-installation-methods.html
[install]: https://www.rust-lang.org/tools/install
[msvc]: https://rust-lang.github.io/rustup/installation/windows-msvc.html

12
rustbook-en/src/ch07-04-bringing-paths-into-scope-with-the-use-keyword.md

@ -130,12 +130,12 @@ considered idiomatic, so the choice is up to you! @@ -130,12 +130,12 @@ considered idiomatic, so the choice is up to you!
### Re-exporting Names with `pub use`
When we bring a name into scope with the `use` keyword, the name available in
the new scope is private. To enable the code that calls our code to refer to
that name as if it had been defined in that code’s scope, we can combine `pub`
and `use`. This technique is called _re-exporting_ because we’re bringing an
item into scope but also making that item available for others to bring into
their scope.
When we bring a name into scope with the `use` keyword, the name is private to
the scope into which we imported it. To enable code outside that scope to refer
to that name as if it had been defined in that scope, we can combine `pub` and
`use`. This technique is called _re-exporting_ because we’re bringing an item
into scope but also making that item available for others to bring into their
scope.
Listing 7-17 shows the code in Listing 7-11 with `use` in the root module
changed to `pub use`.

5
rustbook-en/src/ch16-02-message-passing.md

@ -32,14 +32,13 @@ First, in Listing 16-6, we’ll create a channel but not do anything with it. @@ -32,14 +32,13 @@ First, in Listing 16-6, we’ll create a channel but not do anything with it.
Note that this won’t compile yet because Rust can’t tell what type of values we
want to send over the channel.
<span class="filename">Filename: src/main.rs</span>
<Listing number="16-6" file-name="src/main.rs" caption="Creating a channel and assigning the two halves to `tx` and `rx`">
```rust,ignore,does_not_compile
{{#rustdoc_include ../listings/ch16-fearless-concurrency/listing-16-06/src/main.rs}}
```
<span class="caption">Listing 16-6: Creating a channel and assigning the two
halves to `tx` and `rx`</span>
</Listing>
We create a new channel using the `mpsc::channel` function; `mpsc` stands for
_multiple producer, single consumer_. In short, the way Rust’s standard library

13
rustbook-en/src/ch21-01-single-threaded.md

@ -109,8 +109,17 @@ It could also be that the browser is trying to connect to the server multiple @@ -109,8 +109,17 @@ It could also be that the browser is trying to connect to the server multiple
times because the server isn’t responding with any data. When `stream` goes out
of scope and is dropped at the end of the loop, the connection is closed as
part of the `drop` implementation. Browsers sometimes deal with closed
connections by retrying, because the problem might be temporary. The important
factor is that we’ve successfully gotten a handle to a TCP connection!
connections by retrying, because the problem might be temporary.
Browsers also sometimes open multiple connections to the server without sending
any requests, so that if they *do* later send requests, they can happen faster.
When this happens, our server will see each connection, regardless of whether
there are any requests over that connection. Many versions of Chrome-based
browsers do this, for example; you can disable that optimization by using =
private browsing mode or use a different browser.
The important factor is that we’ve successfully gotten a handle to a TCP
connection!
Remember to stop the program by pressing <kbd>ctrl</kbd>-<kbd>c</kbd> when
you’re done running a particular version of the code. Then restart the program

Loading…
Cancel
Save