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

How to generate

  1. Press Generate UUID above to mint a value in this browser — nothing is sent anywhere.
  2. Copy one value with Copy, or set How many and use Copy all for a fixture list.
  3. Switch Version if your code needs v7 instead of the v4 default, and turn on Formatting to match your stack's conventions.

Use Cases

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