forked from ava57r/rust-cookbook
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Restructure Cookbook (rust-lang-nursery#404)
- Loading branch information
Showing
175 changed files
with
6,550 additions
and
6,692 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,16 +44,68 @@ To run the cookbook test suite: | |
cargo test | ||
``` | ||
|
||
## Linters | ||
|
||
The Rust Cookbook comes with link checking and spell checking linters that | ||
run on the continuous integration server. These linters should be run locally | ||
before submitting a pull request to ensure there are no dead links or spelling | ||
errors made. | ||
|
||
To install the link checker, review the documentation for [python] to install | ||
python 3.6 and pip3. Installing link-checker once the dependencies are met | ||
is done with pip3. | ||
|
||
``` | ||
[sudo] pip3 install link-checker==0.1.0 | ||
``` | ||
|
||
Alternatively, set up the user install directory on your PATH variable and | ||
install link-checker for your user | ||
|
||
``` | ||
pip3 install -user link-checker==0.1.0 | ||
``` | ||
|
||
Checking the links of the book locally first requires the book to be built | ||
with mdBook. From the root directory of the cookbook, the following commands | ||
run the link checker. | ||
|
||
``` | ||
mdbook build | ||
link-checker ./book | ||
``` | ||
|
||
The aspell binary provides spell checking. Apt packages provide installation | ||
on Debian based operating systems. | ||
|
||
``` | ||
[sudo] apt install aspell -y | ||
``` | ||
|
||
To check the spelling of the Rust Cookbook locally, run the following command | ||
from the root of the Cookbook. | ||
|
||
``` | ||
./ci/spellchecker.sh | ||
``` | ||
|
||
If the spell checker finds a misspelled word, you have the opportunity to | ||
correct the spelling mistake with the number keys. If the spelling mistake | ||
is erroneous, add the word to the dictionary located in `ci/dictionary.txt`. | ||
Pressing `a` or `l` will not add the word to the custom dictionary. | ||
|
||
[mdbook]: http://azerupi.github.io/mdBook/index.html | ||
[python]: https://packaging.python.org/tutorials/installing-packages/#install-pip-setuptools-and-wheel | ||
[skeptic]: https://github.com/brson/rust-skeptic | ||
|
||
|
||
## Finding what to contribute | ||
|
||
This project is intended to be simple to contribute to, and to always | ||
have obvious next work items available. If at any time there is not | ||
something obvious to contribute, that is a bug. Please ask for | ||
assistance on the [libz blitz] thread, or email Brian Anderson | ||
directly ([email protected]). | ||
something obvious to contribute, that is a bug. Feel free to ask for | ||
additional support at the | ||
[Rust Ecosystem Working Group](https://gitter.im/rust-lang/WG-ecosystem). | ||
|
||
The development process for the cookbook is presently oriented around | ||
crates: we decide which crates to represent in the cookbook, then come | ||
|
@@ -146,16 +198,18 @@ something a typical Rust user typically wants to do. | |
#### Description | ||
|
||
Describe traits imported and the methods used. Think about what information | ||
supports the use case and might not be obvious to someone new. Keep the | ||
description to 1-4 sentences, avoiding explanations outside the scope of the | ||
supports the use case and might not be obvious to someone new. Keep the | ||
description to 1-4 sentences, avoiding explanations outside the scope of the | ||
code sample. | ||
|
||
Use third person narative of the code execution, taking the opportunity | ||
to link to API documentation. Hyperlink all references to APIs, either | ||
Use third person narrative of the code execution, taking the opportunity | ||
to link to API documentation. Always use | ||
[active voice](https://www.plainlanguage.gov/guidelines/conversational/use-active-voice/). | ||
Hyperlink all references to APIs, either | ||
on doc.rust-lang.org/std or docs.rs, and style them as `code`. Use | ||
wildcard version specifiers for crate links. | ||
|
||
Any requirements to execute the code that are not apparent, such as | ||
Any requirements to execute the code that are not apparent, such as | ||
passing environment flags, or configuring `Cargo.toml` should be added | ||
after the code sample. | ||
|
||
|
@@ -164,8 +218,8 @@ after the code sample. | |
> distribution, then sample from that distribution using | ||
> [`Distribution::sample`] with help of a random-number | ||
> generator [`rand::Rng`]. | ||
> | ||
> The [distributions available are documented here][rand-distributions]. | ||
> | ||
> The [distributions available are documented here][rand-distributions]. | ||
> An example using the [`Normal`] distribution is shown below. | ||
[uniform distribution]: https://en.wikipedia.org/wiki/Uniform_distribution_(continuous) | ||
|
@@ -190,11 +244,11 @@ error handling correctly and propagate errors with `?` (not `try!`, `urwrap`, or | |
`expect`). If there is no need for error handling in the example, prefer `main()`. | ||
|
||
Avoid glob imports (`*`), even for preludes, so that users can see what | ||
traits are called. (Some crates might consider using glob imports for preludes | ||
traits are called. (Some crates might consider using glob imports for preludes | ||
best practice, making this awkward.) | ||
|
||
Examples should be simple and obvious enough that an experienced dev | ||
do not need comments. | ||
do not need comments. | ||
|
||
Examples should compile without warnings, clippy lint warnings, or panics. | ||
The code should be formatted by rustfmt. Hide all error boilerplate and | ||
|
@@ -206,9 +260,9 @@ explanation in the description. | |
|
||
> ```rust | ||
> extern crate rand; | ||
> | ||
> | ||
> use rand::distributions::{Normal, Distribution}; | ||
> | ||
> | ||
> fn main() { | ||
> let mut rng = rand::thread_rng(); | ||
> let normal = Normal::new(2.0, 3.0); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# Algorithms | ||
|
||
| Recipe | Crates | Categories | | ||
|--------|--------|------------| | ||
| [Generate random numbers][ex-rand] | [![rand-badge]][rand] | [![cat-science-badge]][cat-science] | | ||
| [Generate random numbers within a range][ex-rand-range] | [![rand-badge]][rand] | [![cat-science-badge]][cat-science] | | ||
| [Generate random numbers with given distribution][ex-rand-dist] | [![rand-badge]][rand] | [![cat-science-badge]][cat-science] | | ||
| [Generate random values of a custom type][ex-rand-custom] | [![rand-badge]][rand] | [![cat-science-badge]][cat-science] | | ||
| [Create random passwords from a set of alphanumeric characters][ex-rand-passwd] | [![rand-badge]][rand] | [![cat-os-badge]][cat-os] | | ||
| [Create random passwords from a set of user-defined characters][ex-rand-choose] | [![rand-badge]][rand] | [![cat-os-badge]][cat-os] | | ||
|
||
[ex-rand]: algorithms/randomness.html#generate-random-numbers | ||
[ex-rand-range]: algorithms/randomness.html#generate-random-numbers-within-a-range | ||
[ex-rand-dist]: algorithms/randomness.html#generate-random-numbers-with-given-distribution | ||
[ex-rand-custom]: algorithms/randomness.html#generate-random-values-of-a-custom-type | ||
[ex-rand-passwd]: algorithms/randomness.html#create-random-passwords-from-a-set-of-alphanumeric-characters | ||
[ex-rand-choose]: algorithms/randomness.html#create-random-passwords-from-a-set-of-user-defined-characters | ||
|
||
{{#include links.md}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# Generate Random Values | ||
|
||
{{#include randomness/rand.md}} | ||
|
||
{{#include randomness/rand-range.md}} | ||
|
||
{{#include randomness/rand-dist.md}} | ||
|
||
{{#include randomness/rand-custom.md}} | ||
|
||
{{#include randomness/rand-passwd.md}} | ||
|
||
{{#include randomness/rand-choose.md}} | ||
|
||
{{#include ../links.md}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
## Create random passwords from a set of user-defined characters | ||
|
||
[![rand-badge]][rand] [![cat-os-badge]][cat-os] | ||
|
||
Randomly generates a string of given length ASCII characters with custom user-defined bytestring, with [`choose`]. | ||
|
||
```rust | ||
extern crate rand; | ||
|
||
use rand::{thread_rng, Rng}; | ||
|
||
fn main() { | ||
const CHARSET: &[u8] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZ\ | ||
abcdefghijklmnopqrstuvwxyz\ | ||
0123456789)(*&^%$#@!~"; | ||
|
||
let mut rng = thread_rng(); | ||
let password: Option<String> = (0..30) | ||
.map(|_| Some(*rng.choose(CHARSET)? as char)) | ||
.collect(); | ||
|
||
println!("{:?}", password); | ||
} | ||
``` | ||
|
||
[`choose`]: https://docs.rs/rand/*/rand/trait.Rng.html#method.choose |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
## Generate random values of a custom type | ||
|
||
[![rand-badge]][rand] [![cat-science-badge]][cat-science] | ||
|
||
Randomly generates a tuple `(i32, bool, f64)` and variable of user defined type `Point`. | ||
Implements the [`Distribution`] trait on type Point for [`Standard`] in order to allow random generation. | ||
|
||
```rust | ||
extern crate rand; | ||
|
||
use rand::Rng; | ||
use rand::distributions::{Distribution, Standard}; | ||
|
||
#[derive(Debug)] | ||
struct Point { | ||
x: i32, | ||
y: i32, | ||
} | ||
|
||
impl Distribution<Point> for Standard { | ||
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> Point { | ||
let (rand_x, rand_y) = rng.gen(); | ||
Point { | ||
x: rand_x, | ||
y: rand_y, | ||
} | ||
} | ||
} | ||
|
||
fn main() { | ||
let mut rng = rand::thread_rng(); | ||
let rand_tuple = rng.gen::<(i32, bool, f64)>(); | ||
let rand_point: Point = rng.gen(); | ||
println!("Random tuple: {:?}", rand_tuple); | ||
println!("Random Point: {:?}", rand_point); | ||
} | ||
``` | ||
|
||
[`Distribution`]: https://docs.rs/rand/*/rand/distributions/trait.Distribution.html | ||
[`Standard`]: https://docs.rs/rand/*/rand/distributions/struct.Standard.html |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
## Generate random numbers with given distribution | ||
|
||
[![rand-badge]][rand] [![cat-science-badge]][cat-science] | ||
|
||
By default, random numbers have [uniform distribution]. | ||
To generate numbers with other distributions you instantiate a | ||
distribution, then sample from that distribution using | ||
[`IndependentSample::ind_sample`] with help of a random-number | ||
generator [`rand::Rng`]. | ||
|
||
The [distributions available are documented here][rand-distributions]. An example using the | ||
[`Normal`] distribution is shown below. | ||
|
||
```rust | ||
extern crate rand; | ||
|
||
use rand::distributions::{Normal, Distribution}; | ||
|
||
fn main() { | ||
let mut rng = rand::thread_rng(); | ||
let normal = Normal::new(2.0, 3.0); | ||
let v = normal.sample(&mut rng); | ||
println!("{} is from a N(2, 9) distribution", v) | ||
} | ||
``` | ||
|
||
[`Distribution::sample`]: https://docs.rs/rand/*/rand/distributions/trait.Distribution.html#tymethod.sample | ||
[`Normal`]: https://docs.rs/rand/*/rand/distributions/normal/struct.Normal.html | ||
[`rand::Rng`]: https://docs.rs/rand/*/rand/trait.Rng.html | ||
[rand-distributions]: https://docs.rs/rand/*/rand/distributions/index.html | ||
|
||
[uniform distribution]: https://en.wikipedia.org/wiki/Uniform_distribution_(continuous) |
Oops, something went wrong.