Browse Source

update original

pull/1403/head
funkill2 1 year ago
parent
commit
9208888776
  1. 2
      rustbook-en/.git-blame-ignore-revs
  2. 2
      rustbook-en/README.md
  3. 2
      rustbook-en/listings/ch11-writing-automated-tests/listing-11-03/src/lib.rs
  4. 2
      rustbook-en/listings/ch11-writing-automated-tests/no-listing-01-changing-test-name/src/lib.rs
  5. 2
      rustbook-en/listings/ch11-writing-automated-tests/no-listing-10-result-in-tests/src/lib.rs
  6. 2
      rustbook-en/listings/ch11-writing-automated-tests/no-listing-11-ignore-a-test/src/lib.rs
  7. 20
      rustbook-en/listings/ch20-advanced-features/no-listing-18-returns-closure/output.txt
  8. 2
      rustbook-en/listings/ch20-advanced-features/no-listing-18-returns-closure/src/lib.rs
  9. 3
      rustbook-en/listings/ch20-advanced-features/no-listing-19-returns-closure-trait-object/src/lib.rs
  10. 15
      rustbook-en/listings/ch20-advanced-features/no-listing-19-returns-closure-trait-object/src/main.rs
  11. 8
      rustbook-en/src/ch01-02-hello-world.md
  12. 9
      rustbook-en/src/ch11-01-writing-tests.md
  13. 11
      rustbook-en/src/ch20-01-unsafe-rust.md
  14. 30
      rustbook-en/src/ch20-05-advanced-functions-and-closures.md

2
rustbook-en/.git-blame-ignore-revs

@ -1,2 +1,2 @@ @@ -1,2 +1,2 @@
# Ran dprint fmt on the repo
3a30e4c1
3a30e4c1fbe641afc066b3af9eb01dcdf5ed8b24

2
rustbook-en/README.md

@ -39,7 +39,7 @@ look right, but you _will_ still be able to build the book. To use the plugins, @@ -39,7 +39,7 @@ look right, but you _will_ still be able to build the book. To use the plugins,
you should run:
```bash
$ cargo install --locked --path packages/mdbook_trpl
$ cargo install --locked --path packages/mdbook-trpl
```
## Building

2
rustbook-en/listings/ch11-writing-automated-tests/listing-11-03/src/lib.rs

@ -1,4 +1,4 @@ @@ -1,4 +1,4 @@
pub fn add(left: usize, right: usize) -> usize {
pub fn add(left: u64, right: u64) -> u64 {
left + right
}

2
rustbook-en/listings/ch11-writing-automated-tests/no-listing-01-changing-test-name/src/lib.rs

@ -1,4 +1,4 @@ @@ -1,4 +1,4 @@
pub fn add(left: usize, right: usize) -> usize {
pub fn add(left: u64, right: u64) -> u64 {
left + right
}

2
rustbook-en/listings/ch11-writing-automated-tests/no-listing-10-result-in-tests/src/lib.rs

@ -1,4 +1,4 @@ @@ -1,4 +1,4 @@
pub fn add(left: usize, right: usize) -> usize {
pub fn add(left: u64, right: u64) -> u64 {
left + right
}

2
rustbook-en/listings/ch11-writing-automated-tests/no-listing-11-ignore-a-test/src/lib.rs

@ -1,4 +1,4 @@ @@ -1,4 +1,4 @@
pub fn add(left: usize, right: usize) -> usize {
pub fn add(left: u64, right: u64) -> u64 {
left + right
}

20
rustbook-en/listings/ch20-advanced-features/no-listing-18-returns-closure/output.txt

@ -1,20 +0,0 @@ @@ -1,20 +0,0 @@
$ cargo build
Compiling functions-example v0.1.0 (file:///projects/functions-example)
error[E0746]: return type cannot have an unboxed trait object
--> src/lib.rs:1:25
|
1 | fn returns_closure() -> dyn Fn(i32) -> i32 {
| ^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
|
help: consider returning an `impl Trait` instead of a `dyn Trait`
|
1 | fn returns_closure() -> impl Fn(i32) -> i32 {
| ~~~~
help: alternatively, box the return type, and wrap all of the returned values in `Box::new`
|
1 ~ fn returns_closure() -> Box<dyn Fn(i32) -> i32> {
2 ~ Box::new(|x| x + 1)
|
For more information about this error, try `rustc --explain E0746`.
error: could not compile `functions-example` (lib) due to 1 previous error

2
rustbook-en/listings/ch20-advanced-features/no-listing-18-returns-closure/src/lib.rs

@ -1,3 +1,3 @@ @@ -1,3 +1,3 @@
fn returns_closure() -> dyn Fn(i32) -> i32 {
fn returns_closure() -> impl Fn(i32) -> i32 {
|x| x + 1
}

3
rustbook-en/listings/ch20-advanced-features/no-listing-19-returns-closure-trait-object/src/lib.rs

@ -1,3 +0,0 @@ @@ -1,3 +0,0 @@
fn returns_closure() -> Box<dyn Fn(i32) -> i32> {
Box::new(|x| x + 1)
}

15
rustbook-en/listings/ch20-advanced-features/no-listing-19-returns-closure-trait-object/src/main.rs

@ -0,0 +1,15 @@ @@ -0,0 +1,15 @@
fn main() {
let handlers = vec![returns_closure(), returns_initialized_closure(123)];
for handler in handlers {
let output = handler(5);
println!("{output}");
}
}
fn returns_closure() -> Box<dyn Fn(i32) -> i32> {
Box::new(|x| x + 1)
}
fn returns_initialized_closure(init: i32) -> Box<dyn Fn(i32) -> i32> {
Box::new(move |x| x + init)
}

8
rustbook-en/src/ch01-02-hello-world.md

@ -121,18 +121,16 @@ println!("Hello, world!"); @@ -121,18 +121,16 @@ println!("Hello, world!");
This line does all the work in this little program: it prints text to the
screen. There are four important details to notice here.
First, Rust style is to indent with four spaces, not a tab.
Second, `println!` calls a Rust macro. If it had called a function instead, it
First, `println!` calls a Rust macro. If it had called a function instead, it
would be entered as `println` (without the `!`). We’ll discuss Rust macros in
more detail in Chapter 20. For now, you just need to know that using a `!`
means that you’re calling a macro instead of a normal function and that macros
don’t always follow the same rules as functions.
Third, you see the `"Hello, world!"` string. We pass this string as an argument
Second, you see the `"Hello, world!"` string. We pass this string as an argument
to `println!`, and the string is printed to the screen.
Fourth, we end the line with a semicolon (`;`), which indicates that this
Third, we end the line with a semicolon (`;`), which indicates that this
expression is over and the next one is ready to begin. Most lines of Rust code
end with a semicolon.

9
rustbook-en/src/ch11-01-writing-tests.md

@ -62,6 +62,9 @@ cd ../../.. @@ -62,6 +62,9 @@ cd ../../..
</Listing>
The file starts with an example `add` function, so that we have something
to test.
For now, let’s focus solely on the `it_works` function. Note the `#[test]`
annotation: this attribute indicates this is a test function, so the test
runner knows to treat this function as a test. We might also have non-test
@ -69,9 +72,9 @@ functions in the `tests` module to help set up common scenarios or perform @@ -69,9 +72,9 @@ functions in the `tests` module to help set up common scenarios or perform
common operations, so we always need to indicate which functions are tests.
The example function body uses the `assert_eq!` macro to assert that `result`,
which contains the result of adding 2 and 2, equals 4. This assertion serves as
an example of the format for a typical test. Let’s run it to see that this test
passes.
which contains the result of calling `add` with 2 and 2, equals 4. This
assertion serves as an example of the format for a typical test. Let’s run it
to see that this test passes.
The `cargo test` command runs all tests in our project, as shown in Listing
11-2.

11
rustbook-en/src/ch20-01-unsafe-rust.md

@ -186,9 +186,14 @@ With the `unsafe` block, we’re asserting to Rust that we’ve read the functio @@ -186,9 +186,14 @@ With the `unsafe` block, we’re asserting to Rust that we’ve read the functio
documentation, we understand how to use it properly, and we’ve verified that
we’re fulfilling the contract of the function.
Bodies of unsafe functions are effectively `unsafe` blocks, so to perform other
unsafe operations within an unsafe function, we don’t need to add another
`unsafe` block.
> Note: In earlier versions of Rust, the body of an unsafe function was treated
> as an `unsafe` block, so you could perform any unsafe operation within the
> body of an `unsafe` function. In later versions of Rust, the compiler will
> warn you that you need to use an `unsafe` block to perform unsafe operations
> in the body of an unsafe function. This is because Rust now distinguishes
> between `unsafe fn`, which defines what you need to do to call the function
> safely, and an `unsafe` block, where you actually uphold that “contract” the
> function establishes.
#### Creating a Safe Abstraction over Unsafe Code

30
rustbook-en/src/ch20-05-advanced-functions-and-closures.md

@ -95,33 +95,33 @@ function. However, you can’t do that with closures because they don’t have a @@ -95,33 +95,33 @@ function. However, you can’t do that with closures because they don’t have a
concrete type that is returnable; you’re not allowed to use the function
pointer `fn` as a return type, for example.
The following code tries to return a closure directly, but it won’t compile:
Instead, you will normally use the `impl Trait` syntax we learned about in
Chapter 10. You can return any function type, using `Fn`, `FnOnce` and `FnMut`.
For example, this code will work just fine:
```rust,ignore,does_not_compile
{{#rustdoc_include ../listings/ch20-advanced-features/no-listing-18-returns-closure/src/lib.rs}}
```
The compiler error is as follows:
```console
{{#include ../listings/ch20-advanced-features/no-listing-18-returns-closure/output.txt}}
```
The error references the `Sized` trait again! Rust doesn’t know how much space
it will need to store the closure. We saw a solution to this problem earlier.
We can use a trait object:
However, as we noted in the [“Closure Type Inference and
Annotation”][closure-types]<!-- ignore --> section in Chapter 13, each closure
is also its own distinct type. If you need to work with multiple functions that
have the same signature but different implementations, you will need to use a
trait object for them:
```rust,noplayground
{{#rustdoc_include ../listings/ch20-advanced-features/no-listing-19-returns-closure-trait-object/src/lib.rs}}
{{#rustdoc_include ../listings/ch20-advanced-features/no-listing-19-returns-closure-trait-object/src/main.rs}}
```
This code will compile just fine. For more about trait objects, refer to the
section [“Using Trait Objects That Allow for Values of Different
Types”][using-trait-objects-that-allow-for-values-of-different-types]<!--
ignore --> in Chapter 19.
This code will compile just fine—but it wouldn’t if we had tried to stick with
`impl Fn(i32) -> i32`. For more about trait objects, refer to the section
[“Using Trait Objects That Allow for Values of Different
Types”][using-trait-objects-that-allow-for-values-of-different-types]<!-- ignore
--> in Chapter 19.
Next, let’s look at macros!
[advanced-traits]: ch20-03-advanced-traits.html#advanced-traits
[enum-values]: ch06-01-defining-an-enum.html#enum-values
[closure-types]: ch13-01-closures.html#closure-type-inference-and-annotation
[using-trait-objects-that-allow-for-values-of-different-types]: ch18-02-trait-objects.html#using-trait-objects-that-allow-for-values-of-different-types

Loading…
Cancel
Save