UUID in Rust
Rust UUID Generator
In Rust the UUID comes from the uuid crate, and the interesting part is the feature flags — the generators are opt-in.
uuid crateFeature-gatedno_std capable
Generated locally with the Web Crypto API — nothing leaves your browser.
Generating UUIDs in Rust
Uuid::new_v4()requires thev4feature, and the randomness comes fromgetrandom— so it is CSPRNG-backed rather than reproducible.Uuid::now_v7()requires thev7feature. Turning features off is a real benefit here: a parser that only ever reads existing identifiers can leave both generator features disabled and pull in no randomness at all.UuidisCopy, comparable andHash, so it works as a map key and in a database row struct without ceremony.Displaygives the hyphenated lowercase form, and the crate also exposes the byte array and theUuid::nil()constant for the all-zero value.
How to generate
- Press Generate UUID above to mint a value in this browser — nothing is sent anywhere.
- Copy one value with Copy, or set How many and use Copy all for a fixture list.
- Switch Version if your code needs v7 instead of the v4 default, and turn on Formatting to match your stack's conventions.
Use Cases
- Row identifiers in a sqlx or Diesel model, so the ID exists before the insert.
- Distributed nodes — no coordinator needed to hand out an identifier.
- Test fixtures — generate a set here and paste them into a test module.
How Rust UUID Generator Compares
| Uuid::new_v4() | Random, feature "v4", CSPRNG-backed |
| Uuid::now_v7() | Time-ordered, feature "v7" — best for primary keys |
| Uuid::new_v5() | Deterministic from a namespace and name, feature "v5" |
Code Examples
Cargo.toml
[dependencies]
uuid = { version = "1", features = ["v4", "v7", "serde"] }Rust
use uuid::Uuid;
fn main() {
let v4 = Uuid::new_v4(); // random
let v7 = Uuid::now_v7(); // time-ordered
println!("{v4}"); // 8f2c7b41-0d3e-4a55-b6c1-8e7d9f0a1b2c
println!("{v7}"); // 0198c2f1-6d8a-7cc3-9b21-4a7f0e5d3c11
let parsed: Uuid = "8f2c7b41-0d3e-4a55-b6c1-8e7d9f0a1b2c"
.parse()
.expect("valid uuid");
println!("{}", parsed.as_bytes().len()); // 16
}Frequently Asked Questions
Why is Uuid::new_v4() not found in Rust?
The generators are behind cargo features. Add
features = ["v4"] (or "v7") to the uuid dependency in Cargo.toml.Is the Rust uuid crate's v4 cryptographically random?
Yes — it draws from
getrandom, which uses the operating system entropy source. Use v5 instead when you want the same name to always produce the same value.How do I serialise a Uuid as a string with serde?
Enable the crate's
serde feature. Uuid then serialises as the canonical hyphenated string rather than as a byte array.Related tools
UUID v1 GeneratorUUID v3 GeneratorUUID v4 GeneratorUUID v5 GeneratorUUID v6 GeneratorUUID v7 GeneratorUUID v8 GeneratorBulk UUID GeneratorShort UUID GeneratorUUID ValidatorUUID FormatterJavaScript UUID GeneratorTypeScript UUID GeneratorNode.js UUID GeneratorReact UUID GeneratorPython UUID GeneratorJava UUID GeneratorC# UUID GeneratorGo UUID GeneratorPHP UUID GeneratorRuby UUID GeneratorRust UUID GeneratorKotlin UUID GeneratorSwift UUID GeneratorSQL UUID Generator