Try it here
Generate a UUID to paste into your Rust code, in any version and format.
Your UUIDs
Copy one, copy them all, or download the list. The format options above apply to everything shown here.
The uuid crate
# Cargo.toml
[dependencies]
uuid = { version = "1", features = ["v4", "v7", "v5", "serde"] }
use uuid::Uuid;
let id = Uuid::new_v4(); // random
let id7 = Uuid::now_v7(); // time ordered, use for keys
let id5 = Uuid::new_v5(&Uuid::NAMESPACE_DNS, b"example.com"); // name based
id.to_string(); // "b076e3c8-fe3c-4475-bad6-bb9dfcc13c0c"
id.as_bytes(); // &[u8; 16]
Each version is behind a Cargo feature so the crate stays small. Forgetting the feature gives a "no function named new_v4" error, which is the first thing to check. Check the target configuration in the crate documentation. Browser WebAssembly needs the js feature. Embedded builds without the standard library need an appropriate source of random bytes.
Formats
id.hyphenated().to_string(); // b076e3c8-fe3c-4475-bad6-bb9dfcc13c0c default
id.simple().to_string(); // b076e3c8fe3c4475bad6bb9dfcc13c0c 32 hex
id.braced().to_string(); // {b076e3c8-fe3c-4475-bad6-bb9dfcc13c0c}
id.urn().to_string(); // urn:uuid:b076e3c8-fe3c-4475-bad6-bb9dfcc13c0c
format!("{:X}", id.simple()); // uppercase
Parse and validate
let parsed = Uuid::parse_str("b076e3c8-fe3c-4475-bad6-bb9dfcc13c0c")?;
let parsed: Uuid = "b076e3c8-fe3c-4475-bad6-bb9dfcc13c0c".parse()?;
parsed.get_version(); // Some(Version::Random)
parsed.get_version_num(); // 4
parsed.is_nil();
Uuid::nil(); // 00000000-0000-0000-0000-000000000000
Uuid::max(); // ffffffff-ffff-ffff-ffff-ffffffffffff
// read the timestamp back from a v7
if let Some(ts) = id7.get_timestamp() {
let (secs, nanos) = ts.to_unix();
}
parse_str accepts hyphenated, simple, braced and urn forms in either case. It rejects anything with the wrong length or non hex characters.
serde, sqlx and axum
#[derive(Serialize, Deserialize)]
struct Order {
id: Uuid, // serialises as the hyphenated string with the serde feature
}
// sqlx with the "uuid" feature maps Uuid to PostgreSQL uuid columns
let row = sqlx::query!("INSERT INTO orders (id) VALUES ($1) RETURNING id", Uuid::now_v7())
.fetch_one(&pool).await?;
// axum path extractor parses it for you
async fn get_order(Path(id): Path<Uuid>) { }
Mistakes to avoid
- Missing Cargo features. v4, v7 and serde are all opt in.
- Building ids from rand::random. The crate already uses a secure source and sets the version bits correctly.
- Storing to_string() in a bytea or BINARY(16) column. Use as_bytes() there, and to_string() only for text columns and APIs.
Frequently asked questions
Which Cargo features do I need for the uuid crate?
v4 for random, v7 for time ordered, v5 or v3 for name based, serde for serialisation. Add js when targeting WebAssembly in the browser, including current crate versions.
How do I generate a UUID v7 in Rust?
Uuid::now_v7() with the v7 feature enabled. Uuid::new_v7(Timestamp) lets you supply the time yourself.
Is Uuid::new_v4 cryptographically secure?
Yes. It uses the getrandom crate, which reads from the operating system's secure source on every supported platform.