Generate QR Codes with Rust
Rust's qrcode crate provides fast, memory-safe QR code generation. Ideal for high-performance applications, WebAssembly, and system-level tools.
Installation
Add the qrcode crate to your Cargo.toml.
[dependencies]
qrcode = "0.14"
image = "0.25" # For PNG outputGenerate QR Codes with Rust
Code examples using the qrcode crate.
QR Code as SVG
use qrcode::QrCode;
use qrcode::render::svg;
fn main() {
let code = QrCode::new("https://qrcode.fun").unwrap();
let svg = code.render::<svg::Color>()
.min_dimensions(200, 200)
.build();
std::fs::write("qrcode.svg", &svg).unwrap();
println!("SVG QR code saved!");
}QR Code as PNG
use qrcode::QrCode;
use image::Luma;
fn main() {
let code = QrCode::new("https://qrcode.fun").unwrap();
let image = code.render::<Luma<u8>>()
.dark_color(Luma([26u8]))
.light_color(Luma([255u8]))
.quiet_zone(true)
.min_dimensions(300, 300)
.build();
image.save("qrcode.png").unwrap();
println!("PNG QR code saved!");
}Generate QR Codes via API in Rust
Call the QRCode.fun API from Rust using reqwest for styled QR codes.
use reqwest;
use serde_json::json;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = reqwest::Client::new();
let response = client
.post("https://qrcode.fun/api/generate-qr-styled")
.json(&json!({
"data": "https://qrcode.fun",
"width": 300,
"height": 300,
"type": "png",
"margin": 10,
"dotsOptions": { "color": "#1A2B3C", "type": "rounded" },
"cornersSquareOptions": { "color": "#8564C3", "type": "extra-rounded" },
"backgroundOptions": { "color": "#FFFFFF" }
}))
.send()
.await?;
let result: serde_json::Value = response.json().await?;
println!("{}", &result["data"].as_str().unwrap()[..50]);
Ok(())
}Live QR Code Preview
Try generating a QR code with Rust right now.
Native Library vs API
Compare using the qrcode crate versus the QRCode.fun API.
| Feature | qrcode Crate | QRCode.fun API |
|---|---|---|
| Setup complexity | cargo add + image crate for PNG | Single HTTP request via reqwest |
| Customization | Colors via image crate | Full styling: colors, shapes, logos |
| Offline support | Yes | Requires internet |
| Maintenance | cargo update | Always up-to-date |
| Output formats | SVG, PNG (with image crate), terminal | PNG, SVG |
Rust QR Code Use Cases
Common scenarios for QR codes in Rust applications.
WebAssembly
Compile QR generation to WASM for blazing-fast browser-side QR code creation without JavaScript libraries.
CLI Tools
Build command-line utilities that generate QR codes for terminal display, file output, or clipboard integration.
High-Performance Servers
Generate QR codes in Actix or Axum web servers with minimal memory allocation and maximum throughput.
Embedded Systems
Run QR code generation on resource-constrained devices where Rust's zero-cost abstractions shine.
Rust QR Code Ecosystem Deep Dive
Rust's zero-cost abstractions and memory safety make it uniquely suited for high-performance QR code generation and WebAssembly deployment.
WebAssembly (WASM) QR Generation
Rust compiles to WebAssembly for browser-side QR code generation with near-native performance. Use wasm-bindgen to expose a generate_qr function that returns SVG strings or pixel data directly to JavaScript. The compiled WASM module is typically 50-100KB — comparable to a JavaScript library but significantly faster for batch generation. Combine with wasm-pack for npm-publishable packages that work in any frontend framework.
Actix Web & Axum Server Integration
Build high-throughput QR code APIs with Rust's async web frameworks. Actix Web and Axum both handle QR generation in async handlers, serving PNG bytes with proper Content-Type headers. Rust's ownership model eliminates data races in concurrent QR generation. Use tower middleware for rate limiting and request validation. Benchmark with criterion to verify sub-millisecond generation times for standard QR codes.
Embedded Systems & no_std Environments
The qrcode crate supports no_std environments, enabling QR generation on microcontrollers and embedded devices without an operating system. Generate QR matrices on ESP32 or STM32 devices for IoT configuration, device pairing, and diagnostics. In resource-constrained environments, Rust's predictable memory usage (no garbage collector) ensures QR generation never causes unexpected memory spikes or latency.
Frequently Asked Questions
Common questions about generating QR codes with Rust.
Start generating QR codes with Rust
Use our free generator or integrate the API into your Rust applications.