uuid
UUID generation, parsing, and validation. All UUIDs cross the language
boundary as String in the canonical lowercase hyphenated form:
"550e8400-e29b-41d4-a716-446655440000" (8-4-4-4-12, 36 characters).
Two generators are provided:
uuid.v4— fully random UUIDs (RFC 9562 version 4). Random bits come from the OS CSPRNG via thegetrandomcrate (the same source that backscrypto.random_bytes).uuid.v7— time-ordered UUIDs (RFC 9562 version 7, 2022 spec). The first 48 bits encode a Unix millisecond timestamp, the remainder is random. Lexicographic string comparison on two v7 UUIDs minted in order tracks generation time — ideal for B-tree-friendly primary keys where monotonic inserts avoid page splits, while still being unguessable.
Use uuid.parse when you need to validate and canonicalize a UUID
string at trust boundaries (e.g. parsing an HTTP path param). It
accepts any form the underlying parser understands (hyphenated,
simple/32-char, braced, urn-prefixed) and canonicalizes the output.
Use uuid.is_valid when you only need the boolean — no Result
allocation.
Summary
| Function | Signature | Description |
|---|---|---|
v4 | () -> String | Random UUID (version 4, CSPRNG-backed) |
v7 | () -> String | Time-ordered UUID (version 7, RFC 9562) |
parse | (String) -> Result(String, UuidError) | Validate + canonicalize any-version UUID |
nil | () -> String | The all-zero UUID sentinel |
is_valid | (String) -> Bool | Predicate form of parse |
uuid.v4
uuid.v4() -> String
A random (version 4) UUID from the operating system’s secure random number generator, in the lowercase hyphenated form.
import string
import uuid
fn main() {
let id = uuid.v4()
println(id) -- e.g. 550e8400-e29b-41d4-a716-446655440000
println(string.slice(id, 14, 15)) -- 4
}open in playgrounduuid.v7
uuid.v7() -> String
A version 7 UUID (RFC 9562): the current time in milliseconds, then random bits. A value made in a later millisecond sorts after an earlier one, as text too, which suits a key that is inserted in order.
import string
import uuid
fn main() {
let id = uuid.v7()
println(id) -- e.g. 019a7d3c-5b2e-7c41-9f0a-3d2b8e6f1a47
println(string.slice(id, 14, 15)) -- 7
}open in playgrounduuid.parse
uuid.parse(s: String) -> Result(String, UuidError)
Checks s and gives it in the canonical form: lowercase, hyphenated. Any version is accepted, and the nil UUID; upper case, braces and the urn:uuid: prefix are read. Other text is Err(Invalid(why)).
import uuid
fn main() {
println(uuid.parse("urn:uuid:550E8400-E29B-41D4-A716-446655440000")) -- Ok("550e8400-e29b-41d4-a716-446655440000")
match uuid.parse("not-a-uuid") {
Ok(id) -> println(id)
Err(e) -> println("{e}") -- invalid uuid: invalid character: found `n` at 1
}
}open in playgrounduuid.nil
uuid.nil() -> String
The nil UUID, all zeros.
import uuid
fn main() {
println(uuid.nil()) -- 00000000-0000-0000-0000-000000000000
}open in playgrounduuid.is_valid
uuid.is_valid(s: String) -> Bool
Whether uuid.parse would accept s.
import uuid
fn main() {
println(uuid.is_valid("550e8400-e29b-41d4-a716-446655440000")) -- true
println(uuid.is_valid("550e8400")) -- false
}open in playgroundExamples
import uuid
fn main() {
-- Random UUID
let id = uuid.v4()
println(id)
-- e.g. 550e8400-e29b-41d4-a716-446655440000
-- Time-ordered UUID (good for DB primary keys)
let pk = uuid.v7()
println(pk)
-- Two v7s sort by generation time
let _a = uuid.v7()
let _b = uuid.v7()
-- _a < _b when compared as strings
-- Validate + canonicalize external input
match uuid.parse("550E8400-E29B-41D4-A716-446655440000") {
Ok(canonical) -> println(canonical)
-- 550e8400-e29b-41d4-a716-446655440000
Err(e) -> println(e)
}
-- Predicate form, no Result allocation
match uuid.is_valid("not-a-uuid") {
true -> println("valid")
false -> println("invalid")
}
-- Sentinel
println(uuid.nil())
-- 00000000-0000-0000-0000-000000000000
}open in playgroundErrors
Only parse is fallible at the type level; the generators and nil
are total. It returns Result(String, UuidError):
| Variant | Fields | Meaning |
|---|---|---|
Invalid(String) | why | the input is not a syntactically valid UUID; "{e}" is "invalid uuid: ..." |
Notes
- Output is always the 36-character lowercase hyphenated form. If you
need a different shape (braced, simple/32-char, uppercase), transform
the returned string with
stringhelpers — keeping one canonical wire format here avoids a combinatorial API surface. uuid.parseis version-agnostic: it accepts v1 through v8 UUIDs and the nil UUID. If you need to reject particular versions, match on the relevant substring of the returned canonical form.uuid.v4anduuid.v7collide with probability ~2^-122 and ~2^-74-per-ms respectively — treat collisions as impossible in practice, not as a case to handle at call sites.- Prefer
uuid.v7overuuid.v4for database primary keys: monotonic inserts keep B-tree pages hot and avoid the random-write amplification that v4 causes on sorted indexes.