A Rust implementation of ULIDs (Universally Unique Lexicographically Sortable Identifiers) with a focus on correctness and ease of use.
Generated ULIDs are guaranteed to be unique and strictly monotonically increasing, even across threads. The random component is overflow-proof by design -- see Overflow Protection for details.
[dependencies]
mr-ulid = "3"use mr_ulid::Ulid;
let u = Ulid::new();
println!("{u}");
let s = u.to_string();
let parsed: Ulid = s.parse().unwrap();
assert_eq!(u, parsed);- Overflow-proof generation -- At least 1010 ULIDs per millisecond without overflow or failure.
- Strict monotonicity -- Thread-safe generation; every ULID is greater than the previous one.
- Non-zero type (
Ulid) -- WrapsNonZero<u128>, soOption<Ulid>is the same size asUlid(16 bytes). - Zeroable type (
ZeroableUlid) -- For use cases that need a zero sentinel value. - Crockford Base32 -- Case-insensitive encoding with automatic
i/lto1andoto0disambiguation. - Optional
serdesupport -- Enable theserdefeature for string-based serialization. - Custom entropy sources -- Swap in your own RNG via the
EntropySourcetrait. - Minimal dependencies -- Only
rand(enabled by default). Disable withdefault-features = false.
Enable the serde feature for JSON (and other format) support:
[dependencies]
mr-ulid = { version = "3", features = ["serde"] }use mr_ulid::Ulid;
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize)]
struct Record {
id: Ulid,
data: String,
}ULIDs are serialized as 26-character Crockford Base32 strings.
The 80-bit random component is reduced by 1010 values (a ~0.000000000001% reduction in entropy). This reserves enough space to guarantee at least 1010 monotonically increasing ULIDs per millisecond -- equivalent to 1013 per second -- without overflow or failure. This exceeds the capability of current hardware by orders of magnitude.
See CHANGELOG.md for version history.