UUIDs (Universally Unique Identifiers) are a reliable means of uniquely identifying objects in distributed systems since they eliminate the need for centralized coordination. UUIDs enhance data integrity and promote interoperability between systems with their uniqueness and collision-resistant nature.

Rust is popular in fields where unique identifiers are essential, including networking, building web applications, and distributed systems; there are many crates for generating and customizing UUIDs with Rust, plus you can write Rust code to execute the uuid command on your machine and retrieve a UUID.

Generating UUIDs With the uuid Crate

The uuid crate is the most popular tool for generating UUIDs in Rust.

Add the uuid crate as one of your project’s dependencies in your Cargo.toml file:

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

Generating UUIDs with the package is simple. You can use the new_v4 function to generate a version four UUID:

        use uuid::Uuid;

fn main() {
    // new_v4 generates a version 4 UUID
    let my_uuid = Uuid::new_v4();
    println!("{}", my_uuid);

}

The main function generates a new UUID with the new_v4 function and prints the UUID to the console with the println! macro.

You can customize your UUID generation with the Builder and Version modules of the uuid crates.

Here’s how you can generate an RFC4122 UUID of a random version with the uuid crate:

        // Import the necessary modules from the uuid crate
use uuid::{Builder, Version};

fn main() {
    // Create a new Builder and initialize it with an array of 16 zero bytes
    let uuid_result = Builder::from_bytes([0; 16])
        // Set the UUID version to Random
        .set_version(Version::Random)
        // Set the UUID variant to RFC4122
        .set_variant(uuid::Variant::RFC4122)
        // Build the UUID
        .build();

    // Print the customized UUID in hyphenated format
    println!("Customized UUID: {}", uuid_result.to_hyphenated());
}

The main function generates the UUID with a new Builder instance created with the from_bytes function that takes an array of sixteen bytes as an argument (in this case, an array of zeros). The builder configures the UUID generation by setting the version to Random and the variant to RFC4122.

Finally, the main function builds the UUID with the build method call on the builder and prints the UUID to the console.

result from generating UUID

Generating UUIDs by Executing the UUID Command

You may not need third-party dependencies in Rust to generate UUIDs, especially if you don’t intend on customizing the UUID based on your use case. Most operating systems have a UUID generation tool installed that most applications call to generate UUIDs. You can write Rust code to execute the UUID command line tool and retrieve the UUID for your program.

You can use Rust’s built-in std::process::Command module to spawn and interact with new processes. To generate UUIDs with the Command module, you’ll need to identify the name of the UUID generation tool on your operating system. On macOS, the UUID generation tool is named uuigen.

Here’s how you can generate and retrieve UUIDs from your Rust code by executing the uuidgen command with the Command module:

        use std::process::Command;

fn generate_uuid() -> Result<String, std::io::Error> {
    let output = Command::new("uuidgen").output()?;
    let uuid = String::from_utf8_lossy(&output.stdout).into_owned();
    Ok(uuid)
}

fn main() {
    match generate_uuid() {
        Ok(uuid) => println!("Generated UUID: {}", uuid),
        Err(e) => eprintln!("Error generating UUID: {}", e),
    }
}

The generate_uuid function returns the string version of UUID and an error. The generate_uuid function spawns a new process with the new method of the Command module, retrieves the output with the output function, and converts the UUID to a string with the from_utf8_lossy function.

The main function calls the generate_uuid function with a match statement, handles the error, and outputs the UUID or an error message based on the status of the operation.

result from customizing UUID

You Can Build Sophisticated Web Applications With Rust

UUIDs are very important in modern-day software development. Most of your everyday applications use UUIDs, and UUID generation tools are installed in most operating systems, including Microsoft Windows, Linux, and macOS.

You can use UUIDs to identify users of your web applications. This is a great way to ensure users have a unique identifier that they can use to track their activity. Additionally, you can use UUIDs to identify other objects in your web app, from files to documents and products.