Compare commits

...

14 Commits

Author SHA1 Message Date
Vilem Liepelt b7b138ff1a
Merge d66cb9871d into d207d894cc 2024-04-25 11:32:19 -06:00
Chris Krycho d207d894cc
Merge pull request #3898 from rust-lang/chriskrycho/crab-pincers
Ch. 2: intentionally use `{}` at first
2024-04-19 13:48:19 -06:00
Chris Krycho 0b4e00345c
Merge pull request #3897 from nyurik/redirect-lints
Inline fmt lints for `redirects/`
2024-04-19 13:48:13 -06:00
Chris Krycho 502d012cab Ch. 2: intentionally use `{}` at first
This supports the prose in the guessing game tutorial, which uses this
as a way of teaching where you can do `{foo}` and where you cannot.
2024-04-19 13:42:05 -06:00
Yuri Astrakhan c13271897d Inline fmt lints for `redirects/`
I couldn't find any documentation of how `redirects/` dir is being used,
but fixed inlined format args for consistency
2024-04-19 15:40:41 -04:00
Chris Krycho 8a79f3e15d
Merge pull request #3287 from bravequickcleverfibreyarn/listing-12-02
ch12-03: `expect` is not used multitude times by this chapter
2024-04-19 13:36:49 -06:00
Chris Krycho 62516ee9e3
Merge pull request #3896 from nyurik/inline-fmt-arg
Inline format args to `tools/src`
2024-04-19 13:36:10 -06:00
Chris Krycho f3d9661aff
Merge pull request #3290 from agotsis/closure-grammar
Improve awkward phrasing around the kinds of closures
2024-04-19 13:35:36 -06:00
Chris Krycho 54726034c5
Ch. 12.3: be clearer about how `expect` was used 2024-04-19 13:35:31 -06:00
Chris Krycho 8264d69aa6
Ch. 13: `unwrap_or_else` takes all three kinds of closures 2024-04-19 13:31:53 -06:00
Yuri Astrakhan c5211edeec Apply clippy::uninlined_format_args fix 2024-04-19 15:20:22 -04:00
Vilem Liepelt d66cb9871d Add example of `cargo add` 2022-08-29 00:38:29 +02:00
Alex Gotsis 6c63c46817 Improve awkward phrasing around the kinds of closures 2022-08-05 00:56:52 -07:00
JirCep 15616bc09b `expect` is not used multitude times by this chapter 2022-08-03 22:14:44 +02:00
19 changed files with 160 additions and 29 deletions

View File

@ -25,7 +25,7 @@ fn main() {
// ANCHOR_END: expect
// ANCHOR: print_guess
println!("You guessed: {guess}");
println!("You guessed: {}", guess);
// ANCHOR_END: print_guess
}
// ANCHOR: all

View File

@ -0,0 +1,83 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
[[package]]
name = "cfg-if"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "getrandom"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c9495705279e7140bf035dde1f6e750c162df8b625267cd52cc44e0b156732c8"
dependencies = [
"cfg-if",
"libc",
"wasi",
]
[[package]]
name = "guessing_game"
version = "0.1.0"
dependencies = [
"rand",
]
[[package]]
name = "libc"
version = "0.2.86"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b7282d924be3275cec7f6756ff4121987bc6481325397dde6ba3e7802b1a8b1c"
[[package]]
name = "ppv-lite86"
version = "0.2.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac74c624d6b2d21f425f752262f42188365d7b8ff1aff74c82e45136510a4857"
[[package]]
name = "rand"
version = "0.8.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0ef9e7e66b4468674bfcb0c81af8b7fa0bb154fa9f28eb840da5c447baeb8d7e"
dependencies = [
"libc",
"rand_chacha",
"rand_core",
"rand_hc",
]
[[package]]
name = "rand_chacha"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e12735cf05c9e10bf21534da50a147b924d555dc7a547c42e6bb2d5b6017ae0d"
dependencies = [
"ppv-lite86",
"rand_core",
]
[[package]]
name = "rand_core"
version = "0.6.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "34cf66eb183df1c5876e2dcf6b13d57340741e8dc255b48e40a26de954d06ae7"
dependencies = [
"getrandom",
]
[[package]]
name = "rand_hc"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3190ef7066a446f2e7f42e239d161e905420ccab01eb967c9eb27d21b2322a73"
dependencies = [
"rand_core",
]
[[package]]
name = "wasi"
version = "0.10.2+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fd6fbd9a79829dd1ad0cc20627bf1ed606756a7f77edff7b66b7064f9cb327c6"

View File

@ -0,0 +1,8 @@
[package]
name = "guessing_game"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

View File

@ -0,0 +1,18 @@
$ cargo add rand@0.8.3
Updating crates.io index
Adding rand v0.8.3 to dependencies.
Features:
+ alloc
+ getrandom
+ libc
+ rand_chacha
+ rand_hc
+ std
+ std_rng
- log
- nightly
- packed_simd
- serde
- serde1
- simd_support
- small_rng

View File

@ -0,0 +1,15 @@
use std::io;
fn main() {
println!("Guess the number!");
println!("Please input your guess.");
let mut guess = String::new();
io::stdin()
.read_line(&mut guess)
.expect("Failed to read line");
println!("You guessed: {guess}");
}

View File

@ -7,7 +7,7 @@
```rust
let b = Box::new(5);
println!("b = {}", b);
println!("b = {b}");
```
---

View File

@ -11,7 +11,7 @@ let v1 = vec![1, 2, 3];
let v1_iter = v1.iter();
for val in v1_iter {
println!("Got: {}", val);
println!("Got: {val}");
}
```

View File

@ -11,7 +11,7 @@
// |
let r = &x; // --+--+-- 'a
// | |
println!("r: {}", r); // | |
println!("r: {r}"); // | |
// --+ |
} // -----+
```

View File

@ -6,9 +6,9 @@
```rust
let mut x = 5;
println!("The value of x is: {}", x);
println!("The value of x is: {x}");
x = 6;
println!("The value of x is: {}", x);
println!("The value of x is: {x}");
```
---

View File

@ -367,6 +367,13 @@ Cargo considers these versions to have public APIs compatible with version
will still compile with the code in this chapter. Any version 0.9.0 or greater
is not guaranteed to have the same API as what the following examples use.
Instead of manually adding the dependency by editing the *Cargo.toml* file, we
can ask Cargo to add the dependency for us, using the `cargo add` command:
```console
{{#include ../listings/ch02-guessing-game-tutorial/no-listing-06-cargo-add/output.txt}}
```
Now, without changing any of the code, lets build the project, as shown in
Listing 2-2.

View File

@ -23,13 +23,13 @@ example, the file could be missing, or we might not have permission to open it.
Right now, regardless of the situation, wed print the same error message for
everything, which wouldnt give the user any information!
Fourth, we use `expect` repeatedly to handle different errors, and if the user
runs our program without specifying enough arguments, theyll get an `index out
of bounds` error from Rust that doesnt clearly explain the problem. It would
be best if all the error-handling code were in one place so future maintainers
had only one place to consult the code if the error-handling logic needed to
change. Having all the error-handling code in one place will also ensure that
were printing messages that will be meaningful to our end users.
Fourth, we use `expect` to handle an error, and if the user runs our program
without specifying enough arguments, theyll get an `index out of bounds` error
from Rust that doesnt clearly explain the problem. It would be best if all the
error-handling code were in one place so future maintainers had only one place
to consult the code if the error-handling logic needed to change. Having all the
error-handling code in one place will also ensure that were printing messages
that will be meaningful to our end users.
Lets address these four problems by refactoring our project.

View File

@ -329,8 +329,8 @@ Using `FnOnce` in the trait bound expresses the constraint that
`unwrap_or_else` is only going to call `f` at most one time. In the body of
`unwrap_or_else`, we can see that if the `Option` is `Some`, `f` wont be
called. If the `Option` is `None`, `f` will be called once. Because all
closures implement `FnOnce`, `unwrap_or_else` accepts the most different kinds
of closures and is as flexible as it can be.
closures implement `FnOnce`, `unwrap_or_else` accepts all three kinds of
closures and is as flexible as it can be.
> Note: Functions can implement all three of the `Fn` traits too. If what we
> want to do doesnt require capturing a value from the environment, we can use

View File

@ -8,7 +8,7 @@ fn main() {
let mut buffer = String::new();
if let Err(e) = io::stdin().read_to_string(&mut buffer) {
panic!("{}", e);
panic!("{e}");
}
for line in buffer.lines() {
@ -21,7 +21,7 @@ fn main() {
if is_in_code_block {
is_in_inline_code = false;
is_in_html_tag = false;
println!("{}", line);
println!("{line}");
} else {
let modified_line = &mut String::new();
let mut previous_char = std::char::REPLACEMENT_CHARACTER;
@ -72,7 +72,7 @@ fn main() {
modified_line.push(char_to_push);
previous_char = char_to_push;
}
println!("{}", modified_line);
println!("{modified_line}");
}
}
}

View File

@ -18,7 +18,7 @@ fn main() {
.map(|entry| match entry {
Ok(entry) => entry,
Err(err) => {
eprintln!("{:?}", err);
eprintln!("{err:?}");
std::process::exit(911)
}
})

View File

@ -14,12 +14,12 @@ fn read_md() -> String {
let mut buffer = String::new();
match io::stdin().read_to_string(&mut buffer) {
Ok(_) => buffer,
Err(error) => panic!("{}", error),
Err(error) => panic!("{error}"),
}
}
fn write_md(output: String) {
print!("{}", output);
print!("{output}");
}
fn parse_references(buffer: String) -> (String, HashMap<String, String>) {
@ -81,7 +81,7 @@ fn parse_links((buffer, ref_map): (String, HashMap<String, String>)) -> String {
}
}
};
format!("{} at *{}*", name, val)
format!("{name} at *{val}*")
}
}
});

View File

@ -149,7 +149,7 @@ fn copy_cleaned_rust_file(
if !ANCHOR_OR_SNIP_COMMENTS.is_match(&line)
&& (item_name != "lib.rs" || !EMPTY_MAIN.is_match(&line))
{
writeln!(&mut to_buf, "{}", line)?;
writeln!(&mut to_buf, "{line}")?;
}
}

View File

@ -9,12 +9,12 @@ fn read_md() -> String {
let mut buffer = String::new();
match io::stdin().read_to_string(&mut buffer) {
Ok(_) => buffer,
Err(error) => panic!("{}", error),
Err(error) => panic!("{error}"),
}
}
fn write_md(output: String) {
print!("{}", output);
print!("{output}");
}
fn remove_hidden_lines(input: &str) -> String {

View File

@ -8,7 +8,7 @@ use std::io::Read;
fn main() {
let mut buffer = String::new();
if let Err(e) = io::stdin().read_to_string(&mut buffer) {
panic!("{}", e);
panic!("{e}");
}
let mut refs = HashSet::new();
@ -41,5 +41,5 @@ fn main() {
caps.get(0).unwrap().as_str().to_string()
});
print!("{}", out);
print!("{out}");
}

View File

@ -12,12 +12,12 @@ fn read_md() -> String {
let mut buffer = String::new();
match io::stdin().read_to_string(&mut buffer) {
Ok(_) => buffer,
Err(error) => panic!("{}", error),
Err(error) => panic!("{error}"),
}
}
fn write_md(output: String) {
print!("{}", output);
print!("{output}");
}
fn remove_markup(input: String) -> String {