diff --git a/rustbook-en/src/ch02-00-guessing-game-tutorial.md b/rustbook-en/src/ch02-00-guessing-game-tutorial.md index 5aebefdd9..9c9601bf6 100644 --- a/rustbook-en/src/ch02-00-guessing-game-tutorial.md +++ b/rustbook-en/src/ch02-00-guessing-game-tutorial.md @@ -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*. -Filename: src/main.rs + ```rust,ignore {{#rustdoc_include ../listings/ch02-guessing-game-tutorial/listing-02-01/src/main.rs:all}} ``` -Listing 2-1: Code that gets a guess from the user and -prints it + 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 @@ -376,6 +375,8 @@ rm Cargo.lock cargo clean cargo build --> ++ ```console $ cargo build Updating crates.io index @@ -397,8 +398,7 @@ $ cargo build Finished dev [unoptimized + debuginfo] target(s) in 2.53s ``` -Listing 2-2: The output from running `cargo build` after -adding the rand crate as a dependency + 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 @@ -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. -Filename: src/main.rs + ```rust,ignore {{#rustdoc_include ../listings/ch02-guessing-game-tutorial/listing-02-03/src/main.rs:all}} ``` -Listing 2-3: Adding code to generate a random -number + 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 @@ -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. -Filename: src/main.rs + ```rust,ignore,does_not_compile {{#rustdoc_include ../listings/ch02-guessing-game-tutorial/listing-02-04/src/main.rs:here}} ``` -Listing 2-4: Handling the possible return values of -comparing two numbers + First we add another `use` statement, bringing a type called `std::cmp::Ordering` into scope from the standard library. The `Ordering` type @@ -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. -Filename: src/main.rs + ```rust,ignore {{#rustdoc_include ../listings/ch02-guessing-game-tutorial/listing-02-05/src/main.rs:here}} ``` -Listing 2-5: Ignoring a non-number guess and asking for -another guess instead of crashing the program + 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` @@ -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. -Filename: src/main.rs + ```rust,ignore {{#rustdoc_include ../listings/ch02-guessing-game-tutorial/listing-02-06/src/main.rs}} ``` -Listing 2-6: Complete guessing game code + At this point, you’ve successfully built the guessing game. Congratulations! diff --git a/rustbook-en/src/ch03-03-how-functions-work.md b/rustbook-en/src/ch03-03-how-functions-work.md index 2b59f0cd4..b282cbb2a 100644 --- a/rustbook-en/src/ch03-03-how-functions-work.md +++ b/rustbook-en/src/ch03-03-how-functions-work.md @@ -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. -Filename: src/main.rs + ```rust {{#rustdoc_include ../listings/ch03-common-programming-concepts/listing-03-01/src/main.rs}} ``` -Listing 3-1: A `main` function declaration containing one statement + Function definitions are also statements; the entire preceding example is a statement in itself. diff --git a/rustbook-en/src/ch03-05-control-flow.md b/rustbook-en/src/ch03-05-control-flow.md index fcf344288..0c7aeff13 100644 --- a/rustbook-en/src/ch03-05-control-flow.md +++ b/rustbook-en/src/ch03-05-control-flow.md @@ -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. -Filename: src/main.rs + ```rust {{#rustdoc_include ../listings/ch03-common-programming-concepts/listing-03-02/src/main.rs}} ``` -Listing 3-2: Assigning the result of an `if` expression -to a variable + The `number` variable will be bound to a value based on the outcome of the `if` expression. Run this code to see what happens: @@ -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. -Filename: src/main.rs + ```rust {{#rustdoc_include ../listings/ch03-common-programming-concepts/listing-03-03/src/main.rs}} ``` -Listing 3-3: Using a `while` loop to run code while a -condition holds true + 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 @@ -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`. -Filename: src/main.rs + ```rust {{#rustdoc_include ../listings/ch03-common-programming-concepts/listing-03-04/src/main.rs}} ``` -Listing 3-4: Looping through each element of a collection -using a `while` loop + 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, @@ -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. -Filename: src/main.rs + ```rust {{#rustdoc_include ../listings/ch03-common-programming-concepts/listing-03-05/src/main.rs}} ``` -Listing 3-5: Looping through each element of a collection -using a `for` loop + 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