There are many cases when you might want to create a unique identifier. For example when we upload a video to YouTube the video gets an identifier that is unique to YouTube. For example w0VFcVYIfhg.
If you let people register on your web site you probably save their information in a database and assign a unique number to their information just as you do with any other piece of data, but you probably also let them select a username which needs to be unique in your system.
If you are inserting data into a relational database you probably have an id for each row. It is usually the next value in an automatically increasing sequence of whole numbers, 1,2,3. It is usually handled by a service provided by the database.
If you are inserting data into a distributed database, eg. MongoDB, then two pieces of data might be recorded on different servers. The database system cannot use a centralized place to increase a counter to assign the id. One solution would be to give each server a unique value that would be used as part of the ID. That way each server can has its own counter and the IDs that are a combination of the server-specific ID and the counter will be unique in the whole system. Then of course the question arises, how will a new server that joins this cluster of servers get its own unique id?
That's where UUIDs help.
A Universally Unique Identifier (UUID) is a 128-bit label that is going to be unique for practical purposes.
In Rust the uuid crate makes it easy to generate a regular uuid and the short_uuid crate to create a short one.
The README of the crate provides you with the basic usage. However I was surprised to see that there are many versions of UUID and this crate has all kinds of features.
For now, however I only looked at the basic usage as that was enough for me.
Dependencies
It is not enough to add the uuid crate as dependency, we also need to add one or more features to make the crate usable.
examples/generate-uuid/Cargo.toml
[package]
name = "create-uuid"
version = "0.1.0"
edition = "2024"
[dependencies]
short-uuid = "0.2.1"
uuid = { version = "1.20.0", features = ["v1", "v3", "v4", "v5", "v6"] }
The code
examples/generate-uuid/src/main.rs
use uuid::Uuid;
fn main() {
// node_id is unique to this process
let node_id = &[1, 2, 3, 4, 5, 6];
let id = Uuid::now_v1(node_id);
println!("uuid v1: {}", id);
let id = Uuid::new_v3(&Uuid::NAMESPACE_DNS, b"rust-lang.org");
println!("uuid v3: {}", id);
let id = Uuid::new_v4();
println!("uuid v4: {}", id);
let id = Uuid::new_v5(&Uuid::NAMESPACE_DNS, b"rust-lang.org");
println!("uuid v5: {}", id);
// let context = Context::new(random_seed());
// let ts = Timestamp::from_unix(context, 1497624119, 1234);
// let id = Uuid::new_v6(ts, node_id);
// println!("uuid v6: {}", id);
//
use short_uuid::ShortUuid;
let id = ShortUuid::generate();
println!("short_uuid: {}", id);
}
running the code
$ cargo run -q
uuid v1: 90517f42-001c-11f1-a58e-010203040506
uuid v3: c6db027c-615c-3b4d-959e-1a917747ca5a
uuid v4: 9bab0227-b3b5-486d-b9d1-396b9018b9e5
uuid v5: c66bbb60-d62e-5f17-a399-3a0bd237c503
short_uuid: 7TCeS59JQCnxS8X8fA3oVj
Conclusion
I did not have much to show here beyond what the README already shows, but I think I'll have a few more additions later as I implement email address validation for the Meet-OS project.