Skip to content

short_id

A short id of len characters drawn from an Alphabet with a seedable generator, every character equally likely.

The same generator state always gives the same id, which makes it right for tests and reproducible fixtures, and wrong for anything secret (a 64-bit seed can be guessed): use new_short_id (feature random-entropy) for tokens.

use helpers4::id::short_id;

Cargo feature id (enabled by default). To compile only this module:

cargo add helpers4 --no-default-features --features id

or in Cargo.toml:

[dependencies]
helpers4 = { version = "0.0.9", default-features = false, features = ["id"] }
pub fn short_id(alphabet: &Alphabet, len: usize, rng: &mut Rng) -> String
ParameterTypeDescription
alphabet&AlphabetThe characters to draw from.
lenusizeThe number of characters.
rng&mut RngThe generator that decides them.

String — A string of len characters (len characters, not bytes, for a non-ASCII alphabet).

use helpers4::id::{short_id, Alphabet};
use helpers4::random::Rng;

let id = short_id(&Alphabet::safe(), 8, &mut Rng::seeded(1));
assert_eq!(id.chars().count(), 8);
assert_eq!(id, short_id(&Alphabet::safe(), 8, &mut Rng::seeded(1))); // reproducible

src/id/short_id.rs