Skip to main content

How to Generate a UUID in Rust with the uuid Crate

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

Rust's standard library has no UUIDs, so the usual way is the uuid crate: Uuid::new_v4() makes a random UUID. The function only exists when the crate's v4 feature is on, which cargo add uuid --features v4 takes care of.

Syntax​

Syntax
use uuid::Uuid;

let <id> = Uuid::new_v4();
let <text> = <id>.to_string();
PartWhat it does
use uuid::UuidBrings the Uuid type into scope.
Uuid::new_v4()Makes a new random version 4 UUID. Needs the v4 feature.
to_string()Returns the UUID as text in the usual form with dashes.

The uuid crate's documentation for new_v4: "Creates a random UUID. This uses the getrandom crate to utilise the operating system's RNG as the source of random numbers." Its list of features says: "v4 - Version 4 UUIDs with random data."

The Example​

The example makes a UUID and prints it, its version and the length of its text, then makes a second one.

Prerequisites​

  • Rust, with Cargo. This guide uses Rust 1.100.1.

Step 1: Create the Project​

Terminal
cargo new demo
cd demo
cargo add uuid --features v4

cargo add uuid --features v4 adds the crate, version 1.29.3 here, with the v4 feature turned on.

Step 2: Write the Code​

Replace the contents of src/main.rs:

src/main.rs, which uses uuid::Uuid, stores Uuid::new_v4() in id, prints it, its get_version_num() and the length of its text, then prints a second Uuid::new_v4().

src/main.rs
use uuid::Uuid;

fn main() {
let id = Uuid::new_v4();
println!("{id}");
println!("{}", id.get_version_num());
println!("{}", id.to_string().len());
println!("{}", Uuid::new_v4());
}

Step 3: Run It​

Terminal
cargo run -q
Output
cfccf210-d338-4c59-a9e8-0faa283c5f81
4
36
90d34760-a145-4d4e-89f3-5c2c0adb7d89

A zsh terminal where cargo run prints a UUID, the version 4, the length 36 and a second, different UUID.

The command is the same in zsh, bash and PowerShell. Your UUIDs will differ.

What Happened​

  • println!("{id}") printed the UUID as text, because Uuid implements Display.
  • get_version_num() returned 4: the documentation says it "Returns the version number of the UUID. This represents the algorithm used to generate the value."
  • The text is 36 characters long, and the second UUID is unrelated to the first.

Where a String is needed, for example the jti field of a JWT claims struct, call to_string().

Common Mistakes​

MistakeWhat happensDo this instead
Adding the crate without --features v4new_v4 doesn't exist, so the code doesn't compileRun cargo add uuid --features v4
Storing a Uuid in a String fieldThe types don't matchCall .to_string()
Expecting UUIDs to sort by creation timeVersion 4 UUIDs are randomTurn on the v7 feature and use Uuid::now_v7()

Troubleshooting​

SymptomLikely causeFix
error[E0599]: no associated function or constant named `new_v4` found for struct `Uuid` in the current scopeThe v4 feature isn't onRun cargo add uuid --features v4
error[E0432]: unresolved import `uuid` The crate isn't in Cargo.tomlAdd it with cargo add

FAQ​

Why does the uuid crate need features?​

So programs only build the kinds of UUID they use. The crate has a feature for each kind it can make, such as v4 for random UUIDs and v7 for time-ordered ones.

Is new_v4 random enough for IDs that must not be guessed?​

Yes. It takes its random numbers from the operating system's random number generator, through the getrandom crate.

Conclusion​

Add the uuid crate with its v4 feature, and Uuid::new_v4() gives you a random UUID; call to_string() when you need it as text.

Additional Resources​