[Awesome Rust] uuid Provides support for Universally Unique Identifiers (UUIDs)

uuid

uuid Provides support for Universally Unique Identifiers (UUIDs). A UUID is a unique 128-bit number, stored as 16 octets. UUIDs are used to assign unique identifiers to entities without requiring a central allocating authority.

Dependencies

By default, this crate depends on nothing but std and cannot generate UUIDs. You need to enable the following Cargo features to enable various pieces of functionality:

  • v1 - adds the Uuid::new_v1 function and the ability to create a V1 using an implementation of
    v1::ClockSequence (usually v1::Context) and a timestamp from time::timespec.

  • v3 - adds the Uuid::new_v3 function and the ability to create a V3 UUID based on the MD5 hash of some data.

  • v4 - adds the Uuid::new_v4 function and the ability to randomly generate a UUID.

  • v5 - adds the Uuid::new_v5 function and the ability to create a V5 UUID based on the SHA1 hash of some data.

  • serde - adds the ability to serialize and deserialize a UUID using the serde crate.

For WebAssembly, enable one of the following features depending on your JavaScript interop toolchain of choice:

  • stdweb - for stdweb combined with cargo-web

  • wasm-bindgen - for wasm-bindgen

By default, uuid can be depended on with:

1
2
3
4
# Cargo.toml

[dependencies]
uuid = "0.8"

To activate various features, use syntax like:

1
2
3
4
# Cargo.toml

[dependencies]
uuid = { version = "0.8", features = ["serde", "v4"] }

You can disable default features with:

1
2
3
4
# Cargo.toml

[dependencies]
uuid = { version = "0.8", default-features = false }

Examples

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
use uuid::Uuid;

fn main() -> Result<(), uuid::Error> {
// To create a new random (V4) UUID and print it out in hexadecimal form:
// Note that this requires the `v4` feature enabled in the uuid crate.
#[cfg(feature = "v4")] {
let my_uuid = Uuid::new_v4();
println!("{}", my_uuid);
}


// To parse a UUID given in the simple format and print it:
let my_uuid = Uuid::parse_str("936DA01F9ABD4d9d80C702AF85C822A8")?;

println!("simple = {}", my_uuid.to_simple());
// simple = 936da01f9abd4d9d80c702af85c822a8

println!("urm = {}", my_uuid.to_urn());
// urm = urn:uuid:936da01f-9abd-4d9d-80c7-02af85c822a8

println!("hyphenated = {}", my_uuid.to_hyphenated());
// 936da01f-9abd-4d9d-80c7-02af85c822a8

println!("bytes = {:?}", my_uuid.as_bytes());
// bytes = [147, 109, 160, 31, 154, 189, 77, 157, 128, 199, 2, 175, 133, 200, 34, 168]

Ok(())
}

References

[1] uuid-rs/uuid: Generate and parse UUIDs. - https://github.com/uuid-rs/uuid

[2] uuid - crates.io: Rust Package Registry - https://crates.io/crates/uuid