Struct rand::distributions::Alphanumeric
source · [−]pub struct Alphanumeric;
Expand description
Sample a u8
, uniformly distributed over ASCII letters and numbers:
a-z, A-Z and 0-9.
Example
use rand::{Rng, thread_rng};
use rand::distributions::Alphanumeric;
let mut rng = thread_rng();
let chars: String = (0..7).map(|_| rng.sample(Alphanumeric) as char).collect();
println!("Random chars: {}", chars);
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);
println!("Random string: {}", string);
Passwords
Users sometimes ask whether it is safe to use a string of random characters
as a password. In principle, all RNGs in Rand implementing CryptoRng
are
suitable as a source of randomness for generating passwords (if they are
properly seeded), but it is more conservative to only use randomness
directly from the operating system via the getrandom
crate, or the
corresponding bindings of a crypto library.
When generating passwords or keys, it is important to consider the threat model and in some cases the memorability of the password. This is out of scope of the Rand project, and therefore we defer to the following references:
Trait Implementations
sourceimpl Clone for Alphanumeric
impl Clone for Alphanumeric
sourcefn clone(&self) -> Alphanumeric
fn clone(&self) -> Alphanumeric
Returns a copy of the value. Read more
1.0.0 · sourcefn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from source
. Read more
sourceimpl Debug for Alphanumeric
impl Debug for Alphanumeric
sourceimpl DistString for Alphanumeric
impl DistString for Alphanumeric
sourceimpl Distribution<u8> for Alphanumeric
impl Distribution<u8> for Alphanumeric
sourcefn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> u8
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> u8
Generate a random value of T
, using rng
as the source of randomness.
sourcefn sample_iter<R>(self, rng: R) -> DistIter<Self, R, T>ⓘNotable traits for DistIter<D, R, T>impl<D, R, T> Iterator for DistIter<D, R, T> where
D: Distribution<T>,
R: Rng, type Item = T;
where
R: Rng,
Self: Sized,
fn sample_iter<R>(self, rng: R) -> DistIter<Self, R, T>ⓘNotable traits for DistIter<D, R, T>impl<D, R, T> Iterator for DistIter<D, R, T> where
D: Distribution<T>,
R: Rng, type Item = T;
where
R: Rng,
Self: Sized,
D: Distribution<T>,
R: Rng, type Item = T;
Create an iterator that generates random values of T
, using rng
as
the source of randomness. Read more
impl Copy for Alphanumeric
Auto Trait Implementations
impl RefUnwindSafe for Alphanumeric
impl Send for Alphanumeric
impl Sync for Alphanumeric
impl Unpin for Alphanumeric
impl UnwindSafe for Alphanumeric
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> ToOwned for T where
T: Clone,
impl<T> ToOwned for T where
T: Clone,
type Owned = T
type Owned = T
The resulting type after obtaining ownership.
sourcefn clone_into(&self, target: &mut T)
fn clone_into(&self, target: &mut T)
toowned_clone_into
)Uses borrowed data to replace owned data, usually by cloning. Read more