Answers for "sum all elements of array rust"

0

sum all elements of array rust

//Iterator::sum was stabilized in Rust 1.11.0. You can get an iterator from
//your array/slice/Vec and then use sum:

fn main() {
    let a = [1, 2, 3, 4, 5];
    let sum: u8 = a.iter().sum();
    println!("the total sum is: {}", sum);
}
Posted by: Guest on February-18-2022

Code answers related to "sum all elements of array rust"

Browse Popular Code Answers by Language