base64 for Rust: complete guide with examples

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
\n

base64 library for Rust: complete guide

\n\n
\n

1. What it is and why it's needed

\n

Base64 is a scheme for encoding binary data into a text format based on 64 ASCII characters. It is widely used for transmitting data through channels that do not support binary formats, for example, in URLs, email (MIME), or when storing images in JSON. The base64 library for Rust provides fast and safe functions for encoding and decoding data in Base64 format, and also supports various variants of this format (e.g., URL-safe Base64).

\n

Why is it needed? In modern web development and systems programming, there is often a need to transmit binary data (images, files, encryption keys) through text protocols. Base64 solves this problem by converting bytes into a string that can be safely inserted into JSON, XML, or an HTTP header. The Rust version of the library is distinguished by high performance and strong typing, making it an ideal choice for systems where reliability and speed are important.

\n

The library supports several standards: standard Base64 (RFC 4648), URL-safe Base64 (with replacement of + and / characters with - and _), as well as variants with and without padding. This allows it to be used in a wide variety of scenarios — from simple string encoding to processing large files in streaming mode.

\n
\n\n
\n

2. Installation

\n

To install the base64 library in a Rust project, use the Cargo package manager. Add the dependency to the Cargo.toml file:

\n
[dependencies]\nbase64 = "0.22"
\n

Then run the command to download and build the dependencies:

\n
cargo build
\n

If you want to use the latest version from the GitHub repository, specify:

\n
[dependencies]\nbase64 = { git = "https://github.com/marshallpierce/rust-base64" }
\n

After adding the dependency, you can import the library into your code using use base64::*;.

\n
\n\n
\n

3. Quick start

\n

Minimal working example of encoding and decoding a string:

\n
use base64::{Engine as _, engine::general_purpose};\n\nfn main() {\n    let input = b"Hello, Rust!";\n    \n    // Encoding\n    let encoded = general_purpose::STANDARD.encode(input);\n    println!("Encoded: {}", encoded); // Output: SGVsbG8sIFJ1c3Qh\n    \n    // Decoding\n    let decoded = general_purpose::STANDARD.decode(&encoded).unwrap();\n    println!("Decoded: {}", String::from_utf8(decoded).unwrap()); // Output: Hello, Rust!\n}
\n

This example demonstrates basic operation: we encode a byte array into a Base64 string, and then decode it back. Note the import of the Engine trait — it is necessary for calling the encode and decode methods.

\n
\n\n
\n

4. Main methods and functions

\n

The library provides several ways to work with Base64. Below are the main methods, grouped by functionality.

\n\n

4.1. Engine::encode

\n

Signature: fn encode(&self, input: &[u8]) -> String

\n

Description: Encodes arbitrary bytes into a Base64 string. Uses the selected engine to determine the encoding format.

\n
use base64::{Engine as _, engine::general_purpose};\n\nlet data = b"Data to encode";\nlet encoded = general_purpose::STANDARD.encode(data);\nprintln!("{}", e                    

Recommendations