How to Generate a UUID in Rust with the uuid Crate
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
use uuid::Uuid;
let <id> = Uuid::new_v4();
let <text> = <id>.to_string();
| Part | What it does |
|---|---|
use uuid::Uuid | Brings 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
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:

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
cargo run -q
cfccf210-d338-4c59-a9e8-0faa283c5f81
4
36
90d34760-a145-4d4e-89f3-5c2c0adb7d89

The command is the same in zsh, bash and PowerShell. Your UUIDs will differ.
What Happened
println!("{id}")printed the UUID as text, becauseUuidimplementsDisplay.get_version_num()returned4: 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
| Mistake | What happens | Do this instead |
|---|---|---|
Adding the crate without --features v4 | new_v4 doesn't exist, so the code doesn't compile | Run cargo add uuid --features v4 |
Storing a Uuid in a String field | The types don't match | Call .to_string() |
| Expecting UUIDs to sort by creation time | Version 4 UUIDs are random | Turn on the v7 feature and use Uuid::now_v7() |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
error[E0599]: no associated function or constant named `new_v4` found for struct `Uuid` in the current scope | The v4 feature isn't on | Run cargo add uuid --features v4 |
error[E0432]: unresolved import `uuid` | The crate isn't in Cargo.toml | Add 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
- Uuid and the crate's features, on docs.rs.
- RFC 9562 section 5.4, the UUID version 4 format.
