From 9208888776227ace87cbde49e225fc711840e047 Mon Sep 17 00:00:00 2001 From: funkill2 Date: Tue, 10 Dec 2024 04:00:17 +0300 Subject: [PATCH] update original --- rustbook-en/.git-blame-ignore-revs | 2 +- rustbook-en/README.md | 2 +- .../listing-11-03/src/lib.rs | 2 +- .../src/lib.rs | 2 +- .../no-listing-10-result-in-tests/src/lib.rs | 2 +- .../no-listing-11-ignore-a-test/src/lib.rs | 2 +- .../no-listing-18-returns-closure/output.txt | 20 ------------- .../no-listing-18-returns-closure/src/lib.rs | 2 +- .../src/lib.rs | 3 -- .../src/main.rs | 15 ++++++++++ rustbook-en/src/ch01-02-hello-world.md | 8 ++--- rustbook-en/src/ch11-01-writing-tests.md | 9 ++++-- rustbook-en/src/ch20-01-unsafe-rust.md | 11 +++++-- ...ch20-05-advanced-functions-and-closures.md | 30 +++++++++---------- 14 files changed, 54 insertions(+), 56 deletions(-) delete mode 100644 rustbook-en/listings/ch20-advanced-features/no-listing-18-returns-closure/output.txt delete mode 100644 rustbook-en/listings/ch20-advanced-features/no-listing-19-returns-closure-trait-object/src/lib.rs create mode 100644 rustbook-en/listings/ch20-advanced-features/no-listing-19-returns-closure-trait-object/src/main.rs diff --git a/rustbook-en/.git-blame-ignore-revs b/rustbook-en/.git-blame-ignore-revs index 2bcb16231..3c71ce418 100644 --- a/rustbook-en/.git-blame-ignore-revs +++ b/rustbook-en/.git-blame-ignore-revs @@ -1,2 +1,2 @@ # Ran dprint fmt on the repo -3a30e4c1 +3a30e4c1fbe641afc066b3af9eb01dcdf5ed8b24 diff --git a/rustbook-en/README.md b/rustbook-en/README.md index a63240238..2ff16818b 100644 --- a/rustbook-en/README.md +++ b/rustbook-en/README.md @@ -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 diff --git a/rustbook-en/listings/ch11-writing-automated-tests/listing-11-03/src/lib.rs b/rustbook-en/listings/ch11-writing-automated-tests/listing-11-03/src/lib.rs index 866f0238b..336ea081a 100644 --- a/rustbook-en/listings/ch11-writing-automated-tests/listing-11-03/src/lib.rs +++ b/rustbook-en/listings/ch11-writing-automated-tests/listing-11-03/src/lib.rs @@ -1,4 +1,4 @@ -pub fn add(left: usize, right: usize) -> usize { +pub fn add(left: u64, right: u64) -> u64 { left + right } diff --git a/rustbook-en/listings/ch11-writing-automated-tests/no-listing-01-changing-test-name/src/lib.rs b/rustbook-en/listings/ch11-writing-automated-tests/no-listing-01-changing-test-name/src/lib.rs index 5be58b93f..5014a76f2 100644 --- a/rustbook-en/listings/ch11-writing-automated-tests/no-listing-01-changing-test-name/src/lib.rs +++ b/rustbook-en/listings/ch11-writing-automated-tests/no-listing-01-changing-test-name/src/lib.rs @@ -1,4 +1,4 @@ -pub fn add(left: usize, right: usize) -> usize { +pub fn add(left: u64, right: u64) -> u64 { left + right } diff --git a/rustbook-en/listings/ch11-writing-automated-tests/no-listing-10-result-in-tests/src/lib.rs b/rustbook-en/listings/ch11-writing-automated-tests/no-listing-10-result-in-tests/src/lib.rs index c31cdcae4..06b1a03e1 100644 --- a/rustbook-en/listings/ch11-writing-automated-tests/no-listing-10-result-in-tests/src/lib.rs +++ b/rustbook-en/listings/ch11-writing-automated-tests/no-listing-10-result-in-tests/src/lib.rs @@ -1,4 +1,4 @@ -pub fn add(left: usize, right: usize) -> usize { +pub fn add(left: u64, right: u64) -> u64 { left + right } diff --git a/rustbook-en/listings/ch11-writing-automated-tests/no-listing-11-ignore-a-test/src/lib.rs b/rustbook-en/listings/ch11-writing-automated-tests/no-listing-11-ignore-a-test/src/lib.rs index 0c8b38def..05fbe1af0 100644 --- a/rustbook-en/listings/ch11-writing-automated-tests/no-listing-11-ignore-a-test/src/lib.rs +++ b/rustbook-en/listings/ch11-writing-automated-tests/no-listing-11-ignore-a-test/src/lib.rs @@ -1,4 +1,4 @@ -pub fn add(left: usize, right: usize) -> usize { +pub fn add(left: u64, right: u64) -> u64 { left + right } diff --git a/rustbook-en/listings/ch20-advanced-features/no-listing-18-returns-closure/output.txt b/rustbook-en/listings/ch20-advanced-features/no-listing-18-returns-closure/output.txt deleted file mode 100644 index 3a2342639..000000000 --- a/rustbook-en/listings/ch20-advanced-features/no-listing-18-returns-closure/output.txt +++ /dev/null @@ -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 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 diff --git a/rustbook-en/listings/ch20-advanced-features/no-listing-18-returns-closure/src/lib.rs b/rustbook-en/listings/ch20-advanced-features/no-listing-18-returns-closure/src/lib.rs index d699ac34e..7679cc7c0 100644 --- a/rustbook-en/listings/ch20-advanced-features/no-listing-18-returns-closure/src/lib.rs +++ b/rustbook-en/listings/ch20-advanced-features/no-listing-18-returns-closure/src/lib.rs @@ -1,3 +1,3 @@ -fn returns_closure() -> dyn Fn(i32) -> i32 { +fn returns_closure() -> impl Fn(i32) -> i32 { |x| x + 1 } diff --git a/rustbook-en/listings/ch20-advanced-features/no-listing-19-returns-closure-trait-object/src/lib.rs b/rustbook-en/listings/ch20-advanced-features/no-listing-19-returns-closure-trait-object/src/lib.rs deleted file mode 100644 index b11407747..000000000 --- a/rustbook-en/listings/ch20-advanced-features/no-listing-19-returns-closure-trait-object/src/lib.rs +++ /dev/null @@ -1,3 +0,0 @@ -fn returns_closure() -> Box i32> { - Box::new(|x| x + 1) -} diff --git a/rustbook-en/listings/ch20-advanced-features/no-listing-19-returns-closure-trait-object/src/main.rs b/rustbook-en/listings/ch20-advanced-features/no-listing-19-returns-closure-trait-object/src/main.rs new file mode 100644 index 000000000..f033ea716 --- /dev/null +++ b/rustbook-en/listings/ch20-advanced-features/no-listing-19-returns-closure-trait-object/src/main.rs @@ -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 i32> { + Box::new(|x| x + 1) +} + +fn returns_initialized_closure(init: i32) -> Box i32> { + Box::new(move |x| x + init) +} diff --git a/rustbook-en/src/ch01-02-hello-world.md b/rustbook-en/src/ch01-02-hello-world.md index 600401d4d..5779360b1 100644 --- a/rustbook-en/src/ch01-02-hello-world.md +++ b/rustbook-en/src/ch01-02-hello-world.md @@ -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. diff --git a/rustbook-en/src/ch11-01-writing-tests.md b/rustbook-en/src/ch11-01-writing-tests.md index 93009f4d0..b04f0c2fe 100644 --- a/rustbook-en/src/ch11-01-writing-tests.md +++ b/rustbook-en/src/ch11-01-writing-tests.md @@ -62,6 +62,9 @@ cd ../../.. +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 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. diff --git a/rustbook-en/src/ch20-01-unsafe-rust.md b/rustbook-en/src/ch20-01-unsafe-rust.md index 3e6147fe9..8796483a4 100644 --- a/rustbook-en/src/ch20-01-unsafe-rust.md +++ b/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 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 diff --git a/rustbook-en/src/ch20-05-advanced-functions-and-closures.md b/rustbook-en/src/ch20-05-advanced-functions-and-closures.md index 9f04892b0..323e57bc9 100644 --- a/rustbook-en/src/ch20-05-advanced-functions-and-closures.md +++ b/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 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] 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] 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] 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