uuid Library for Rust: A Complete Guide to Generating and Working with UUIDs
\n\n1. What is uuid and why is it needed?
\nThe uuid library for Rust is one of the most popular and stable crates for generating, parsing, and working with universally unique identifiers (UUIDs) in accordance with the RFC 4122 standard. A UUID (Universally Unique Identifier) is a 128-bit number that is guaranteed to be unique in space and time, making it ideal for use as primary keys in databases, session identifiers, event trackers, and any other scenarios where uniqueness is required without centralized management.
\nWhy is this library needed? In modern Rust development, there is often a need to generate unique identifiers that do not depend on the order of object creation, do not reveal information about the number of records (unlike auto-incrementing IDs), and can be safely transferred between different systems. The uuid crate provides a full set of tools for working with UUID versions 1, 3, 4, 5, and 7 (the latter being a time-based version added in recent updates). It supports serialization via serde, working with different representation formats (canonical, hyphen-free, URN), and integration with popular databases through corresponding features.
The library is actively maintained by the community, has excellent documentation, and is used in thousands of projects. It is written in pure Rust without external dependencies (except for optional features), ensuring memory safety and high performance. Whether you are writing a web server, a distributed system, a command-line tool, or any other Rust application that needs unique identifiers — uuid will become your indispensable assistant.
2. Installation
\nTo add the uuid library to your Rust project, run one of the following commands in the project's root directory:
\ncargo add uuid\nOr manually add the dependency to the Cargo.toml file:
[dependencies]\nuuid = { version = "1.10", features = ["v4", "v7", "serde"] }\nPay attention to the features section. By default, the library includes only basic capabilities. To work with specific UUID versions, you must explicitly specify the corresponding features:
- \n
v1— UUID version 1 (time-based and MAC address) \nv3— UUID version 3 (based on MD5 hash of name and namespace) \nv4— UUID version 4 (random) \nv5— UUID version 5 (based on SHA-1 hash of name and namespace) \nv7— UUID version 7 (time-based with millisecond precision, RFC 9562) \nserde— support for serialization/deserialization via the serde crate \nfast-rng— use of a fast random number generator (recommended for v4) \n
If you need all features, you can specify:
\n[dependencies]\nuuid = { version = "1.10", features = ["v1", "v3", "v4", "v5", "v7", "serde"] }\n3. Quick Start — Minimal Working Example
\nCreate a new project with the command cargo new uuid_example, add the dependency to Cargo.toml as shown above, then replace the contents of the src/main.rs file with the following code:
use uuid::Uuid;\n\nfn main() {\n // Generate a random UUID version 4\n let my_uuid = Uuid::new_v4();\n println!("Generated UUID v4: {}", my_uuid);\n \n // Parse a UUID from a string\n let parsed = Uuid::parse_str("550e8400-e29b-41d4-a716-446655440000")\n .expect("Failed to parse UUID");\n println!("Pa