Generating UUIDs with the `uuid` Crate in Rust

UUID (Universally Unique Identifier) is a 128-bit number used to uniquely identify resources in distributed systems. They are quite popular due to their uniqueness, which helps avoid collisions in distributed systems and databases. In this article, we will explore how to generate UUIDs using the uuid crate in Rust.
Prerequisites
Section titled “Prerequisites”Before we begin, ensure you have the following installed:
- Rust programming language (Installation instructions)
- Cargo, the package manager for Rust, which is included when installing Rust.
Setting Up the Project
Section titled “Setting Up the Project”Create a new Rust project using Cargo:
cargo new uuid_examplecd uuid_exampleThis will create a new Rust project in a directory called uuid_example.
Adding the uuid Crate Dependency
Section titled “Adding the uuid Crate Dependency”Now, let’s add the uuid crate as a dependency. Open the Cargo.toml file and add the following line under the [dependencies] section:
uuid = "0.8"This will include the uuid crate version 0.8.x in your project. Save the file and return to your terminal.
Generating UUIDs
Section titled “Generating UUIDs”Let’s create a simple example to demonstrate how to generate UUIDs using the uuid crate. Open the src/main.rs file and replace its content with the following code:
// Import the required modulesuse uuid::Uuid;
fn main() { // Generate a random UUID let random_uuid = Uuid::new_v4(); println!("Random UUID: {}", random_uuid);
// Generate a UUID based on a name and a namespace let namespace = Uuid::new_v5(&Uuid::NAMESPACE_DNS, "example.com".as_bytes()); println!("Namespace-based UUID: {}", namespace);}Here, we’re using the Uuid struct from the uuid crate. We generate a random UUID using the new_v4 method, which creates a version 4 UUID. A version 4 UUID is randomly generated and has 122 bits of randomness. We also generate a namespace-based UUID using the new_v5 method. This method creates a version 5 UUID, which is based on the SHA-1 hash of a namespace and a name. In our example, we use the DNS namespace and the name “example.com”.
Now, run the program using Cargo:
cargo runYou should see output similar to the following:
Random UUID: 3e3c5e5c-8c7d-4b29-aa03-6f1b6e1d6f53Namespace-based UUID: 9073920b-4b6c-5d1c-8918-74fb0e2cc2c2Please note that your output will be different since UUIDs are unique.
Conclusion
Section titled “Conclusion”In this article, we learned how to generate UUIDs using the uuid crate in Rust. To recap, we:
- Set up a new Rust project using Cargo.
- Added the
uuidcrate as a dependency. - Generated random and namespace-based UUIDs using the
Uuidstruct and its methods.
The uuid crate provides a simple and efficient way to generate UUIDs in Rust applications. It offers various options to generate UUIDs based on your requirements and ensures uniqueness across distributed systems.
© 2026 David Li · Home · Blog · RSS ·Classic site · Built with Astro, Starlight and three.js