Rust · uuid crate · Code Reference
Generate a UUID in Rust
Complete reference for generating UUIDs in Rust — covers the uuid crate (the Rust standard), Cargo features, validation, and Actix/Diesel integration.
Quick Start
Generate a Random UUID (v4) with the uuid Crate
Rust’s standard library has no UUID type. The uuid crate is the de facto standard, with the required feature flags for random generation:
Cargo.toml
[dependencies]
uuid = { version = “1”, features = [“v4”] }
Rust
use uuid::Uuid;
fn main() {
let id = Uuid::new_v4();
println!(“{}”, id);
// 550e8400-e29b-41d4-a716-446655440000
}
💡
Feature flags matter
Unlike most languages, Rust’s uuid crate requires you to explicitly enable feature flags for each version you use — v4, v7, v5, etc. This keeps compiled binaries small by excluding unused generation code.
All Versions
UUID Versions with the uuid Crate
| Version | Rust Call | Cargo Feature |
|---|---|---|
| v1 (Time-based) | Uuid::now_v1(&node_id) | v1 |
| v3 (MD5 name-based) | Uuid::new_v3(&ns, name) | v3 |
| v4 (Random) | Uuid::new_v4() | v4 |
| v5 (SHA-1 name-based) | Uuid::new_v5(&ns, name) | v5 |
| v7 (Unix timestamp) | Uuid::now_v7() | v7 |
Cargo.toml — enable v7
[dependencies]
uuid = { version = “1”, features = [“v7”] }
Rust — UUID v7 for database keys
use uuid::Uuid;
let id = Uuid::now_v7();
println!(“{}”, id); // time-ordered, sortable UUID
Validation
Validate a UUID String in Rust
Rust
use uuid::Uuid;
fn is_valid_uuid(s: &str) -> bool {
Uuid::parse_str(s).is_ok()
}
is_valid_uuid(“550e8400-e29b-41d4-a716-446655440000”); // true
is_valid_uuid(“not-a-uuid”); // false
// Get parsed UUID and inspect version
let id = Uuid::parse_str(“550e8400-e29b-41d4-a716-446655440000”).unwrap();
println!(“{:?}”, id.get_version_num()); // 4
Frameworks
UUIDs in Actix, Diesel & Serde
Diesel model with UUID primary key
use uuid::Uuid;
use diesel::prelude::*;
#[derive(Queryable, Insertable)]
#[diesel(table_name = orders)]
pub struct Order {
pub id: Uuid,
pub created_at: chrono::NaiveDateTime,
}
Serde JSON serialization
[dependencies]
uuid = { version = “1”, features = [“v4”, “serde”] }
Rust struct
use serde::{Serialize, Deserialize};
use uuid::Uuid;
#[derive(Serialize, Deserialize)]
struct Response {
id: Uuid, // serializes to standard string format automatically
}
FAQ
Rust UUID FAQ
No. Rust’s standard library does not include UUID generation. The uuid crate on crates.io is the de facto standard, used across the Rust ecosystem including web frameworks like Actix and ORMs like Diesel and SeaORM.
The uuid crate uses Cargo feature flags (v1, v3, v4, v5, v7, serde, etc.) to keep compiled binaries small by excluding code for UUID versions and integrations you do not use. If you get a compile error saying new_v4 is not found, add features = [“v4”] to your Cargo.toml dependency line.
Add features = [“v7”] to the uuid dependency in Cargo.toml, then call Uuid::now_v7(). This produces a time-ordered UUID with a Unix millisecond timestamp prefix, recommended for database primary keys in 2026 for better B-tree index performance than random UUID v4 keys.
Yes, with the serde feature flag enabled (features = [“v4”, “serde”]). The Uuid type then implements Serialize and Deserialize automatically, converting to and from the standard hyphenated string format in JSON without any custom code in your structs.
Related Tools
Need to Generate a UUID Right Now?
Skip the code — use our free browser-based generator for all 8 UUID versions.
Open UUID Generator