short-id
A tiny Rust library for generating short, URL-safe, unique identifiers.
What is this?
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.
This library has two main 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.
It is intentionally minimal - no configuration, no custom alphabets, no complex API. You just call:
short_id()for a random URL-safe IDshort_id_ordered()for a URL-safe ID that is roughly time-ordered
This crate is for you if you want something simpler and shorter than UUIDs, and you don't need strict UUID semantics or reversibility.
Quick Start
use short_id;
// Generate a random ID
let id = short_id;
println!;
// Example output: "X7K9mP2nQwE-Tg"
Installation
Add this to your Cargo.toml:
[]
= "0.3"
Usage
Random IDs
Perfect for request IDs, session tokens, or any unique identifier:
use short_id;
let request_id = short_id;
let session_id = short_id;
let token = short_id;
// Each ID is 14 characters, URL-safe, and unique
assert_eq!;
Time-Ordered IDs
For IDs that roughly sort by creation time:
use short_id_ordered;
let id1 = short_id_ordered;
sleep;
let id2 = short_id_ordered;
// IDs sort chronologically
assert!;
This is useful for:
- Log entries that should sort by time
- Event IDs in chronological order
- Resource IDs where temporal order matters
Convenience Macros
use ;
let random = id!; // Same as short_id()
let ordered = ordered_id!; // Same as short_id_ordered()
Typed Wrapper
use ShortId;
let id = random; // Returns ShortId newtype
let id = ordered; // Time-ordered variant
// Convert to/from String
let s: String = id.into;
let id: ShortId = s.into;
API Reference
Functions:
short_id() -> String- Generate a random IDshort_id_ordered() -> String- Generate a time-ordered ID (requiresstd)
Macros:
id!()- Shorthand forshort_id()ordered_id!()- Shorthand forshort_id_ordered()
Type:
ShortId- Newtype wrapper with methods:ShortId::random() -> SelfShortId::ordered() -> Self(requiresstd)as_str(&self) -> &strinto_string(self) -> String- Implements:
Display,AsRef<str>,From<String>,From<ShortId> for String
All IDs are:
- Exactly 14 characters
- URL-safe:
A-Z,a-z,0-9,-,_ - Cryptographically secure (using
OsRng) - Base64url encoded (no padding)
no_std Support
This crate works in no_std environments with alloc:
[]
= { = "0.3", = false }
Note: In no_std mode, only short_id() is available. The short_id_ordered() function requires the std feature because it needs std::time::SystemTime.
Contributing
Contributions are welcome! Please see CONTRIBUTING.md for guidelines.
For Maintainers
Releases are automated via GitHub Actions. See RELEASE.md for full details.
Quick release:
# Update Cargo.toml and CHANGELOG.md
# Tag triggers automatic publish to crates.io
Or use the release script:
License
MIT