Skip to content

Commit 275553c

Browse files
committed
update original
1 parent 2ecc9f8 commit 275553c

File tree

18 files changed

+95
-97
lines changed

18 files changed

+95
-97
lines changed

rust-cookbook/CONTRIBUTING.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -271,9 +271,7 @@ Mark examples that depend on external systems with `no_run` or remove them
271271
if they are not required for the example. Avoid inline comments, preferring
272272
explanation in the description.
273273

274-
> ```rust
275-
> extern crate rand;
276-
>
274+
> ```rust,edition2018
277275
> use rand::distributions::{Normal, Distribution};
278276
>
279277
> fn main() {

rust-cookbook/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ bitflags = "1.0"
1515
byteorder = "1.0"
1616
cc = "1.0"
1717
chrono = "0.4"
18-
clap = "2.29"
18+
clap = "4.5"
1919
crossbeam = "0.5"
2020
crossbeam-channel = "0.3.9"
2121
csv = "1.0"
2222
data-encoding = "2.1.0"
23-
env_logger = "0.5"
23+
env_logger = "0.11.3"
2424
error-chain = "0.12"
2525
flate2 = "1.0"
2626
glob = "0.3"

rust-cookbook/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ If you'd like to read it locally:
1919
```bash
2020
$ git clone https://github.com/rust-lang-nursery/rust-cookbook
2121
$ cd rust-cookbook
22-
$ cargo install mdbook --vers "0.4.5"
22+
$ cargo install mdbook --vers "0.4.43"
2323
$ mdbook serve --open
2424
```
2525

rust-cookbook/build.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
extern crate skeptic;
2-
extern crate walkdir;
3-
41
use walkdir::WalkDir;
52

63
const REMOVED_TESTS: &[&str] = &[

rust-cookbook/ci/install_deps.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ if [[ "${CONTENT_TESTS:-}" == 1 ]]; then
1818
pyenv local 3.6.0
1919
pip3 install --user link-checker==0.1.0
2020
fi
21-
cargo install mdbook --vers '0.4.5' --debug
21+
cargo install mdbook --vers '0.4.43' --debug
2222
fi
2323

2424
exit 0

rust-cookbook/src/cli/arguments/clap-basic.md

Lines changed: 37 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6,39 +6,52 @@ This application describes the structure of its command-line interface using
66
`clap`'s builder style. The [documentation] gives two other possible ways to
77
instantiate an application.
88

9-
In the builder style, `with_name` is the unique identifier that `value_of` will
10-
use to retrieve the value passed. The `short` and `long` options control the
9+
In the builder style, each possible argument is described by an `Arg`
10+
struct. The string given to `Arg::new()` is the internal
11+
name of the argument. The `short` and `long` options control the
1112
flag the user will be expected to type; short flags look like `-f` and long
1213
flags look like `--file`.
1314

15+
The `get_one()` method is used to get an argument's value.
16+
It returns `Some(&`value`)` if the argument was supplied by
17+
the user, else `None`.
18+
19+
The use of `PathBuf` is to allow file paths which are legal
20+
in Linux and MacOS, but not in Rust UTF-8 strings. This is
21+
best practice: one encounters such paths quite rarely in
22+
practice, but when it happens it is really frustrating
23+
without this.
24+
1425
```rust,edition2018
15-
use clap::{Arg, App};
26+
use std::path::PathBuf;
27+
28+
use clap::{Arg, Command, builder::PathBufValueParser};
1629
1730
fn main() {
18-
let matches = App::new("My Test Program")
31+
let matches = Command::new("My Test Program")
1932
.version("0.1.0")
20-
.author("Hackerman Jones <[email protected]>")
2133
.about("Teaches argument parsing")
22-
.arg(Arg::with_name("file")
23-
.short("f")
34+
.arg(Arg::new("file")
35+
.short('f')
2436
.long("file")
25-
.takes_value(true)
26-
.help("A cool file"))
27-
.arg(Arg::with_name("num")
28-
.short("n")
37+
.help("A cool file")
38+
.value_parser(PathBufValueParser::default()))
39+
.arg(Arg::new("num")
40+
.short('n')
2941
.long("number")
30-
.takes_value(true)
3142
.help("Five less than your favorite number"))
3243
.get_matches();
3344
34-
let myfile = matches.value_of("file").unwrap_or("input.txt");
35-
println!("The file passed is: {}", myfile);
45+
let default_file = PathBuf::from("input.txt");
46+
let myfile: &PathBuf = matches.get_one("file").unwrap_or(&default_file);
47+
println!("The file passed is: {}", myfile.display());
3648
37-
let num_str = matches.value_of("num");
49+
let num_str: Option<&String> = matches.get_one("num");
3850
match num_str {
3951
None => println!("No idea what your favorite number is."),
4052
Some(s) => {
41-
match s.parse::<i32>() {
53+
let parsed: Result<i32, _> = s.parse();
54+
match parsed {
4255
Ok(n) => println!("Your favorite number must be {}.", n + 5),
4356
Err(_) => println!("That's not a number! {}", s),
4457
}
@@ -47,24 +60,19 @@ fn main() {
4760
}
4861
```
4962

50-
Usage information is generated by `clap`. The usage for the example application
51-
looks like this.
63+
Usage information is generated by `clap -h`. The usage for
64+
the example application looks like this.
5265

5366
```
54-
My Test Program 0.1.0
55-
Hackerman Jones <[email protected]>
5667
Teaches argument parsing
5768
58-
USAGE:
59-
testing [OPTIONS]
60-
61-
FLAGS:
62-
-h, --help Prints help information
63-
-V, --version Prints version information
69+
Usage: clap-cookbook [OPTIONS]
6470
65-
OPTIONS:
66-
-f, --file <file> A cool file
67-
-n, --number <num> Five less than your favorite number
71+
Options:
72+
-f, --file <file> A cool file
73+
-n, --number <num> Five less than your favorite number
74+
-h, --help Print help
75+
-V, --version Print version
6876
```
6977

7078
We can test the application by running a command like the following.

rust-cookbook/src/concurrency/parallel/rayon-parallel-sort.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ fn main() {
1919
let mut vec = vec![String::new(); 100_000];
2020
vec.par_iter_mut().for_each(|p| {
2121
let mut rng = thread_rng();
22-
*p = (0..5).map(|_| rng.sample(&Alphanumeric)).collect()
22+
*p = (0..5).map(|_| rng.sample(&Alphanumeric) as char).collect()
2323
});
2424
vec.par_sort_unstable();
2525
}

rust-cookbook/src/concurrency/thread/crossbeam-complex.md

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,7 @@ to prevent the entire program from blocking on the worker for-loops. You can
2424
think of the calls to `drop` as signaling that no more messages will be sent.
2525

2626

27-
```rust
28-
extern crate crossbeam;
29-
extern crate crossbeam_channel;
30-
27+
```rust,edition2018
3128
use std::thread;
3229
use std::time::Duration;
3330
use crossbeam_channel::bounded;

rust-cookbook/src/concurrency/thread/crossbeam-spsc.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ fn main() {
3333
```
3434

3535
[crossbeam-channel]: https://docs.rs/crate/crossbeam-channel/
36-
[ex-crossbeam-spawn]: concurrency/threads.html#spawn-a-short-lived-thread
36+
[ex-crossbeam-spawn]: #spawn-a-short-lived-thread
3737
[`crossbeam::scope`]: https://docs.rs/crossbeam/*/crossbeam/fn.scope.html
3838
[`Scope::spawn`]: https://docs.rs/crossbeam/*/crossbeam/thread/struct.Scope.html#method.spawn
3939
[`crossbeam_channel::unbounded`]: https://docs.rs/crossbeam-channel/*/crossbeam_channel/fn.unbounded.html

rust-cookbook/src/concurrency/thread/threadpool-fractal.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ use image::{ImageBuffer, Pixel, Rgb};
2727
# foreign_links {
2828
# MpscRecv(RecvError);
2929
# Io(std::io::Error);
30+
# Image(image::ImageError);
3031
# }
3132
# }
3233
#

0 commit comments

Comments
 (0)