std-method

Unsigned checked_add_signed

Adjust an unsigned quantity by a signed delta without silently wrapping. Expected stdout (not executed): Some(7) None Common mistake: Casting the negative delta to u32 before addition.

Formal Syntax & Structure

rust syntaxrust
n.checked_add_signed(delta: i32) -> Option<u32>

Working Example

Example Snippetrust
fn main() {    println!("{:?}", 10u32.checked_add_signed(-3));    println!("{:?}", 2u32.checked_add_signed(-3));}
Rust Reference