std-method

Iterator::sum

Combine items through the destination Sum implementation. For integers, an empty iterator sums to zero; account for overflow when input values can be large. Expected stdout (not executed): 17 Common mistake: Assuming sum detects overflow as a Result.

Formal Syntax & Structure

rust syntaxrust
fn sum<S: Sum<Self::Item>>(self) -> S where Self: Sized

Working Example

Example Snippetrust
fn main() {    let total: i32 = [7, 4, 6].into_iter().sum();    println!("{total}");}
Rust Reference