Developer Documentation
Rust
Updated September 19, 2026

Generate and Work with UUIDs in Rust

Generate, parse, format, and validate UUIDs safely in Rust.

UUIDs are 128-bit identifiers standardized by RFC 9562. UUIDv4 is random and broadly supported; newer UUID versions add other generation properties while keeping the same canonical textual shape.

Quick Start#

The following example uses the standard Rust API or a widely used package for this task.

Using the uuid crate#

Rust
1// Cargo.toml: uuid = { version = "1", features = ["v4"] }2use uuid::Uuid;34fn main() {5    let id = Uuid::new_v4();6    println!("{id}");7}

Common Operations#

This second pattern covers a common production operation or a safer way to handle real input.

Parse input#

Rust
1use uuid::Uuid;23fn parse_uuid(input: &str) -> Result<Uuid, uuid::Error> {4    Uuid::parse_str(input)5}

Important Notes#

Enable only the UUID crate features you need. Keep values as Uuid rather than strings inside typed application code.

Best Practices#

  1. Treat external input as untrusted and handle parse/validation errors explicitly.
  2. Keep encoding, units, formats, and library-specific mapping rules explicit at system boundaries.
  3. Add test vectors that cover empty input, Unicode text, malformed input, and representative production data.

References#