std-method
Integer checked_sub
Validate a withdrawal from an unsigned balance; an excessive amount is rejected. Expected stdout (not executed): Some(5) None Common mistake: Assuming an unsigned subtraction can produce a negative result.
Formal Syntax & Structure
rust syntaxrust
x.checked_sub(y) -> Option<Self>Working Example
Example Snippetrust
fn main() { let available = 12u8; println!("{:?}", available.checked_sub(7)); println!("{:?}", available.checked_sub(13));}