std-method

Signed checked_neg and checked_abs

The magnitude of the most negative signed number is one greater than the largest positive value. Use checked operations when a signed result is required. Expected stdout (not executed): Some(9) Some(9) None None Common mistake: Assuming every signed value has a representable positive counterpart.

Formal Syntax & Structure

rust syntaxrust
x.checked_neg(); x.checked_abs()

Working Example

Example Snippetrust
fn main() {    println!("{:?} {:?}", (-9i32).checked_neg(), (-9i32).checked_abs());    println!("{:?} {:?}", i32::MIN.checked_neg(), i32::MIN.checked_abs());}
Rust Reference