Skip to main content

Rust: Checked Token Transfer

Rust

Never let a balance underflow.

M
Mariam Toure Sample
Sample Sep 18, 2026
Rust · 5 lines
1
2
3
4
5
fn transfer(from: &mut u64, to: &mut u64, amount: u64) -> Result<(), &'static str> {
    *from = from.checked_sub(amount).ok_or("insufficient balance")?;
    *to = to.checked_add(amount).ok_or("overflow")?;
    Ok(())
}
4 Fire 2 Learned something 1 Saved me time 1 Mind-blown
Sign in to react and star