Crate short_id

Crate short_id 

Source
Expand description

A tiny crate for generating short, URL-safe, unique identifiers.

Unlike full UUIDs (which are 36 characters and include hyphens), short-id gives you compact 14-character strings that are easy to copy, paste, and use in URLs.

§Goals

  1. Make it very easy to generate short random IDs for things like request IDs, user-facing tokens, test data, and log correlation.

  2. Provide an optional “ordered” variant where IDs include a timestamp prefix, so when you sort them as strings they roughly follow creation time.

This crate is intentionally minimal - no configuration, no custom alphabets, no complex API.

§Quick Start

use short_id::short_id;

// Generate a random ID
let id = short_id();
println!("Request ID: {}", id);
// Example output: "X7K9mP2nQwE-Tg"

For time-ordered IDs:

use short_id::short_id_ordered;

let id1 = short_id_ordered();
std::thread::sleep(std::time::Duration::from_millis(100));
let id2 = short_id_ordered();

// IDs from different times are different
assert_ne!(id1, id2);

§Use Cases

  • Request IDs for logging and tracing
  • User-facing tokens and session IDs
  • Test data generation
  • Short URLs and resource identifiers
  • Any place you want something shorter and simpler than UUIDs

§Characteristics

  • Length: Always exactly 14 characters
  • URL-safe: Only A-Z, a-z, 0-9, -, _ (no special characters)
  • Cryptographically secure: Uses OsRng for random bytes
  • No configuration needed: Just call the function

§Features

For no_std environments with alloc:

[dependencies]
short-id = { version = "0.1", default-features = false }

In no_std mode, only short_id() is available.

Functions§

short_id
Generates a random, URL-safe short ID.
short_id_ordered
Generates a time-ordered, URL-safe short ID.