nanoid (Rust)

A tiny, secure, URL-friendly, unique string ID generator for Rust

Library
Cargo
v0.5.0
618stars
MIT License

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum.How we score it →
52/100Fair
Development Activity60
Maintenance12
Community48
Maturity60
Momentum28

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation.How we score it →
69/100Good
Architecture65
Code Quality68
Innovation60
Learning Curve82

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::SAFE constant 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.

Join founders buildingwith open source

Opinionated takes, migration guides, cost-saving tips, and insights from the open source ecosystem.

Subscribe on Substack
Join 750+ subscribers

Search