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
-
Make it very easy to generate short random IDs for things like request IDs, user-facing tokens, test data, and log correlation.
-
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 (default)
- URL-safe: Only
A-Z,a-z,0-9,-,_(no special characters) - Cryptographically secure: Uses
OsRngfor random bytes - No configuration needed: Just call the function
§Advanced: Custom Length IDs
For advanced use cases, you can control the ID length by specifying the number of random bytes:
use short_id::{short_id_with_bytes, short_id_ordered_with_bytes};
// Generate a shorter 8-character ID (6 bytes)
let short = short_id_with_bytes(6);
assert_eq!(short.len(), 8);
// Generate a longer 22-character ID (16 bytes)
let long = short_id_with_bytes(16);
assert_eq!(long.len(), 22);
// Time-ordered IDs also support custom lengths
let ordered = short_id_ordered_with_bytes(12);When to use custom lengths:
-
Fewer bytes (e.g., 4-6): Use for low-volume applications where you need very short IDs and collision risk is acceptable. Keep in mind that 6 bytes provides only ~48 bits of entropy.
-
Default (10 bytes): Recommended for most applications. Provides ~80 bits of entropy with 14-character IDs. The
short_id()andshort_id_ordered()functions use this. -
More bytes (e.g., 16-32): Use for high-volume applications or when you need extra safety margin. 16 bytes provides ~128 bits of entropy.
Important: Using fewer bytes significantly increases collision probability. For most users,
the default short_id() and short_id_ordered() functions are recommended.
§Features
std(enabled by default): Enablesshort_id_ordered()andshort_id_ordered_with_bytes()which needstd::time::SystemTime
For no_std environments with alloc:
[dependencies]
short-id = { version = "0.4", default-features = false }In no_std mode, only short_id() and short_id_with_bytes() are available.
Macros§
- id
- Convenience macro for generating a random short ID.
- ordered_
id - Convenience macro for generating a time-ordered short ID.
Structs§
- ShortId
- A newtype wrapper around a short ID string.
Functions§
- short_
id - Generates a random, URL-safe short ID.
- short_
id_ ordered - Generates a time-ordered, URL-safe short ID.
- short_
id_ ordered_ with_ bytes - Advanced: Generates a time-ordered, URL-safe short ID with a custom number of bytes.
- short_
id_ with_ bytes - Advanced: Generates a random, URL-safe short ID with a custom number of bytes.