From c831b8dd3b5eefef7adec4f0b02a2a5515b55164 Mon Sep 17 00:00:00 2001 From: Arif Driessen Date: Tue, 20 Jul 2021 08:38:13 +0200 Subject: [PATCH 1/4] Better random chars example --- src/distributions/other.rs | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/src/distributions/other.rs b/src/distributions/other.rs index 0935d055b3e..fada05d3b4b 100644 --- a/src/distributions/other.rs +++ b/src/distributions/other.rs @@ -32,16 +32,11 @@ use std::mem::{self, MaybeUninit}; /// # Example /// /// ``` -/// use std::iter; /// use rand::{Rng, thread_rng}; /// use rand::distributions::Alphanumeric; /// /// let mut rng = thread_rng(); -/// let chars: String = iter::repeat(()) -/// .map(|()| rng.sample(Alphanumeric)) -/// .map(char::from) -/// .take(7) -/// .collect(); +/// let chars: String = (0..7).map(|_| rng.sample(Alphanumeric) as char).collect(); /// println!("Random chars: {}", chars); /// ``` /// From e64df6631eb3be38e75b611bd5aa7a395552f642 Mon Sep 17 00:00:00 2001 From: Diggory Hardy Date: Tue, 17 Aug 2021 09:51:37 +0100 Subject: [PATCH 2/4] Alphanumeric: document usage with DistString trait --- src/distributions/other.rs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/distributions/other.rs b/src/distributions/other.rs index fada05d3b4b..d0f107d9bd5 100644 --- a/src/distributions/other.rs +++ b/src/distributions/other.rs @@ -40,6 +40,13 @@ use std::mem::{self, MaybeUninit}; /// println!("Random chars: {}", chars); /// ``` /// +/// Alternatively, one can use the [`DistString`] trait: +/// ``` +/// use rand::distributions::{Alphanumeric, DistString}; +/// let string = Alphanumeric.sample_string(&mut rand::thread_rng(), 16); +/// println!("Random string: {}", string); +/// ``` +/// /// # Passwords /// /// Users sometimes ask whether it is safe to use a string of random characters From 32343a6f371dd99635bdd74119300e1eb8661a34 Mon Sep 17 00:00:00 2001 From: Diggory Hardy Date: Tue, 17 Aug 2021 09:55:53 +0100 Subject: [PATCH 3/4] Rearrange Cargo.toml --- Cargo.toml | 18 +++++++++--------- rand_core/Cargo.toml | 18 +++++++++--------- 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 1597b1257d6..19cf619a0fe 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,6 +16,15 @@ autobenches = true edition = "2018" include = ["src/", "LICENSE-*", "README.md", "CHANGELOG.md", "COPYRIGHT"] +[package.metadata.docs.rs] +# To build locally: +# RUSTDOCFLAGS="--cfg doc_cfg" cargo +nightly doc --all-features --no-deps --open +all-features = true +rustdoc-args = ["--cfg", "doc_cfg"] + +[package.metadata.playground] +features = ["small_rng", "serde1"] + [features] # Meta-features: default = ["std", "std_rng"] @@ -74,12 +83,3 @@ libc = { version = "0.2.22", optional = true, default-features = false } rand_pcg = { path = "rand_pcg", version = "0.3.0" } # Only to test serde1 bincode = "1.2.1" - -[package.metadata.docs.rs] -# To build locally: -# RUSTDOCFLAGS="--cfg doc_cfg" cargo +nightly doc --all-features --no-deps --open -all-features = true -rustdoc-args = ["--cfg", "doc_cfg"] - -[package.metadata.playground] -features = ["small_rng", "serde1"] diff --git a/rand_core/Cargo.toml b/rand_core/Cargo.toml index 6604bc5a8b1..c9ce4263326 100644 --- a/rand_core/Cargo.toml +++ b/rand_core/Cargo.toml @@ -14,15 +14,6 @@ keywords = ["random", "rng"] categories = ["algorithms", "no-std"] edition = "2018" -[features] -std = ["alloc", "getrandom", "getrandom/std"] # use std library; should be default but for above bug -alloc = [] # enables Vec and Box support without std -serde1 = ["serde"] # enables serde for BlockRng wrapper - -[dependencies] -serde = { version = "1", features = ["derive"], optional = true } -getrandom = { version = "0.2", optional = true } - [package.metadata.docs.rs] # To build locally: # RUSTDOCFLAGS="--cfg doc_cfg" cargo +nightly doc --all-features --no-deps --open @@ -31,3 +22,12 @@ rustdoc-args = ["--cfg", "doc_cfg"] [package.metadata.playground] all-features = true + +[features] +std = ["alloc", "getrandom", "getrandom/std"] # use std library; should be default but for above bug +alloc = [] # enables Vec and Box support without std +serde1 = ["serde"] # enables serde for BlockRng wrapper + +[dependencies] +serde = { version = "1", features = ["derive"], optional = true } +getrandom = { version = "0.2", optional = true } From 1996707ac23d43bc56c5c86b32f32fbbf9b4cfba Mon Sep 17 00:00:00 2001 From: Diggory Hardy Date: Tue, 17 Aug 2021 15:34:25 +0100 Subject: [PATCH 4/4] Review feedback --- src/distributions/other.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/distributions/other.rs b/src/distributions/other.rs index d0f107d9bd5..4c2471e6273 100644 --- a/src/distributions/other.rs +++ b/src/distributions/other.rs @@ -40,7 +40,8 @@ use std::mem::{self, MaybeUninit}; /// println!("Random chars: {}", chars); /// ``` /// -/// Alternatively, one can use the [`DistString`] trait: +/// The [`DistString`] trait provides an easier method of generating +/// a random `String`, and offers more efficient allocation: /// ``` /// use rand::distributions::{Alphanumeric, DistString}; /// let string = Alphanumeric.sample_string(&mut rand::thread_rng(), 16);