DevToolsForYou

Base64 Encoder and Decoder in Rust — Code Examples

Base64 Encoder and Decoder in RustUse the online tool →

Base64 encoding converts binary or text data into a 64-character ASCII alphabet so it can be safely transmitted over text-only channels. Here is how to encode and decode Base64 in each language using only the standard library.

Rust uses the base64 crate. Use general_purpose::STANDARD for standard Base64 and URL_SAFE_NO_PAD for JWT and URL contexts.

Rust
// Cargo.toml: base64 = "0.22"
use base64::{Engine as _, engine::general_purpose};

fn main() {
    let input = b"Hello, world!";

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

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

    // URL-safe without padding (JWT / URL contexts)
    let url_safe = general_purpose::URL_SAFE_NO_PAD.encode(input);
    println!("{}", url_safe);

    let decoded_url = general_purpose::URL_SAFE_NO_PAD.decode(&url_safe).unwrap();
    println!("{}", String::from_utf8(decoded_url).unwrap());
}
Notes & gotchas
  • Call Engine::encode / Engine::decode on the engine constant — the old free functions were removed in base64 0.21.
  • Use URL_SAFE_NO_PAD for JWTs and URL parameters; STANDARD includes + / characters and = padding.
  • decode() returns a Result — always handle the error; malformed input causes a panic if unwrapped blindly.
Try it in your browser

Need to base64 encode/decode without writing code? The Base64 Encoder and Decoder runs entirely in your browser — paste your input and get the result instantly. No signup, no install, no data sent to a server.

Open Base64 Encode/Decode
Base64 Encoder and Decoder in other languages