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§

source§

impl Clone for Alphanumeric

source§

fn clone(&self) -> Alphanumeric

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for Alphanumeric

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl DistString for Alphanumeric

source§

fn append_string<R: Rng + ?Sized>( &self, rng: &mut R, string: &mut String, len: usize, )

Append len random chars to string
source§

fn sample_string<R: Rng + ?Sized>(&self, rng: &mut R, len: usize) -> String

Generate a String of len random chars
source§

impl Distribution<u8> for Alphanumeric

source§

fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> u8

Generate a random value of T, using rng as the source of randomness.
source§

fn sample_iter<R>(self, rng: R) -> DistIter<Self, R, T>
where R: Rng, Self: Sized,

Create an iterator that generates random values of T, using rng as the source of randomness. Read more
source§

fn map<F, S>(self, func: F) -> DistMap<Self, F, T, S>
where F: Fn(T) -> S, Self: Sized,

Create a distribution of values of ‘S’ by mapping the output of Self through the closure F Read more
source§

impl Copy for Alphanumeric

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> CloneToUninit for T
where T: Copy,

source§

unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
source§

impl<T> CloneToUninit for T
where T: Clone,

source§

default unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

source§

fn vzip(self) -> V