UUIDs in 10 Languages — Part 2: Rust, Go, and C#
UUIDs are everywhere — and every programming language has its own take on generating, parsing, and working with them.
In this second part of the “UUIDs in 10 Languages” series, we’ll explore how to use UUIDs effectively in Rust, Go, and C#.
🦀 UUIDs in Rust
Rust’s ecosystem provides the uuid
crate — fast, full-featured, and RFC-compliant.
🔧 Installation
Add this to your Cargo.toml
:
[dependencies]
uuid = { version = "1", features = ["v4"] }
> You can also enable v1
, v5
, or serde
features as needed.
📦 Generate a UUIDv4 (Random)
use uuid::Uuid;
fn main() {
let id = Uuid::new_v4();
println!("Generated UUIDv4: {}", id);
}
🧪 UUIDv1 (Time-based) — with `getrandom` + `mac_address`
[dependencies]
uuid = { version = "1", features = ["v1"] }
use uuid::{Uuid, Timestamp};
use std::time::{SystemTime, UNIX_EPOCH};
fn main() {
let ts = SystemTime::now().duration_since(UNIX_EPOCH).unwrap();
let ts = Timestamp::from_unix(&uuid::NoContext, ts.as_secs(), ts.subsec_nanos());
let id = Uuid::new_v1(ts, &[1, 2, 3, 4, 5, 6]).unwrap();
println!("UUIDv1: {}", id);
}
> For production use, derive the MAC automatically using mac_address
crate.
🧠 Best Practices
- Use
Uuid::new_v4()
for general use - Use
Uuid::new_v5()
if you need deterministic identifiers - Enable only the features you need to reduce binary size
- Serialize with
serde
usinguuid
's built-in support
🐹 UUIDs in Go
Go doesn’t include UUID generation in the standard library — but the community provides excellent packages.
🔧 Install Google’s UUID Package
go get github.com/google/uuid
📦 Generate UUIDv4
package main
import (
"fmt"
"github.com/google/uuid"
)
func main() {
id := uuid.New()
fmt.Println("UUIDv4:", id.String())
}
🧪 UUIDv5 (Namespace + Name)
package main
import (
"fmt"
"github.com/google/uuid"
)
func main() {
ns := uuid.MustParse("6ba7b810-9dad-11d1-80b4-00c04fd430c8") // DNS namespace
id := uuid.NewMD5(ns, []byte("example.com"))
fmt.Println("UUIDv3:", id)
id5 := uuid.NewSHA1(ns, []byte("example.com"))
fmt.Println("UUIDv5:", id5)
}
🧠 Best Practices
- Use
uuid.New()
for UUIDv4 — it’s the Go-to choice (pun intended) - Use v3/v5 for deterministic IDs (e.g., from strings or paths)
- Don’t roll your own UUIDs using
math/rand
— it’s not secure
👩💻 UUIDs in C#
C# includes UUIDs (called Guid
) in its standard library.
📦 Generate a GUID
using System;
class Program
{
static void Main()
{
Guid id = Guid.NewGuid();
Console.WriteLine("GUID (UUIDv4): " + id.ToString());
}
}
🎯 Parse and Validate
string str = "3f2504e0-4f89-11d3-9a0c-0305e82c3301";
Guid guid;
if (Guid.TryParse(str, out guid))
{
Console.WriteLine("Valid GUID: " + guid);
}
else
{
Console.WriteLine("Invalid UUID");
}
🔎 Format Variants
Guid id = Guid.NewGuid();
Console.WriteLine(id.ToString("D")); // Default: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
Console.WriteLine(id.ToString("N")); // No hyphens
Console.WriteLine(id.ToString("B")); // Braced
Console.WriteLine(id.ToString("P")); // Parenthesized
🧠 Best Practices
- Use
Guid.NewGuid()
— it generates a cryptographically strong UUIDv4 - Avoid exposing
Guid
in URLs unless formatted safely (useToString("N")
) - Consider sorting or indexing implications — GUIDs are not ordered by default
🧪 Performance and Interop Tips
Language | UUID Type | Notes |
---|---|---|
Rust | uuid::Uuid | Strong type, supports many formats |
Go | uuid.UUID | Simple struct, implements String |
C# | System.Guid | Rich formatting + built-in parsing |
Interop Suggestions
- Use UUIDv4 across services for maximum compatibility
- Encode UUIDs in canonical form (
lowercase-hyphenated
) to avoid mismatches - Serialize consistently across languages (e.g., using JSON with string UUIDs)
🧭 When to Use Each Version
Use Case | Best UUID Version |
---|---|
Random ID for API or DB | UUIDv4 |
Name-based deterministic | UUIDv5 |
Ordered UUIDs | UUIDv7 (not widely supported yet) |
Custom formats | UUIDv8 or alternatives (ULID, KSUID) |
Final Thoughts
Rust, Go, and C# each provide solid UUID support — whether built-in or via trusted libraries. If you're building distributed systems, APIs, or databases, knowing how to properly use UUIDs in your language of choice is a must.
Pick the right version, serialize consistently, and embrace UUIDs for what they are: simple, scalable, and uniquely universal.
Stay tuned for Part 3!