MOCKSTACKS
EN
Questions And Answers

More Tutorials




How to generate a random UUID in Rust?

To create a new random (V4) UUID and print it out in hexadecimal form we can use uuid create and function new_v4():

use uuid::Uuid;

fn main() {
    let my_uuid = Uuid::new_v4();
    println!("{}", my_uuid);
    Ok(())
}

Another way to generate a UUID

use uuid::Uuid;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    #[cfg(feature = "v4")] {
        let my_uuid = Uuid::new_v4()?;
        println!("{}", my_uuid);
    }
    Ok(())
}


Examples of string representations:

  • simple: 936DA01F9ABD4d9d80C702AF85C822A8
  • hyphenated: 550e8400-e29b-41d4-a716-446655440000
  • urn: urn:uuid:F9168C5E-CEB2-4faa-B6BF-329BF39FA1E4


Conclusion

In this page (written and validated by ) you learned about How to generate a random UUID in Rust? . What's Next? If you are interested in completing Rust tutorial, we encourage you simply to start here: Rust Tutorial.



Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.


Share On:


Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.