std-method

Integer checked_add

Reject an out-of-range total rather than silently changing a count. Expected stdout (not executed): Some(253) None Common mistake: Unwrapping an unchecked user-supplied total.

Formal Syntax & Structure

rust syntaxrust
x.checked_add(y) -> Option<Self>

Working Example

Example Snippetrust
fn main() {    let stock = 248u8;    println!("{:?}", stock.checked_add(5));    println!("{:?}", stock.checked_add(9));}
Rust Reference