Skip to content

Commit 31f463f

Browse files
committed
Review recipes, updates to language and style
1 parent bd3df03 commit 31f463f

File tree

24 files changed

+82
-334
lines changed

24 files changed

+82
-334
lines changed

Cargo.toml

+2
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ syslog = "4.0"
5454

5555
[build-dependencies]
5656
skeptic = "0.13"
57+
walkdir = "2.0"
5758

5859
[dev-dependencies]
5960
skeptic = "0.13"
61+
walkdir = "2.0"

build.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
extern crate skeptic;
2+
extern crate walkdir;
23

3-
use std::fs;
4+
use walkdir::WalkDir;
45

56
fn main() {
6-
let paths = fs::read_dir("./src/").unwrap()
7+
let paths = WalkDir::new("./src/").into_iter()
78
// convert paths to Strings
89
.map(|p| p.unwrap().path().to_str().unwrap().to_string())
910
// only compile markdown files

src/algorithms/randomness/rand-choose.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,4 @@ fn main() {
2323
}
2424
```
2525

26-
[`choose`]: https://docs.rs/rand/0.4/rand/trait.Rng.html#method.choose
26+
[`choose`]: https://docs.rs/rand/*/rand/trait.Rng.html#method.choose

src/algorithms/randomness/rand-custom.md

+7-5
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,22 @@
33
[![rand-badge]][rand] [![cat-science-badge]][cat-science]
44

55
Randomly generates a tuple `(i32, bool, f64)` and variable of user defined type `Point`.
6-
Implements the [`rand::Rand`] trait for `Point` in order to allow random generation.
6+
Implements the [`Distribution`] trait on type Point for [`Standard`] in order to allow random generation.
77

88
```rust
99
extern crate rand;
1010

11-
use rand::{Rng, Rand};
11+
use rand::Rng;
12+
use rand::distributions::{Distribution, Standard};
1213

1314
#[derive(Debug)]
1415
struct Point {
1516
x: i32,
1617
y: i32,
1718
}
1819

19-
impl Rand for Point {
20-
fn rand<R: Rng>(rng: &mut R) -> Point {
20+
impl Distribution<Point> for Standard {
21+
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> Point {
2122
let (rand_x, rand_y) = rng.gen();
2223
Point {
2324
x: rand_x,
@@ -35,4 +36,5 @@ fn main() {
3536
}
3637
```
3738

38-
[`rand::Rand`]: https://doc.rust-lang.org/rand/0.4/rand/trait.Rand.html
39+
[`Distribution`]: https://docs.rs/rand/*/rand/distributions/trait.Distribution.html
40+
[`Standard`]: https://docs.rs/rand/*/rand/distributions/struct.Standard.html

src/algorithms/randomness/rand-dist.md

+7-8
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,19 @@ The [distributions available are documented here][rand-distributions]. An exampl
1414
```rust
1515
extern crate rand;
1616

17-
use rand::distributions::{Normal, IndependentSample};
17+
use rand::distributions::{Normal, Distribution};
1818

1919
fn main() {
2020
let mut rng = rand::thread_rng();
21-
22-
// mean 2, standard deviation 3:
2321
let normal = Normal::new(2.0, 3.0);
24-
let v = normal.ind_sample(&mut rng);
22+
let v = normal.sample(&mut rng);
2523
println!("{} is from a N(2, 9) distribution", v)
2624
}
2725
```
2826

29-
[`IndependentSample::ind_sample`]: https://doc.rust-lang.org/rand/0.4/rand/distributions/trait.IndependentSample.html#tymethod.ind_sample
30-
[`Normal`]: https://doc.rust-lang.org/rand/0.4/rand/distributions/normal/struct.Normal.html
31-
[`rand::Rng`]: https://doc.rust-lang.org/rand/0.4/rand/trait.Rng.html
32-
[rand-distributions]: https://doc.rust-lang.org/rand/0.4/rand/distributions/index.html
27+
[`Distribution::sample`]: https://docs.rs/rand/*/rand/distributions/trait.Distribution.html#tymethod.sample
28+
[`Normal`]: https://docs.rs/rand/*/rand/distributions/normal/struct.Normal.html
29+
[`rand::Rng`]: https://docs.rs/rand/*/rand/trait.Rng.html
30+
[rand-distributions]: https://docs.rs/rand/*/rand/distributions/index.html
31+
3332
[uniform distribution]: https://en.wikipedia.org/wiki/Uniform_distribution_(continuous)

src/algorithms/randomness/rand-passwd.md

+9-3
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,23 @@
22

33
[![rand-badge]][rand] [![cat-os-badge]][cat-os]
44

5-
Randomly generates a string of given length ASCII characters in the range `A-Z, a-z, 0-9`, with [`gen_ascii_chars`].
5+
Randomly generates a string of given length ASCII characters in the range `A-Z,
6+
a-z, 0-9`, with [`Alphanumeric`] sample.
67

78
```rust
89
extern crate rand;
910

1011
use rand::{thread_rng, Rng};
12+
use rand::distributions::Alphanumeric;
1113

1214
fn main() {
13-
let rand_string: String = thread_rng().gen_ascii_chars().take(30).collect();
15+
let rand_string: String = thread_rng()
16+
.sample_iter(&Alphanumeric)
17+
.take(30)
18+
.collect();
19+
1420
println!("{}", rand_string);
1521
}
1622
```
1723

18-
[`gen_ascii_chars`]: https://docs.rs/rand/0.4/rand/trait.Rng.html#method.gen_ascii_chars
24+
[`Alphanumeric`]: https://docs.rs/rand/*/rand/distributions/struct.Alphanumeric.html#struct.Alphanumaric

src/algorithms/randomness/rand-range.md

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
[ex-rand-range]: #ex-rand-range
2-
<a name="ex-rand-range"></a>
31
## Generate random numbers within a range
42

53
[![rand-badge]][rand] [![cat-science-badge]][cat-science]
@@ -25,14 +23,14 @@ in the same range.
2523
```rust
2624
extern crate rand;
2725

28-
use rand::distributions::{Range, IndependentSample};
26+
use rand::distributions::{Range, Distribution};
2927

3028
fn main() {
3129
let mut rng = rand::thread_rng();
3230
let die = Range::new(1, 7);
3331

3432
loop {
35-
let throw = die.ind_sample(&mut rng);
33+
let throw = die.sample(&mut rng);
3634
println!("Roll the die: {}", throw);
3735
if throw == 6 {
3836
break;

src/algorithms/randomness/rand.md

+6-7
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,28 @@
33
[![rand-badge]][rand] [![cat-science-badge]][cat-science]
44

55
Generates random numbers with help of random-number
6-
generator [`rand::Rng`] obtained via [`rand::thread_rng`].
6+
generator [`rand::Rng`] obtained via [`rand::thread_rng`]. Each thread has an
7+
intialized generator. Integers are uniformly distributed over the range of the
8+
type, and floating point numbers are uniformly distributed from 0 up to but not
9+
including 1.
710

811
```rust
912
extern crate rand;
1013

1114
use rand::Rng;
1215

1316
fn main() {
14-
// Each thread has an automatically-initialised random number generator:
1517
let mut rng = rand::thread_rng();
1618

17-
// Integers are uniformly distributed over the type's whole range:
1819
let n1: u8 = rng.gen();
1920
let n2: u16 = rng.gen();
2021
println!("Random u8: {}", n1);
2122
println!("Random u16: {}", n2);
2223
println!("Random u32: {}", rng.gen::<u32>());
2324
println!("Random i32: {}", rng.gen::<i32>());
24-
25-
// Floating point numbers are uniformly distributed in the half-open range [0, 1)
2625
println!("Random float: {}", rng.gen::<f64>());
2726
}
2827
```
2928

30-
[`rand::Rng`]: https://doc.rust-lang.org/rand/0.4/rand/trait.Rng.html
31-
[`rand::thread_rng`]: https://doc.rust-lang.org/rand/0.4/rand/fn.thread_rng.html
29+
[`rand::Rng`]: https://docs.rs/rand/*/rand/trait.Rng.html
30+
[`rand::thread_rng`]: https://docs.rs/rand/*/rand/fn.thread_rng.html

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

+8-12
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,17 @@ is usually faster than [stable sorting] algorithms.
1313
extern crate rand;
1414
extern crate rayon;
1515

16-
use rand::Rng;
16+
use rand::{Rng, thread_rng};
17+
use rand::distributions::Alphanumeric;
1718
use rayon::prelude::*;
1819

1920
fn main() {
20-
// [1]
21-
let mut vec = vec![String::new(); 100_000];
22-
23-
// [2]
24-
vec.par_iter_mut().for_each(|p| {
25-
// [3]
26-
*p = rand::weak_rng().gen_ascii_chars().take(5).collect()
27-
});
28-
29-
// [4]
30-
vec.par_sort_unstable();
21+
let mut vec = vec![String::new(); 100_000];
22+
vec.par_iter_mut().for_each(|p| {
23+
let mut rng = thread_rng();
24+
*p = (0..5).map(|_| rng.sample(&Alphanumeric)).collect()
25+
});
26+
vec.par_sort_unstable();
3127
}
3228
```
3329

src/development_tools/debugging/config_log/log-timestamp.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
[![log-badge]][log] [![env_logger-badge]][env_logger] [![chrono-badge]][chrono] [![cat-debugging-badge]][cat-debugging]
44

55
Creates a custom logger configuration with [`Builder`].
6-
Each log entry calls [`Local::now`] to get the current [`DateTime`] in local timezone and uses [`DateTime::format`] with [`strftime::specifiers`] to format a timestamp used in the final log.
6+
Each log entry calls [`Local::now`] to get the current [`DateTime`] in local
7+
timezone and uses [`DateTime::format`] with [`strftime::specifiers`] to format
8+
a timestamp used in the final log.
79

810
The example calls [`Builder::format`] to set a closure which formats each
911
message text with timestamp, [`Record::level`] and body ([`Record::args`]).
@@ -14,7 +16,6 @@ extern crate log;
1416
extern crate chrono;
1517
extern crate env_logger;
1618

17-
use std::env;
1819
use std::io::Write;
1920
use chrono::Local;
2021
use env_logger::Builder;

src/encoding/csv/delimiter.md

+4-5
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,11 @@ use csv::ReaderBuilder;
2828
# }
2929

3030
fn run() -> Result<()> {
31-
let data = \
32-
"name place id
33-
Mark Melbourne 46
34-
Ashley Zurich 92";
31+
let data = "name\tplace\tid
32+
Mark\tMelbourne\t46
33+
Ashley\tZurich\t92";
3534

36-
let mut reader = ReaderBuilder::new().delimiter(b' ').from_reader(data.as_bytes());
35+
let mut reader = ReaderBuilder::new().delimiter(b'\t').from_reader(data.as_bytes());
3736
for result in reader.records() {
3837
println!("{:?}", result?);
3938
}

src/hardware/processor/cpu-count.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
[![num_cpus-badge]][num_cpus] [![cat-hardware-support-badge]][cat-hardware-support]
44

5-
Shows the number of logical cpu cores in current machine using [`num_cpus::get`].
5+
Shows the number of logical CPU cores in current machine using [`num_cpus::get`].
66

77
```rust
88
extern crate num_cpus;

src/incomplete/crossbeam.md

-82
This file was deleted.

src/incomplete/petgraph.md

-54
This file was deleted.

0 commit comments

Comments
 (0)