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):

Limitations

If you want a more generalized alphabet that doesn't have a size that is a power of two and/or isn't know in advance, then rand::distributions::Slice is probably sufficient. For example:

```rust use rand::{Rng, distributions::Slice, thread_rng};

let alphabet = ['1', '2', '3', '4', '5', '6', '7', '9', '0', 'a', 'b', 'c']; let id: String = threadrng().sampleiter(&Slice::new(&alphabet).unwrap()).take(21).collect(); ```

Feature Flags

Usage

Install

toml [dependencies] randoid = "0.3.0"

Simple

```rust use randoid::{randoid, Generator};

// All of the below generate a string like "9wxwPU-kQU-RDjYdxj6Eq" let id = randoid(); let id = randoid!(); let id = Generator::default().gen(); ```

Custom length

```rust

use randoid::{randoid, Generator};

// The below generate a string like "MPlJcWfI" let id = randoid!(10); let id = Generator::with_size(10).gen(); ```

Custom alphabet

```rust use randoid::{randoid, Generator};

let id = randoid!(21, ['a', 'b', 'c', 'd']); let id = Generator::with_alphabet(&randoid::alphabet::HEX).gen(); ```

Custom random number generator

```rust use randoid::{randoid, Generator}; use rand::rngs::OsRng;

let id = randoid!(21, &randoid::alphabet::DEFAULT, OsRng); let id = Generator::with_random(OsRng).gen(); ```

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.