Generate UUID v4 and v7 in Rust
Copy-paste Rust code that produces a UUID v4 (random, the long-standing default) or UUID v7 (time-ordered, the modern choice for database keys). Both blocks below are production-ready.
UUID v4 in Rust
v4 is purely random — 122 bits of entropy, no structure. Use it when you don't want timing information embedded in the ID, or when you don't care about index locality.
// Cargo.toml:
// uuid = { version = "1", features = ["v4"] }
use uuid::Uuid;
fn main() {
let id = Uuid::new_v4();
println!("{}", id);
}
UUID v7 in Rust
v7 embeds a millisecond Unix timestamp in the first 48 bits, so the IDs sort chronologically. This is what modern databases want from a primary key — sequential inserts land in adjacent index pages instead of scattering writes.
// Cargo.toml:
// uuid = { version = "1", features = ["v7"] }
use uuid::Uuid;
fn main() {
let id = Uuid::now_v7();
println!("{}", id);
}
Library support
The `uuid` crate (v1.10+, May 2024) supports v7 behind the `v7` feature flag. Enable it in `Cargo.toml`. The crate is the standard choice for Rust UUID work.
Notes
If you need UUIDs in async contexts or want to avoid the OS RNG path on tight loops, look at `uuid::Builder` for explicit control. Most code paths don't need that.
Need them in bulk?
Our UUID generator produces 1 to 1000 UUIDs (v4 or v7) instantly, all in your browser, with one-click copy and JSON/CSV export. Useful for seeding test data or pre-generating IDs offline.