Skip to main content

Command Palette

Search for a command to run...

Building a QR Code Generator in Rust

Published
4 min read
Building a QR Code Generator in Rust
B
aspiring SDE

In this blog, we’ll walk through a simple QR Code Generator built using Rust. The application takes user input from the command line and generates a QR code in SVG format. This project is useful for understanding how Rust handles external crates, user input, file generation, and structured program flow.

Repository:
https://github.com/Bhavadharani412/Rust-QR-code-generator


What the Application Does

  • Accepts text or a URL from the user

  • Generates a QR code from the input

  • Saves the QR code as an SVG file

  • Produces an output that can be scanned by any QR scanner

This is a command-line application, so it runs locally and does not require any UI or browser setup.


Project Structure

Rust-QR-code-generator/
├── src/
│   └── main.rs
├── Cargo.toml
└── qr.svg
  • Cargo.toml manages dependencies and project metadata

  • main.rs contains the application logic

  • qr.svg is the generated output file

This minimal structure keeps the project easy to understand and extend.


Dependencies Used

The project uses the qrcode crate.

This crate handles:

  • QR encoding logic

  • Error correction

  • Rendering the QR matrix into different formats such as SVG

Using a well-maintained crate allows us to focus on application logic instead of QR algorithms.


Code Walkthrough

Importing Required Modules

The program begins by importing standard Rust modules and the QR code library.

  • std::io is used to read user input

  • std::fs is used to write the SVG file

  • qrcode::QrCode generates the QR data

  • qrcode::render::svg converts the QR matrix into SVG

Each import has a clear purpose and avoids unnecessary dependencies.


Reading User Input

The program prompts the user to enter text or a URL. Input is read from standard input and stored in a mutable string.

Rust requires explicit mutability, which prevents accidental data modification and keeps input handling safe and predictable.


Generating the QR Code

The input string is passed to QrCode::new().

Internally, this function:

  • Encodes the input into binary data

  • Applies QR error correction rules

  • Constructs a two-dimensional QR matrix

If the input is invalid, the function returns an error. Rust enforces handling this explicitly, avoiding silent failures.


Rendering the QR Code as SVG

Once the QR matrix is generated, it is rendered into SVG format.

SVG is chosen because:

  • It scales without losing quality

  • It produces sharp edges required for accurate scanning

  • It works across browsers and design tools

The renderer converts each QR module into SVG path elements.


Writing the Output File

The generated SVG string is written to a file named qr.svg.

Rust’s file handling ensures:

  • The file is created safely

  • Errors are handled explicitly

  • The program does not proceed if writing fails

This makes the output reliable and predictable.


Running the Application

To run the project:

cargo run

After entering input, the program generates qr.svg in the project directory. Open the file in a browser or image viewer and scan it using a phone.


Why This Project Matters

Although small, this project demonstrates several important Rust concepts:

  • Using external crates correctly

  • Safe input handling

  • Structured file output

  • Error handling without runtime crashes

  • Producing real-world usable output

It is a good foundation project for developers transitioning into Rust or building command-line tools.


Possible Improvements

This project can be extended in multiple ways:

  • Accept command-line arguments instead of interactive input

  • Allow custom file names and sizes

  • Support PNG output using the image crate

  • Integrate into a web backend to generate QR codes dynamically

Each improvement builds naturally on the existing structure.


Conclusion

This Rust QR Code Generator is a clean, practical project that focuses on fundamentals. It avoids unnecessary complexity while still producing a useful result. Projects like this help build confidence in Rust and prepare you for larger systems where safety and correctness matter.

If you understand this code, you are well on your way to writing reliable Rust applications.