std-method

Integer saturating_add and saturating_sub

Keep a bounded quantity within its numeric limits. This works for byte brightness but may hide invalid financial or inventory changes. Expected stdout (not executed): 255 0 Common mistake: Using saturation when the caller needs to know that an operation failed.

Formal Syntax & Structure

rust syntaxrust
x.saturating_add(y); x.saturating_sub(y)

Working Example

Example Snippetrust
fn main() {    let brightness = 245u8;    println!("{} {}", brightness.saturating_add(30), 10u8.saturating_sub(30));}
Rust Reference