Standard library · Randomness
random/uuid
Imported as import "random/uuid" as uuid, its names are then uuid.…. Every signature below is the one the checker infers.
UUIDs (RFC 9562): the random kind, version 4, and the time-ordered kind, version 7, whose first 48 bits are the millisecond it was made, so a column of them sorts by when. Both draw their random bits from the operating system, not from rnd's predictable stream.
A Uuid is two 64-bit halves, so it compares, hashes as a map key and prints like any value; text and bytes are its two spellings.
id = uuid.v7() uuid.version(id) # => 7 #uuid.text(id) # => 36 uuid.parse("550e8400-e29b-41d4-a716-446655440000") # => Some(550e8400-e29b-41d4-a716-446655440000) uuid.time_of(uuid.v7_at(1700000000000)) # => Some(1700000000000)
Types
Uuid
Uuid(hi: Int, lo: Int)
Implementations
impl Show(Uuid)
Functions
fn nil() -> Uuid
uuid.nil() # => 00000000-0000-0000-0000-000000000000
fn v4() -> Uuid
Version 4: 122 random bits.
uuid.version(uuid.v4()) # => 4 uuid.v4() == uuid.v4() # => false
fn v7() -> Uuid
Version 7: the time in milliseconds, then 74 random bits.
uuid.version(uuid.v7()) # => 7
fn v7_at(millis: Int) -> Uuid
uuid.time_of(uuid.v7_at(1700000000000)) # => Some(1700000000000)
fn version(u: Uuid) -> Int
uuid.version(uuid.parse("550e8400-e29b-41d4-a716-446655440000") |> unwrap_or(uuid.nil())) # => 4
fn time_of(u: Uuid) -> Option(Int)
The millisecond a version-7 id was made.
uuid.time_of(uuid.v4()) # => None
fn text(u: Uuid) -> Str
xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx, lowercase.
uuid.text(uuid.nil()) # => 00000000-0000-0000-0000-000000000000
fn bytes(u: Uuid) -> Str
The sixteen bytes, big-endian.
#uuid.bytes(uuid.v4()) # => 16
fn from_bytes(s: Str) -> Option(Uuid)
id = uuid.v4() uuid.from_bytes(uuid.bytes(id)) == Some(id) # => true uuid.from_bytes("short") # => None
fn parse(s: Str) -> Option(Uuid)
The text form, hyphens where they belong, either case; None for anything else.
uuid.parse("550E8400-E29B-41D4-A716-446655440000") # => Some(550e8400-e29b-41d4-a716-446655440000) uuid.parse("not an id") # => None
Tests
test_text_round_triptest_versions_and_varianttest_v7_sorts_by_time