Working with UUIDs in Rust

I want to use UUID in my Rust app. I didn't find any mention in the Rust documentation. Is there a standard, de facto way to work with UUIDs in Rust?

+3


source to share


2 answers


There is an official UUID box at https://github.com/rust-lang/uuid .



+7


source


There is a built-in box for dealing with uuid in rust, check the documentation .

Example code that creates a new or parsing an existing uuid and gets many of its representations:

extern crate uuid;

use uuid::Uuid;

fn show_uuid(uuid: &Uuid) {
    println!("bytes: {}", uuid.as_bytes());
    println!("simple: {}", uuid.to_simple_str());
    println!("hyphenated: {}", uuid.to_hyphenated_str());
    println!("urn: {}", uuid.to_urn_str());
}

fn main() {

    // Generate a new UUID
    let uuid = Uuid::new_v4();
    show_uuid(&uuid);

    // Parse an existing UUID
    let uuid = Uuid::parse_string("95022733-f013-301a-0ada-abc18f151006").unwrap();
    show_uuid(&uuid);

}

      



EDIT:

The box was deprecated as inline and moved outside of Rust, so as Chris said, use the box at https://github.com/rust-lang/uuid (this is the same as was included in the rust distribution, so this example would be work).

Hope it helped.

+4


source







All Articles