Uuid
A UUID (RFC 9562): 128 bits, written xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx.
Two kinds can be built here, from randomness and time you pass in (see
new_v4 and new_v7, with the random-entropy feature, to
take them from the system):
v4: 122 random bits, the general-purpose unique id;v7: a millisecond timestamp followed by random bits, so ids sort by creation time, which keeps database indexes compact.
Display writes the lowercase hyphenated form; parse reads it back (and the
32-digit form without hyphens, in any case).
Import
Section titled “Import”Cargo feature id (enabled by default). To compile only this module:
or in Cargo.toml:
Definition
Section titled “Definition”Examples
Section titled “Examples”Methods
Section titled “Methods”Reads a UUID from text: the hyphenated form 8-4-4-4-12 or 32 digits without hyphens,
upper or lower case. Braces and urn:uuid: prefixes are not accepted.
The version and variant bits are not checked: any 128-bit value is a valid UUID here.
Parameters
| Parameter | Type | Description |
|---|---|---|
text | &str | The text to read. |
Returns
Result<Self, ParseUuidError> — The UUID.
Errors
Returns a ParseUuidError for a length other than 36 or 32, a character that is not a
hexadecimal digit, or a hyphen that is not at its place.
Examples
new_v4
Section titled “new_v4”A random (version 4) UUID, with bytes from the operating system.
Needs the random-entropy feature.
Returns
Result<Self, crate::random::EntropyError> — The UUID.
Errors
Returns an EntropyError when the operating system cannot
provide random bytes.
Examples
new_v7
Section titled “new_v7”A time-sortable (version 7) UUID, with the current time from the system clock and random bytes from the operating system.
Needs the random-entropy feature. A clock set before 1970 gives a timestamp of 0.
Returns
Result<Self, crate::random::EntropyError> — The UUID.
Errors
Returns an EntropyError when the operating system cannot
provide random bytes.
Examples
Error type: ParseUuidError
Section titled “Error type: ParseUuidError”Why a string is not a Uuid.
