Skip to content

Instantly share code, notes, and snippets.

@tzemanovic
Created February 19, 2019 20:54
Show Gist options
  • Save tzemanovic/22755cc3e07648c6954b490bc09d5482 to your computer and use it in GitHub Desktop.
Save tzemanovic/22755cc3e07648c6954b490bc09d5482 to your computer and use it in GitHub Desktop.
fold sum
fn sum<I>(i: I) -> I::Item
where
I: Iterator,
I::Item: From<u8> + std::ops::Add<Output = I::Item>,
{
i.fold(From::from(0u8), std::ops::Add::add)
}
fn main() {
let my_vec: Vec<u32> = (1..11).collect();
// let my_vec_sum = (1..11).fold(0, std::ops::Add::add);
let my_vec_sum = sum(1..11);
println!("{:?}, sum: {}", my_vec, my_vec_sum);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment