randoid
Rust nanoid implementation
This is a rust implementation of nanoids.
It generates unique IDs as strings that are more compact than UUIDs.
By default, it generates strings of 21 characters from an alphabet of 64 symbols (a-z, A-Z, 0-9, "_", and "-").
Features
This particular implementation of nanoid has the following features (many of which differ from the nanoid crate):
- no_std support
- can be used without allocating (by writing characters directly to output)
- Allows using any
Rngimplementation as a source of random data. - Special case optimized implementation if the size of the alphabet is a power of 2
smartstringsupport, if thesmarstringfeatures is enabled (as an additive feature).
Limitations
- Requires knowing the size of the alphabet at compile time (the main reason for this is it can help the compiler optimize it better)
- Use of generecs could increase compilation time
Feature Flags
alloc: Requires use of thealloccrate, and allows creating an id as aStringstd: Use fullstdlibrarystd-rand: Inlcuderand/stdandrand/std_rngfeatures, and add support for usingthread_rng()as the default source of random data.smartstring: Add a function for creating an id as aSmartString
Usage
Install
[]
= "0.2.0"
Simple
use ;
// All of the below generate a string like "9wxwPU-kQU-RDjYdxj6Eq"
let id = randoid;
let id = randoid!;
let id = default.gen_id;
Custom length
use ;
// The below generate a string like "M_P_lJcWfI"
let id = randoid!;
let id = with_size.gen_id;
Custom alphabet
use ;
let id = randoid!;
let id = with_alphabet.gen_id;
Custom random number generator
use ;
use OsRng;
let id = randoid!;
let id = with_random.gen_id;
About the name
"nanoid" was already taken by a similar library. I considered something like "nano-id" or "nanoid2", but thought those were too similar. Since the IDs are generated randomly, I decided on "randoid" as an abbreviation of "RANDOm ID".
Acknowledgments
The original nanoid of course.
Also, https://github.com/nikolay-govorov/nanoid, as inspiration for this project.