QR Code Generator in Rust: qr_code library

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

QR Code Generator in Rust: the qr_code library



QR codes have become an integral part of the modern web and mobile applications. They are used for quickly sharing links, contacts, payment data, and other information. If you are developing in Rust and want to add QR code generation to your project, the qr_code library is one of the best solutions. In this article, we will take a detailed look at how to install, configure, and use this library to create QR codes directly in Rust code.



What is the qr_code library?

qr_code is a pure Rust library for generating QR codes (version 2) with no external dependencies. It allows you to create QR codes from string data, configure the error correction level, module size, and color. The library supports output in SVG, PNG (via image), and ASCII art for the terminal. Key advantages:

  • High performance (Rust without GC).
  • Minimal memory consumption.
  • Support for all four error correction levels (L, M, Q, H).
  • Flexible customization of the QR code's appearance.


Installation

Add the dependency to your Cargo.toml:

[dependencies]qr_code = "0.2.0"

If you need PNG output, additionally install the image crate:

image = "0.24"

After that, run cargo build to download and compile the library.



Basic example: generating a QR code in SVG

The simplest way to get a QR code is to save it in the vector SVG format. Below is a minimal working example:

use qr_code::QrCode;

fn main() { // Create a QR code from a string let code = QrCode::new("https://pythonlib.ru").unwrap(); // Generate an SVG string with a module size of 8 pixels let svg = code.to_svg().with_width(8).to_string(); // Save to a file std::fs::write("qrcode.svg", svg).unwrap(); println!("QR code saved to qrcode.svg");}

After running, you will get a qrcode.svg file that can be opened in a browser or embedded in an HTML page.



Configuring the error correction level

The error correction level determines how much damage a QR code can sustain while remaining readable. The library supports four levels:

  • ErrorCorrection::Low — 7% of data can be restored.
  • ErrorCorrection::Medium — 15%.
  • ErrorCorrection::Quartile — 25%.
  • ErrorCorrection::High — 30%.

Usage example:

use qr_code::{QrCode, ErrorCorrection};

let code = QrCode::with_error_correction( "Secret code", ErrorCorrection::High).unwrap();

let svg = code.to_svg().with_width(10).to_string();std::fs::write("secret_qr.svg", svg).unwrap();


Generating a QR code in PNG (raster image)

For PNG output, you will need the image crate. First, add it to Cargo.toml, then use the following code:

use qr_code::QrCode;use image::{Luma, ImageBuffer};

fn main() { let code = QrCode::new("Rust is awesome!").unwrap(); // Create an image: each module = 10 pixels let size = code.width() * 10; let mut img = ImageBuffer::new(size as u32, size as u32); // Fill with white background for pixel in img.pixels_mut() { *pixel = Luma([255u8]); } // Draw black modules for y in 0..code.width() { for x in 0..code.width() { if code[(x, y)] { let px = x as u32 * 10; let py = y as u32 * 10; for dy in 0..10 { for dx in 0..10 { img.

Recommendations