Write a program to find the sum of all even numbers in a vector of integers in Rust.
Write a program to find the sum of all even numbers in a vector of integers in Rust.
279
20-Jun-2023
Aryan Kumar
24-Jun-2023Sure, here is a Rust program to find the sum of all even numbers in a vector of integers:
Rust
This program first initializes a sum variable. The program then iterates over the vector. For each number in the vector, the program checks if the number is even. If the number is even, the program adds the number to the sum. Finally, the program returns the sum.
To run this program, you can save it as a
.rsfile and then compile and run it using the following commands:Code snippet
This will print the following output:
Code snippet
This indicates that the sum of all even numbers in the vector is 30.