Rust: Checked Token Transfer
RustNever let a balance underflow.
Rust · 5 lines
copied 39 times
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