nanoid (Rust)
A tiny, secure, URL-friendly, unique string ID generator for Rust
Repository Health
Technical Analysis
nanoid is the Rust port of the popular Nano ID JavaScript library, generating compact, cryptographically secure, URL-friendly unique IDs. It packs a similar number of unique combinations into 21 characters as a UUID, using a larger 64-symbol URL-safe alphabet (A-Za-z0-9_-) instead of hex digits.
The crate exposes a single nanoid!() macro with progressively more customizable forms: no-argument generation using safe defaults, a custom length, a custom alphabet, and a fully custom random-byte generator function for deterministic or seeded ID generation in tests.
What You Get
- A
nanoid!()macro producing a 21-character, URL-safe unique ID by default - Custom-length ID generation via
nanoid!(10) - Custom-alphabet support (up to 256 symbols) via
nanoid!(len, &alphabet) - Pluggable random-byte generator support for deterministic/seeded IDs in tests
- A pre-exposed
nanoid::alphabet::SAFEconstant matching the default URL-friendly symbol set
Common Use Cases
- Generating short, URL-safe unique identifiers for database records or public-facing resource URLs
- Producing collision-resistant IDs more compact than UUIDs for space-constrained contexts
- Deterministic/seeded ID generation for reproducible test fixtures
- Cross-language-consistent ID generation where a JS/Node frontend and Rust backend both use Nano ID
Under The Hood
Architecture: The crate is minimal by design — src/lib.rs (361 lines) defines the nanoid!() macro and its core generation logic, src/alphabet.rs (38 lines) defines the default URL-safe symbol set, and src/rngs.rs (68 lines) wraps the pluggable random-byte generation, including the interface for user-supplied Fn/FnMut closures used for seeded/deterministic generation.
Tech Stack: Pure Rust, 2018 edition, with rand 0.9 as its only required runtime dependency for the default cryptographically secure random source; an optional smartstring feature is available for small-string optimization. Dev-dependencies (criterion for benchmarking, doc-comment for doctest support) support the benches/rngs benchmark suite but aren’t part of the shipped library.
Code Quality: Tests are implemented as inline #[test] functions within src/lib.rs (11 found) rather than a separate tests/ integration directory, covering the macro’s various call forms (default, custom length, custom alphabet, custom RNG). The crate is small enough (467 total lines across all source files) that its surface area is easy to audit, and it exposes a benchmark suite (benches/rngs) to track RNG performance regressions.
API Design: The single nanoid!() macro with four progressively-parameterized call forms (no args, length, length+alphabet, length+alphabet+custom RNG) is deliberately minimal — a new user calls it with zero arguments for the common case, while advanced users needing deterministic or custom-alphabet IDs opt in incrementally without a different API surface. This mirrors the ergonomics of the original JavaScript Nano ID it ports from.
Used by 2 apps in this directory
AppFlowy
Productivity · Project Management · Collaboration
The open-source AI workspace that puts your data, your rules — with local LLMs, CRDT collaboration, and full self-hosting built in.
Arroyo
Data Engineering · Analytics
A distributed stream processing engine written in Rust that lets you write SQL to run stateful, real-time computations over data streams with subsecond results.