Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/rust-lang-uz/book
Browse files Browse the repository at this point in the history
  • Loading branch information
0xf90c committed Jun 16, 2024
2 parents 981c9ed + 33c85c4 commit 8484f17
Show file tree
Hide file tree
Showing 20 changed files with 164 additions and 228 deletions.
2 changes: 1 addition & 1 deletion rustbook-en/ADMIN_TASKS.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ extracted into a file. To do that:
- If you only want to show part of the code in the file, use anchor comments
(`// ANCHOR: some_tag` and `// ANCHOR_END: some_tag`) to mark the parts of
the file you want to show.
- For Rust code, use the `{{#rustdoc_include [fileame:some_tag]}}` directive
- For Rust code, use the `{{#rustdoc_include [filename:some_tag]}}` directive
within the code blocks in the text. The `rustdoc_include` directive gives the
code that doesn't get displayed to `rustdoc` for `mdbook test` purposes.
- For anything else, use the `{{#include [filename:some_tag]}}` directive.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@ test tests::it_adds_two ... FAILED
failures:

---- tests::it_adds_two stdout ----
thread 'tests::it_adds_two' panicked at src/lib.rs:11:9:
assertion `left == right` failed
left: 4
right: 5
left: 5
right: 4
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ mod tests {

#[test]
fn it_adds_two() {
assert_eq!(4, add_two(2));
assert_eq!(add_two(2), 4);
}
}
29 changes: 13 additions & 16 deletions rustbook-en/src/ch02-00-guessing-game-tutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,13 @@ that input, and check that the input is in the expected form. To start, we’ll
allow the player to input a guess. Enter the code in Listing 2-1 into
*src/main.rs*.

<span class="filename">Filename: src/main.rs</span>
<Listing number="2-1" file-name="src/main.rs" caption="Code that gets a guess from the user and prints it">

```rust,ignore
{{#rustdoc_include ../listings/ch02-guessing-game-tutorial/listing-02-01/src/main.rs:all}}
```

<span class="caption">Listing 2-1: Code that gets a guess from the user and
prints it</span>
</Listing>

This code contains a lot of information, so let’s go over it line by line. To
obtain user input and then print the result as output, we need to bring the
Expand Down Expand Up @@ -376,6 +375,8 @@ rm Cargo.lock
cargo clean
cargo build -->

<Listing number="2-2" caption="The output from running `cargo build` after adding the rand crate as a dependency">

```console
$ cargo build
Updating crates.io index
Expand All @@ -397,8 +398,7 @@ $ cargo build
Finished dev [unoptimized + debuginfo] target(s) in 2.53s
```

<span class="caption">Listing 2-2: The output from running `cargo build` after
adding the rand crate as a dependency</span>
</Listing>

You may see different version numbers (but they will all be compatible with the
code, thanks to SemVer!) and different lines (depending on the operating
Expand Down Expand Up @@ -508,14 +508,13 @@ from a number of packages.
Let’s start using `rand` to generate a number to guess. The next step is to
update *src/main.rs*, as shown in Listing 2-3.

<span class="filename">Filename: src/main.rs</span>
<Listing number="2-3" file-name="src/main.rs" caption="Adding code to generate a random number">

```rust,ignore
{{#rustdoc_include ../listings/ch02-guessing-game-tutorial/listing-02-03/src/main.rs:all}}
```

<span class="caption">Listing 2-3: Adding code to generate a random
number</span>
</Listing>

First we add the line `use rand::Rng;`. The `Rng` trait defines methods that
random number generators implement, and this trait must be in scope for us to
Expand Down Expand Up @@ -585,14 +584,13 @@ Now that we have user input and a random number, we can compare them. That step
is shown in Listing 2-4. Note that this code won’t compile just yet, as we will
explain.

<span class="filename">Filename: src/main.rs</span>
<Listing number="2-4" file-name="src/main.rs" caption="Handling the possible return values of comparing two numbers">

```rust,ignore,does_not_compile
{{#rustdoc_include ../listings/ch02-guessing-game-tutorial/listing-02-04/src/main.rs:here}}
```

<span class="caption">Listing 2-4: Handling the possible return values of
comparing two numbers</span>
</Listing>

First we add another `use` statement, bringing a type called
`std::cmp::Ordering` into scope from the standard library. The `Ordering` type
Expand Down Expand Up @@ -826,14 +824,13 @@ the user inputs a non-number, let’s make the game ignore a non-number so the
user can continue guessing. We can do that by altering the line where `guess`
is converted from a `String` to a `u32`, as shown in Listing 2-5.

<span class="filename">Filename: src/main.rs</span>
<Listing number="2-5" file-name="src/main.rs" caption="Ignoring a non-number guess and asking for another guess instead of crashing the program">

```rust,ignore
{{#rustdoc_include ../listings/ch02-guessing-game-tutorial/listing-02-05/src/main.rs:here}}
```

<span class="caption">Listing 2-5: Ignoring a non-number guess and asking for
another guess instead of crashing the program</span>
</Listing>

We switch from an `expect` call to a `match` expression to move from crashing
on an error to handling the error. Remember that `parse` returns a `Result`
Expand Down Expand Up @@ -896,13 +893,13 @@ that the program is still printing the secret number. That worked well for
testing, but it ruins the game. Let’s delete the `println!` that outputs the
secret number. Listing 2-6 shows the final code.

<span class="filename">Filename: src/main.rs</span>
<Listing number="2-6" file-name="src/main.rs" caption="Complete guessing game code">

```rust,ignore
{{#rustdoc_include ../listings/ch02-guessing-game-tutorial/listing-02-06/src/main.rs}}
```

<span class="caption">Listing 2-6: Complete guessing game code</span>
</Listing>

At this point, you’ve successfully built the guessing game. Congratulations!

Expand Down
4 changes: 2 additions & 2 deletions rustbook-en/src/ch03-03-how-functions-work.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,13 @@ We’ve actually already used statements and expressions. Creating a variable an
assigning a value to it with the `let` keyword is a statement. In Listing 3-1,
`let y = 6;` is a statement.

<span class="filename">Filename: src/main.rs</span>
<Listing number="3-1" file-name="src/main.rs" caption="A `main` function declaration containing one statement">

```rust
{{#rustdoc_include ../listings/ch03-common-programming-concepts/listing-03-01/src/main.rs}}
```

<span class="caption">Listing 3-1: A `main` function declaration containing one statement</span>
</Listing>

Function definitions are also statements; the entire preceding example is a
statement in itself.
Expand Down
20 changes: 8 additions & 12 deletions rustbook-en/src/ch03-05-control-flow.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,14 +120,13 @@ Rust branching construct called `match` for these cases.
Because `if` is an expression, we can use it on the right side of a `let`
statement to assign the outcome to a variable, as in Listing 3-2.

<span class="filename">Filename: src/main.rs</span>
<Listing number="3-2" file-name="src/main.rs" caption="Assigning the result of an `if` expression to a variable">

```rust
{{#rustdoc_include ../listings/ch03-common-programming-concepts/listing-03-02/src/main.rs}}
```

<span class="caption">Listing 3-2: Assigning the result of an `if` expression
to a variable</span>
</Listing>

The `number` variable will be bound to a value based on the outcome of the `if`
expression. Run this code to see what happens:
Expand Down Expand Up @@ -284,14 +283,13 @@ that Rust has a built-in language construct for it, called a `while` loop. In
Listing 3-3, we use `while` to loop the program three times, counting down each
time, and then, after the loop, print a message and exit.

<span class="filename">Filename: src/main.rs</span>
<Listing number="3-3" file-name="src/main.rs" caption="Using a `while` loop to run code while a condition holds true">

```rust
{{#rustdoc_include ../listings/ch03-common-programming-concepts/listing-03-03/src/main.rs}}
```

<span class="caption">Listing 3-3: Using a `while` loop to run code while a
condition holds true</span>
</Listing>

This construct eliminates a lot of nesting that would be necessary if you used
`loop`, `if`, `else`, and `break`, and it’s clearer. While a condition
Expand All @@ -303,14 +301,13 @@ You can also use the `while` construct to loop over the elements of a
collection, such as an array. For example, the loop in Listing 3-4 prints each
element in the array `a`.

<span class="filename">Filename: src/main.rs</span>
<Listing number="3-4" file-name="src/main.rs" caption="Looping through each element of a collection using a `while` loop">

```rust
{{#rustdoc_include ../listings/ch03-common-programming-concepts/listing-03-04/src/main.rs}}
```

<span class="caption">Listing 3-4: Looping through each element of a collection
using a `while` loop</span>
</Listing>

Here, the code counts up through the elements in the array. It starts at index
`0`, and then loops until it reaches the final index in the array (that is,
Expand All @@ -335,14 +332,13 @@ index is within the bounds of the array on every iteration through the loop.
As a more concise alternative, you can use a `for` loop and execute some code
for each item in a collection. A `for` loop looks like the code in Listing 3-5.

<span class="filename">Filename: src/main.rs</span>
<Listing number="3-5" file-name="src/main.rs" caption="Looping through each element of a collection using a `for` loop">

```rust
{{#rustdoc_include ../listings/ch03-common-programming-concepts/listing-03-05/src/main.rs}}
```

<span class="caption">Listing 3-5: Looping through each element of a collection
using a `for` loop</span>
</Listing>

When we run this code, we’ll see the same output as in Listing 3-4. More
importantly, we’ve now increased the safety of the code and eliminated the
Expand Down
48 changes: 21 additions & 27 deletions rustbook-en/src/ch11-01-writing-tests.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ $ cd adder
The contents of the *src/lib.rs* file in your `adder` library should look like
Listing 11-1.

<span class="filename">Filename: src/lib.rs</span>
<Listing number="11-1" file-name="src/lib.rs" caption="The test module and function generated automatically by `cargo new`">

<!-- manual-regeneration
cd listings/ch11-writing-automated-tests
Expand All @@ -59,8 +59,7 @@ cd ../../..
{{#rustdoc_include ../listings/ch11-writing-automated-tests/listing-11-01/src/lib.rs}}
```

<span class="caption">Listing 11-1: The test module and function generated
automatically by `cargo new`</span>
</Listing>

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
Expand All @@ -76,12 +75,13 @@ passes.
The `cargo test` command runs all tests in our project, as shown in Listing
11-2.

<Listing number="11-2" caption="The output from running the automatically generated test">

```console
{{#include ../listings/ch11-writing-automated-tests/listing-11-01/output.txt}}
```

<span class="caption">Listing 11-2: The output from running the automatically
generated test</span>
</Listing>

Cargo compiled and ran the test. We see the line `running 1 test`. The next
line shows the name of the generated test function, called `it_works`, and that
Expand Down Expand Up @@ -133,24 +133,24 @@ marked as failed. In Chapter 9, we talked about how the simplest way to panic
is to call the `panic!` macro. Enter the new test as a function named
`another`, so your *src/lib.rs* file looks like Listing 11-3.

<span class="filename">Filename: src/lib.rs</span>
<Listing number="11-3" file-name="src/lib.rs" caption="Adding a second test that will fail because we call the `panic!` macro">

```rust,panics,noplayground
{{#rustdoc_include ../listings/ch11-writing-automated-tests/listing-11-03/src/lib.rs:here}}
```

<span class="caption">Listing 11-3: Adding a second test that will fail because
we call the `panic!` macro</span>
</Listing>

Run the tests again using `cargo test`. The output should look like Listing
11-4, which shows that our `exploration` test passed and `another` failed.

<Listing number="11-4" caption="Test results when one test passes and one test fails">

```console
{{#include ../listings/ch11-writing-automated-tests/listing-11-03/output.txt}}
```

<span class="caption">Listing 11-4: Test results when one test passes and one
test fails</span>
</Listing>

Instead of `ok`, the line `test tests::another` shows `FAILED`. Two new
sections appear between the individual results and the summary: the first
Expand Down Expand Up @@ -182,29 +182,27 @@ In Chapter 5, Listing 5-15, we used a `Rectangle` struct and a `can_hold`
method, which are repeated here in Listing 11-5. Let’s put this code in the
*src/lib.rs* file, then write some tests for it using the `assert!` macro.

<span class="filename">Filename: src/lib.rs</span>
<Listing number="11-5" file-name="src/lib.rs" caption="The `Rectangle` struct and its `can_hold` method from Chapter 5">

```rust,noplayground
{{#rustdoc_include ../listings/ch11-writing-automated-tests/listing-11-05/src/lib.rs:here}}
```

<span class="caption">Listing 11-5: Using the `Rectangle` struct and its
`can_hold` method from Chapter 5</span>
</Listing>

The `can_hold` method returns a Boolean, which means it’s a perfect use case
for the `assert!` macro. In Listing 11-6, we write a test that exercises the
`can_hold` method by creating a `Rectangle` instance that has a width of 8 and
a height of 7 and asserting that it can hold another `Rectangle` instance that
has a width of 5 and a height of 1.

<span class="filename">Filename: src/lib.rs</span>
<Listing number="11-6" file-name="src/lib.rs" caption="A test for `can_hold` that checks whether a larger rectangle can indeed hold a smaller rectangle">

```rust,noplayground
{{#rustdoc_include ../listings/ch11-writing-automated-tests/listing-11-06/src/lib.rs:here}}
```

<span class="caption">Listing 11-6: A test for `can_hold` that checks whether a
larger rectangle can indeed hold a smaller rectangle</span>
</Listing>

Note that we’ve added a new line inside the `tests` module: `use super::*;`.
The `tests` module is a regular module that follows the usual visibility rules
Expand Down Expand Up @@ -276,14 +274,13 @@ expression, without printing the values that led to the `false` value.
In Listing 11-7, we write a function named `add_two` that adds `2` to its
parameter, then we test this function using the `assert_eq!` macro.

<span class="filename">Filename: src/lib.rs</span>
<Listing number="11-7" file-name="src/lib.rs" caption="Testing the function `add_two` using the `assert_eq!` macro">

```rust,noplayground
{{#rustdoc_include ../listings/ch11-writing-automated-tests/listing-11-07/src/lib.rs}}
```

<span class="caption">Listing 11-7: Testing the function `add_two` using the
`assert_eq!` macro</span>
</Listing>

Let’s check that it passes!

Expand Down Expand Up @@ -320,7 +317,7 @@ assertion functions are called `expected` and `actual`, and the order in which
we specify the arguments matters. However, in Rust, they’re called `left` and
`right`, and the order in which we specify the value we expect and the value
the code produces doesn’t matter. We could write the assertion in this test as
`assert_eq!(add_two(2), 4)`, which would result in the same failure message
`assert_eq!(4, add_two(2))`, which would result in the same failure message
that displays `` assertion failed: `(left == right)` ``.

The `assert_ne!` macro will pass if the two values we give it are not equal and
Expand Down Expand Up @@ -361,7 +358,6 @@ For example, let’s say we have a function that greets people by name and we
want to test that the name we pass into the function appears in the output:

<span class="filename">Filename: src/lib.rs</span>

```rust,noplayground
{{#rustdoc_include ../listings/ch11-writing-automated-tests/no-listing-05-greeter/src/lib.rs}}
```
Expand Down Expand Up @@ -421,14 +417,13 @@ inside the function doesn’t panic.
Listing 11-8 shows a test that checks that the error conditions of `Guess::new`
happen when we expect them to.

<span class="filename">Filename: src/lib.rs</span>
<Listing number="11-8" file-name="src/lib.rs" caption="Testing that a condition will cause a `panic!`">

```rust,noplayground
{{#rustdoc_include ../listings/ch11-writing-automated-tests/listing-11-08/src/lib.rs}}
```

<span class="caption">Listing 11-8: Testing that a condition will cause a
`panic!`</span>
</Listing>

We place the `#[should_panic]` attribute after the `#[test]` attribute and
before the test function it applies to. Let’s look at the result when this test
Expand Down Expand Up @@ -464,14 +459,13 @@ consider the modified code for `Guess` in Listing 11-9 where the `new` function
panics with different messages depending on whether the value is too small or
too large.

<span class="filename">Filename: src/lib.rs</span>
<Listing number="11-9" file-name="src/lib.rs" caption="Testing for a `panic!` with a panic message containing a specified substring">

```rust,noplayground
{{#rustdoc_include ../listings/ch11-writing-automated-tests/listing-11-09/src/lib.rs:here}}
```

<span class="caption">Listing 11-9: Testing for a `panic!` with a panic message
containing a specified substring</span>
</Listing>

This test will pass because the value we put in the `should_panic` attribute’s
`expected` parameter is a substring of the message that the `Guess::new`
Expand Down
Loading

0 comments on commit 8484f17

Please sign in to comment.