Base64 in Rust

How to encode and decode Base64 in Rust — with copy-paste examples and a live converter to check your output.

Rust has no Base64 support in std; the base64 crate is the de facto standard, exposing explicit Engine types for standard and URL-safe alphabets.

Encode to Base64 in Rust

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

let encoded = STANDARD.encode("Hello, World!");

Decode Base64 in Rust

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

let bytes = STANDARD.decode(encoded)?;
let text = String::from_utf8(bytes)?;

Notes & gotchas

Since base64 crate v0.21, encoding/decoding goes through an explicit Engine (STANDARD, URL_SAFE, URL_SAFE_NO_PAD, etc.) rather than free functions, which the older 0.13 API used.

Try it live

0 characters

Base64 in Rust — FAQ

How do I Base64 encode a string in Rust?+

Use the code shown above. Since base64 crate v0.21, encoding/decoding goes through an explicit Engine (STANDARD, URL_SAFE, URL_SAFE_NO_PAD, etc.) rather than free functions, which the older 0.13 API used.

How do I decode Base64 back to text in Rust?+

Use the decode snippet above. Base64 decoding is lossless, so you get the exact original bytes back; decode them with UTF-8 to recover text.

Is Base64 encoding the same across programming languages?+

Yes. Base64 is a standard (RFC 4648), so a string encoded in one language decodes correctly in any other. Only the API differs, not the output.

Does Base64 secure my data?+

No. Base64 is an encoding, not encryption — anyone can decode it. Never use it to protect secrets.