Skip to content

Commit

Permalink
update original
Browse files Browse the repository at this point in the history
  • Loading branch information
funkill committed Dec 10, 2024
1 parent 9bc2bf7 commit 9208888
Show file tree
Hide file tree
Showing 14 changed files with 54 additions and 56 deletions.
2 changes: 1 addition & 1 deletion rustbook-en/.git-blame-ignore-revs
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# Ran dprint fmt on the repo
3a30e4c1
3a30e4c1fbe641afc066b3af9eb01dcdf5ed8b24
2 changes: 1 addition & 1 deletion rustbook-en/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pub fn add(left: usize, right: usize) -> usize {
pub fn add(left: u64, right: u64) -> u64 {
left + right
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pub fn add(left: usize, right: usize) -> usize {
pub fn add(left: u64, right: u64) -> u64 {
left + right
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pub fn add(left: usize, right: usize) -> usize {
pub fn add(left: u64, right: u64) -> u64 {
left + right
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pub fn add(left: usize, right: usize) -> usize {
pub fn add(left: u64, right: u64) -> u64 {
left + right
}

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
fn returns_closure() -> dyn Fn(i32) -> i32 {
fn returns_closure() -> impl Fn(i32) -> i32 {
|x| x + 1
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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 changes: 3 additions & 5 deletions rustbook-en/src/ch01-02-hello-world.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
9 changes: 6 additions & 3 deletions rustbook-en/src/ch11-01-writing-tests.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,19 @@ 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
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.
Expand Down
11 changes: 8 additions & 3 deletions rustbook-en/src/ch20-01-unsafe-rust.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
30 changes: 15 additions & 15 deletions rustbook-en/src/ch20-05-advanced-functions-and-closures.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 9208888

Please sign in to comment.