#random-string #uid #fixed-length #unique #encoded-string #base62 #letter

uid_store

Generate random uid strings containing letters, numbers, or base62 values

14 releases

0.1.9 Aug 31, 2024
0.1.8 Jun 9, 2024
0.1.6 May 25, 2024
0.0.5 Jan 25, 2024
0.0.4 Aug 29, 2023

#398 in Data structures

Download history 168/week @ 2024-08-26 18/week @ 2024-09-02 15/week @ 2024-09-16 4/week @ 2024-09-23 44/week @ 2024-09-30

939 downloads per month

MIT license

16KB
291 lines

UID UidStore

Generate random uid strings containing letters, numbers, or base62 values.

Functions to generate random UID's

Generate a random string of a fixed length using any roman letter or numeral.

let uid = random_string(8);

Generate a random string of a fixed length using any roman letter or numeral except letters often confused by people due to difficult to read fonts. Excludes the letters 0 o O l L i I.

let uid = human_random_string(8);

Convert a number to a base62 encoded string:

let uid = number_to_uid(1000);

Convert a base62 encoded string to a number. Returns none if the string is not a valid base62 string.

let uid = uid_to_number(1000).unwrap();

Generate random UID's guaranteed to be unique.

If you are using a short UID, there is a high chance of collision. Use the UidStore to hold previous UID values to ensure a value is not generated twice.

Generate a sequence of 10 character UID strings that are guaranteed to to be unique:

let mut u = UidStore::new();
let uid = u.next(10);

Generate a sequence of UID strings that are limited in size to fit within a unsigned integer type.

let mut u = UidStore::new();
let uid = u.next_u16();
let uid = u.next_u32();
let uid = u.next_u64();

Generate a new uid if this uid is already in the bucket.

let mut u = UidStore::new();
if let Some(uid) = u.make_unique("0123456789") {
    println!("New UID generated. {}", uid);
}

No runtime deps