DevToolsForYou

UUID Generator in Rust — Code Examples

UUID Generator in RustUse the online tool →

UUIDs (Universally Unique Identifiers) are 128-bit identifiers used as database primary keys, session tokens, and idempotency keys. UUID v4 (random) is the safe default; UUID v7 (time-ordered) is preferred for database primary keys.

Rust uses the uuid crate. Enable the v4 and v7 features in Cargo.toml for random and time-ordered UUIDs.

Rust
// Cargo.toml: uuid = { version = "1", features = ["v4", "v7"] }
use uuid::Uuid;

fn main() {
    // UUID v4 — random
    let id = Uuid::new_v4();
    println!("{}", id); // e.g. 550e8400-e29b-41d4-a716-446655440000

    // UUID v7 — time-ordered (preferred for DB primary keys)
    let id7 = Uuid::now_v7();
    println!("{}", id7);

    // Parse a UUID string
    let parsed = Uuid::parse_str("550e8400-e29b-41d4-a716-446655440000").unwrap();

    // String formatting variants
    println!("{}", parsed);              // hyphenated (default)
    println!("{}", parsed.simple());     // no dashes
    println!("{}", parsed.urn());        // urn:uuid:...
    println!("{:?}", parsed.as_bytes()); // raw [u8; 16]
}
Notes & gotchas
  • Enable features = ["v4"] or ["v7"] in Cargo.toml — UUID generation methods are behind feature flags.
  • UUID v7 is time-sortable and reduces index fragmentation in B-tree databases — prefer it over v4 for primary keys.
  • Uuid::parse_str returns a Result — use it to validate and normalise user-supplied UUID strings.
Try it in your browser

Need to uuid generator without writing code? The UUID Generator runs entirely in your browser — paste your input and get the result instantly. No signup, no install, no data sent to a server.

Open UUID Generator
UUID Generator in other languages