Skip to content

Green Build #730

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 18 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/check-links.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:
fail: false
checkbox: false
output: ./lychee/out.md
args: "--mode task --base . --config ./ci/lychee.toml ."
args: "--base . --config ./ci/lychee.toml ."

- name: Save lychee cache
uses: actions/cache/save@v4
Expand Down
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ target/
book/
*.swp
lines.txt
.idea/
.idea/
.skeptic-cache
.lycheecache
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"makefile.configureOnOpen": false
}
25 changes: 16 additions & 9 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,28 @@ license = "MIT/Apache-2.0"
publish = false
build = "build.rs"

[features]
default = []
test-rand = []

[dependencies]
ansi_term = "0.11.0"
approx = "0.3"
base64 = "0.9"
bitflags = "1.0"
base64 = "0.22.1"
bitflags = "1.3.2"
byteorder = "1.0"
cc = "1.0"
chrono = "0.4"
clap = "4.5"
crossbeam = "0.5"
crossbeam = "0.8"
crossbeam-channel = "0.3.9"
csv = "1.0"
data-encoding = "2.1.0"
env_logger = "0.11.3"
flate2 = "1.0"
glob = "0.3"
image = "0.20"
image = "0.24"

lazy_static = "1.0"
log = "0.4"
log4rs = "0.8"
Expand All @@ -35,23 +40,25 @@ num = "0.4"
num_cpus = "1.16"
percent-encoding = "2.3"
petgraph = "0.6"
postgres = "0.19"
postgres = "0.19.7"
rand = "0.9"
rand_distr = "0.5.1"
rand_distr = "0.5"
rayon = "1.10"
regex = "1.11"
reqwest = { version = "0.12", features = ["blocking", "json", "stream"] }
ring = "0.17"
rusqlite = { version = "0.32", features = ["chrono"] }
same-file = "1.0"
select = "0.6"

semver = "1.0"
serde = { version = "1.0", features = ["derive"] }
serde_derive = "1.0"
serde_json = "1.0"
sha2 = "0.10"
tar = "0.4"
tempfile = "3.14"
thiserror = "2"
anyhow = "1.0"
threadpool = "1.8"
toml = "0.8"
tokio = { version = "1", features = ["full"] }
Expand All @@ -63,9 +70,9 @@ walkdir = "2.5"
syslog = "5.0"

[build-dependencies]
skeptic = "0.13"
skeptic = { git = "https://github.com/andygauge/rust-skeptic", branch = "rlib-patch" }
walkdir = "2.5"

[dev-dependencies]
skeptic = "0.13"
skeptic = { git = "https://github.com/andygauge/rust-skeptic", branch = "rlib-patch" }
walkdir = "2.5"
2 changes: 1 addition & 1 deletion LICENSE-CC0
Original file line number Diff line number Diff line change
Expand Up @@ -113,4 +113,4 @@ Affirmer's express Statement of Purpose.
CC0 or use of the Work.

For more information, please see
<http://creativecommons.org/publicdomain/zero/1.0/>
<https://creativecommons.org/publicdomain/zero/1.0/>
9 changes: 3 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
# A Rust Cookbook &emsp; [![Build Status][build-badge]][build-url]

[build-badge]: https://github.com/rust-lang-nursery/rust-cookbook/workflows/Deploy%20to%20GitHub%20Pages/badge.svg
[build-url]: https://github.com/rust-lang-nursery/rust-cookbook/actions?query=workflow%3A%22Deploy+to+GitHub+Pages%22
# A Rust Cookbook

**[Read it here]**.

Expand Down Expand Up @@ -92,12 +89,12 @@ For details see [CONTRIBUTING.md] on GitHub.
## License [![CC0-badge]][CC0-deed]

Rust Cookbook is licensed under Creative Commons Zero v1.0 Universal License
([LICENSE-CC0](LICENSE-CC0) or https://creativecommons.org/publicdomain/zero/1.0/legalcode)
([LICENSE-CC0](LICENSE-CC0) or https://creativecommons.org/)

Unless you explicitly state otherwise, any contribution intentionally submitted
for inclusion in Rust Cookbook by you, as defined in the CC0-1.0 license, shall be
[dedicated to the public domain][CC0-deed] and licensed as above, without any additional
terms or conditions.

[CC0-deed]: https://creativecommons.org/publicdomain/zero/1.0/deed.en
[CC0-deed]: https://creativecommons.org/publicdomain/zero/1.0/
[CC0-badge]: https://mirrors.creativecommons.org/presskit/buttons/80x15/svg/cc-zero.svg
14 changes: 14 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,20 @@ const REMOVED_TESTS: &[&str] = &[
];

fn main() {
#[cfg(feature = "test-rand")]
{
let rand_paths = vec![
"./src/algorithms/randomness/rand.md",
"./src/algorithms/randomness/rand-range.md",
"./src/algorithms/randomness/rand-dist.md",
"./src/algorithms/randomness/rand-custom.md",
"./src/algorithms/randomness/rand-passwd.md",
"./src/algorithms/randomness/rand-choose.md",
];
skeptic::generate_doc_tests(&rand_paths[..]);
return;
}

let paths = WalkDir::new("./src/").into_iter()
// convert paths to Strings
.map(|p| p.unwrap().path().to_str().unwrap().to_string())
Expand Down
7 changes: 7 additions & 0 deletions ci/lychee.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,10 @@ exclude_loopback = false

# Check mail addresses
include_mail = true

# Exclude problematic links that consistently fail
exclude = [
# Creative Commons links return 403 for automated requests
"https://creativecommons.org/publicdomain/zero/1.0/",
"https://creativecommons.org/"
]
21 changes: 12 additions & 9 deletions src/about.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,13 @@ Consider this example for "generate random numbers within a range":
[![rand-badge]][rand] [![cat-science-badge]][cat-science]

```rust,edition2018
extern crate rand;
use rand::Rng;

fn main() {
let mut rng = rand::thread_rng();
println!("Random f64: {}", rng.gen::<f64>());
let random_number: u32 = rng.gen();
println!("Random number: {}", random_number);
}
```

Expand Down Expand Up @@ -97,14 +99,15 @@ should read after deciding which crate suites your purpose.

## A note about error handling
Rust has [`std::error::Trait`] which is implemented to handle exceptions.
Handling multiple types of these traits can be simplified using [`anyhow`]
or specified with an `enum` which macros exist to make this easier within
[`thiserror`] for library authors.
This cookbook uses [`anyhow`] for simplified error handling in examples,
which provides easy error propagation and context. For library authors,
[`thiserror`] provides a more structured approach using derive macros
to create custom error types.

Error chain has been shown in this book for historical reasons before Rust
`std` and crates represented macro use as a preference. For more background
on error handling in Rust, read [this page of the Rust book][error-docs]
and [this blog post][error-blog].
This cookbook previously used the `error-chain` crate, but has been updated
to use `anyhow` as it's now the preferred approach for application-level
error handling. For more background on error handling in Rust, read
[this page of the Rust book][error-docs] and [this blog post][error-blog].

## A note about crate representation

Expand All @@ -129,7 +132,7 @@ as are crates that are pending evaluation.
{{#include links.md}}

[index]: intro.html
[error-docs]: https://do.rust-lang.org/book/error-handling.html
[error-docs]: https://doc.rust-lang.org/book/ch09-00-error-handling.html
[error-blog]: https://brson.github.io/2016/11/30/starting-with-error-chain
[error-chain]: https://docs.rs/error-chain/
[Rust Libz Blitz]: https://internals.rust-lang.org/t/rust-libz-blitz/5184
Expand Down
21 changes: 12 additions & 9 deletions src/algorithms/randomness/rand-choose.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,23 @@
[![rand-badge]][rand] [![cat-os-badge]][cat-os]

Randomly generates a string of given length ASCII characters with custom
user-defined bytestring, with [`random_range`].
user-defined bytestring, with [`gen_range`].

```rust,edition2018
extern crate rand;
use rand::Rng;

const CHARSET: &[u8] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZ\
abcdefghijklmnopqrstuvwxyz\
0123456789)(*&^%$#@!~";
const PASSWORD_LEN: usize = 30;

fn main() {
use rand::Rng;
const CHARSET: &[u8] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZ\
abcdefghijklmnopqrstuvwxyz\
0123456789)(*&^%$#@!~";
const PASSWORD_LEN: usize = 30;
let mut rng = rand::rng();
let mut rng = rand::thread_rng();

let password: String = (0..PASSWORD_LEN)
.map(|_| {
let idx = rng.random_range(0..CHARSET.len());
let idx = rng.gen_range(0..CHARSET.len());
CHARSET[idx] as char
})
.collect();
Expand All @@ -25,4 +28,4 @@ fn main() {
}
```

[`random_range`]: https://docs.rs/rand/*/rand/trait.Rng.html#method.random_range
[`gen_range`]: https://docs.rs/rand/*/rand/trait.Rng.html#method.gen_range
16 changes: 9 additions & 7 deletions src/algorithms/randomness/rand-custom.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
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,edition2018
```rust,edition2018,ignore,no_run
extern crate rand;
use rand::Rng;
use rand::distributions::{Distribution, Standard};

Expand All @@ -17,7 +18,8 @@ struct Point {

impl Distribution<Point> for Standard {
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> Point {
let (rand_x, rand_y) = rng.random();
let rand_x: i32 = rng.gen();
let rand_y: i32 = rng.gen();
Point {
x: rand_x,
y: rand_y,
Expand All @@ -26,13 +28,13 @@ impl Distribution<Point> for Standard {
}

fn main() {
let mut rng = rand::rng();
let rand_tuple = rng.random::<(i32, bool, f64)>();
let rand_point: Point = rng.random();
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
[Distribution]: https://docs.rs/rand/*/rand/distributions/trait.Distribution.html
[Standard]: https://docs.rs/rand/*/rand/distributions/struct.Standard.html
18 changes: 12 additions & 6 deletions src/algorithms/randomness/rand-dist.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,21 @@ The [distributions available are documented here][rand-distributions].
An example using the [`Normal`] distribution is shown below.

```rust,edition2018,ignore
use rand_distr::{Distribution, Normal, NormalError};
use rand::rng;
extern crate rand;
extern crate rand_distr;
use rand::Rng;
use rand_distr::{Distribution, LogNormal, Normal};
use rand::distributions::Distribution as RandDistribution;

fn main() {
let mut rng = rand::thread_rng();
let normal = Normal::new(2.0, 3.0).unwrap();
let log_normal = LogNormal::new(1.0, 0.5).unwrap();

fn main() -> Result<(), NormalError> {
let mut rng = rng();
let normal = Normal::new(2.0, 3.0)?;
let v = normal.sample(&mut rng);
println!("{} is from a N(2, 9) distribution", v);
Ok(())
let v = log_normal.sample(&mut rng);
println!("{} is from an ln N(1, 0.25) distribution", v);
}
```

Expand Down
11 changes: 8 additions & 3 deletions src/algorithms/randomness/rand-passwd.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
Randomly generates a string of given length ASCII characters in the range `A-Z,
a-z, 0-9`, with [`Alphanumeric`] sample.

```rust,edition2018
use rand::{rng, Rng};
```rust,edition2018,ignore,no_run
extern crate rand;
use rand::{thread_rng, Rng};
use rand::distributions::Alphanumeric;

fn main() {
let rand_string: String = rng()
let rand_string: String = thread_rng()
.sample_iter(&Alphanumeric)
.take(30)
.map(char::from)
Expand All @@ -21,3 +22,7 @@ fn main() {
```

[`Alphanumeric`]: https://docs.rs/rand/*/rand/distributions/struct.Alphanumeric.html




27 changes: 13 additions & 14 deletions src/algorithms/randomness/rand-range.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,31 @@

[![rand-badge]][rand] [![cat-science-badge]][cat-science]

Generates a random value within half-open `[0, 10)` range (not including `10`) with [`Rng::random_range`].
Generates a random value within half-open `[0, 10)` range (not including `10`) with [`Rng::gen_range`].

```rust,edition2018
```rust,edition2018,ignore
extern crate rand;
use rand::Rng;

fn main() {
let mut rng = rand::rng();
println!("Integer: {}", rng.random_range(0..10));
println!("Float: {}", rng.random_range(0.0..10.0));
let mut rng = rand::thread_rng();
println!("Integer: {}", rng.gen_range(0..10));
println!("Float: {}", rng.gen_range(0.0..10.0));
}
```

[`Uniform`] can obtain values with [uniform distribution].
This has the same effect, but may be faster when repeatedly generating numbers
in the same range.

```rust,edition2018

use rand::distributions::{Distribution, Uniform};
```rust,edition2018,ignore
extern crate rand;
extern crate rand_distr;
use rand::Rng;
use rand_distr::{Distribution, Uniform};

fn main() {
let mut rng = rand::rng();
let mut rng = rand::thread_rng();
let die = Uniform::from(1..7);

loop {
Expand All @@ -34,8 +37,4 @@ fn main() {
}
}
}
```

[`Uniform`]: https://docs.rs/rand/*/rand/distributions/uniform/struct.Uniform.html
[`Rng::random_range`]: https://doc.rust-lang.org/rand/*/rand/trait.Rng.html#method.random_range
[uniform distribution]: https://en.wikipedia.org/wiki/Uniform_distribution_(continuous)
```
Loading