Working with UUIDs in Rust
2 answers
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 to share