QR Code Generator in Rust: Creation and Integration into Web Applications

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: A Powerful Tool for Your Project



QR codes have become an integral part of the modern web: links, contacts, Wi-Fi passwords — all of this can be encoded in a square image. If you are developing an educational website or service in Rust, generating QR codes can be a useful feature. In this article, we will look at the qrcode library for Rust, learn how to create QR codes, customize their appearance, and integrate them into web applications.



Rust is a systems programming language valued for its performance and safety. The qrcode library leverages all the advantages of Rust: it is fast, requires no external dependencies, and is easily embedded into any project. Let's figure out how to generate QR codes using it.



Installation and Setup



To work with QR codes in Rust, we need the qrcode crate and, if we need to save images, the image crate. Add them to the Cargo.toml file:



[dependencies]qrcode = "0.14"image = "0.24"


After that, run cargo build to download the dependencies.



Basic Example: Generating a QR Code to the Console



The first example is the simplest: we will output the QR code directly to the terminal. This is convenient for debugging or when you don't need to save an image.



use qrcode::QrCode;use qrcode::render::unicode;

fn main() { // Create a QR code from a string let code = QrCode::new("https://pythonlib.ru").unwrap();

// Render to Unicode characters let image = code.render::<unicode::Dense1x2>() .dark_color(unicode::Dense1x2::Dark) .light_color(unicode::Dense1x2::Light) .build();

println!("{}", image);}


In this code:

  • QrCode::new() — creates a QR code from a string. If the string is too long, the method will return an error — use .unwrap() only for examples.
  • render::<unicode::Dense1x2> — selects the Unicode renderer. It outputs the code using pseudographic characters.
  • dark_color and light_color — set the characters for dark and light modules.


Run the program — a QR code will appear in the terminal that can be scanned with a phone.



Saving a QR Code as a PNG Image



For real projects, you need an image, not text. The qrcode library supports rendering to raster images via the image crate.



use qrcode::QrCode;use image::Luma;

fn main() { let code = QrCode::new("https://pythonlib.ru").unwrap();

// Render to an image (module size 8 pixels) let image = code.render::<Luma<u8>>() .min_dimensions(200, 200) // minimum size .dark_color(Luma([0u8])) // black .light_color(Luma([255u8])) // white .build();

// Save to file image.save("qrcode.png").unwrap(); println!("QR code saved to qrcode.png");}


Here:

  • Luma<u8> — pixel type for a black-and-white image.
  • min_dimensions(200, 200) — sets the minimum width and height. The QR code is automatically scaled.
  • dark_color and light_color — module colors (black and white).


After running the program, a file named qrcode.png will appear in the project root.



Customizing Color and Size



You can change the QR code colors using RGB values. For example, let's make a blue QR code on a yellow background:



use qrcode::QrCode;use image::Rgb;

fn main() { let code = QrCode::new("https://pythonlib.ru").unwrap();

let image = code.render::<Rgb<u8>>() .min_dimensions(300, 300) .dark_color(Rgb([0, 100, 200])) // blue

Recommendations