expression
Accept iterable inputs without prescribing storage
An API that only needs to traverse values can accept IntoIterator rather than requiring Vec. The item type describes what the function consumes. Here try_fold and checked_add reject overflow instead of silently wrapping. The same function accepts arrays and vectors of owned u32 values; borrowed slices require copying their items or a different contract. Expected result (NOT RUN): array: Some(5) overflow: None Common mistake: Requiring Vec when no random access or ownership of the collection is needed. Static Rust example; not a verified ByteMind executable assessment.
Formal Syntax & Structure
rust syntaxrust
fn total(values: impl IntoIterator<Item = u32>) -> Option<u32>Working Example
Example Snippetrust
fn total(values: impl IntoIterator<Item = u32>) -> Option<u32> { values.into_iter().try_fold(0u32, |sum, n| sum.checked_add(n))}fn main() { println!("array: {:?}", total([2, 3])); println!("overflow: {:?}", total(vec![u32::MAX, 1]));}