Base64 Encode & Decode in Rust

Encode or decode below — it runs locally in your browser — then grab the canonical Rust code. Rust's standard library has no Base64, so you add the base64 crate and use its Engine API: general_purpose::STANDARD.encode(...) and .decode(...).

INPUT
OUTPUT
Type or paste to encode / decode

Encode and decode a string

Bring the Engine trait into scope, then call .encode() and .decode() on an engine:

use base64::{engine::general_purpose, Engine as _};

// Encode
let encoded = general_purpose::STANDARD.encode(b"Hello, world!");
println!("{}", encoded); // SGVsbG8sIHdvcmxkIQ==

// Decode
let bytes = general_purpose::STANDARD.decode(&encoded)?;
println!("{}", String::from_utf8(bytes)?); // Hello, world!

Tip: The old base64::encode() / base64::decode() free functions are deprecated and were removed in 0.21+. Use the Engine API — and remember use base64::Engine as _; so .encode resolves.

URL-safe Base64

For JWTs, URLs, and filenames, swap in the URL-safe alphabet (- and _ instead of + and /):

use base64::{engine::general_purpose, Engine as _};

let data = b"data for a URL parameter";

// URL-safe, with padding
let padded = general_purpose::URL_SAFE.encode(data);

// URL-safe, no padding (JWT-style — most common)
let encoded = general_purpose::URL_SAFE_NO_PAD.encode(data);
let decoded = general_purpose::URL_SAFE_NO_PAD.decode(&encoded)?;

Encode bytes / Cargo

Add the crate to Cargo.toml — every engine method takes any &[u8], so byte slices encode directly:

# Cargo.toml
[dependencies]
base64 = "0.22"
use base64::{engine::general_purpose, Engine as _};

let bytes: &[u8] = &[0xDE, 0xAD, 0xBE, 0xEF];
let encoded = general_purpose::STANDARD.encode(bytes);
println!("{}", encoded); // 3q2+7w==

Frequently asked questions

How do I Base64 encode a string in Rust?

Bring the Engine trait into scope and call general_purpose::STANDARD.encode(b"Hello, world!"), which returns "SGVsbG8sIHdvcmxkIQ==".

How do I Base64 decode in Rust?

general_purpose::STANDARD.decode(s)? returns a Vec<u8>; pass it to String::from_utf8(...) for text.

How do I do URL-safe Base64?

Use general_purpose::URL_SAFE, or general_purpose::URL_SAFE_NO_PAD to drop the = padding (JWT-style).

Why is base64::encode() deprecated in Rust?

The free functions were removed in 0.21+. Use the Engine API — general_purpose::STANDARD.encode(...) with use base64::Engine as _;.

Need image, file, or URL-safe modes?

The main base64.dev tool handles text, images, files, and URL-safe Base64 with auto-detect.

Open base64.dev →