Skip to main content

Rust: Checked Token Transfer

Rust

Never let a balance underflow.

M
Mariam Toure
Sample Sep 17, 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(())
}
3 Fire 2 Learned something 0 Saved me time 0 Mind-blown
Sign in to react and star