rust
27 lines · 7 steps
Base64 encode and decode in Rust
A thin wrapper over the base64 crate that exposes standard and URL-safe encoding through explicit Engine values.
Explained by
highlit
1use base64::engine::general_purpose::{STANDARD, URL_SAFE_NO_PAD};
2use base64::{DecodeError, Engine};
3
4pub fn encode_standard(data: &[u8]) -> String {
5 STANDARD.encode(data)
6}
7
8pub fn decode_standard(encoded: &str) -> Result<Vec<u8>, DecodeError> {
9 STANDARD.decode(encoded)
10}
11
12pub fn encode_url_token(data: &[u8]) -> String {
13 URL_SAFE_NO_PAD.encode(data)
14}
15
16pub fn decode_url_token(token: &str) -> Result<Vec<u8>, DecodeError> {
17 URL_SAFE_NO_PAD.decode(token)
18}
19
20pub fn roundtrip(payload: &[u8]) -> Result<Vec<u8>, DecodeError> {
21 let encoded = STANDARD.encode(payload);
22 STANDARD.decode(&encoded)
23}
24
25pub fn encode_into(data: &[u8], buf: &mut String) {
26 STANDARD.encode_string(data, buf);
27}
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1The base64 crate makes the alphabet and padding an explicit Engine choice rather than a hidden default.
- 2Decoding returns a Result because arbitrary input can be malformed, so callers must handle DecodeError.
- 3URL-safe-no-pad output is what you want for tokens that ride inside URLs or headers.
Related explainers
rust
pub fn format_size(bytes: u64) -> String { const UNITS: [&str; 7] = ["B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB"]; if bytes < 1024 {
Formatting byte counts as human-readable sizes
bit-manipulation
formatting
unit-conversion
Intermediate
5 steps
javascript
import { Component } from 'react'; import { reportError } from './services/telemetry'; export class ErrorBoundary extends Component {
How a React ErrorBoundary works
error-handling
lifecycle-methods
render-props
Intermediate
8 steps
rust
use std::collections::HashMap; #[derive(Debug)] pub struct RequestHead {
Parsing an HTTP request head in Rust
parsing
error-handling
iterators
Intermediate
9 steps
rust
#[derive(Debug, Default)] pub struct RequestBuilder { url: String, method: String,
The builder pattern in Rust
builder-pattern
method-chaining
ergonomic-api
Intermediate
8 steps
go
package humanize import ( "fmt"
Parsing human-readable byte sizes in Go
parsing
regex
lookup-table
Intermediate
8 steps
rust
use axum::{extract::{Path, State}, http::StatusCode, Json}; use dashmap::DashMap; use serde::Serialize; use std::sync::Arc;
Request coalescing in an Axum handler
caching
concurrency
request-coalescing
Advanced
8 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/base64-encode-and-decode-in-rust-explained-rust-af61/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.