Here's a Rust program that sums all elements of an array of 10 integers.
Rust offers a very concise and idiomatic way to do this using iterators.
rust
fn main() {
// Declare an array of 10 integers.
// Rust infers the type as [i32; 10] from the literals and the count.
let numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
println!("The array elements are: {:?}", numbers);
// --- Method 1: Using the `sum()` method on an iterator (Most idiomatic Rust) ---
// `numbers.iter()` creates an iterator over references to the elements.
// `sum()` consumes the iterator and sums up all the values.
// The type annotation `as i32` is sometimes needed if Rust can't infer the exact sum type,
// though for simple i32 arrays, it often isn't strictly necessary.
let total_sum_idiomatic: i32 = numbers.iter().sum();
println!("\nSum using .iter().sum(): {}", total_sum_idiomatic);
// --- Method 2: Using a traditional `for` loop (More explicit) ---
// Initialize a mutable variable to store the sum.
let mut total_sum_loop = 0;
// Iterate over each element in the array.
// The `&num` means we are iterating over references to the numbers,
// then dereferencing `*num` to add the value.
// Alternatively, `for num in numbers { ... }` would iterate over owned values (if numbers could be moved).
// For fixed-size arrays, `for &num in &numbers` is common if you want to copy the value.
for num in &numbers {
total_sum_loop += num; // `num` here is `&i32`, so Rust automatically dereferences it (`*num`)
// for the addition operation, making it concise.
}
println!("Sum using a for loop: {}", total_sum_loop);
// --- Method 3: Using `fold` (More general for reducing operations) ---
// `fold` takes an initial value (0) and a closure.
// The closure takes the accumulated value (acc) and the current element (x).
// It returns the new accumulated value.
let total_sum_fold = numbers.iter().fold(0, |acc, &x| acc + x);
// Note: `&x` here explicitly dereferences the reference from the iterator.
println!("Sum using fold: {}", total_sum_fold);
}
Explanation:
let numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
This declares an immutable array named numbers.
[i32; 10] is its inferred type: an array of 10 i32 (32-bit signed integer) values.
numbers.iter().sum() (Most Idiomatic):
numbers.iter(): Creates an iterator over the elements of the numbers array. This iterator yields references (&i32) to the elements.
.sum(): This is a method provided by the Iterator trait for types that can be summed (like numbers). It consumes the iterator and returns the total sum of all elements. Rust is smart enough to handle the dereferencing of &i32 to i32 for the summation.
for num in &numbers { ... } (Traditional Loop):
let mut total_sum_loop = 0;: We initialize a mutable variable total_sum_loop to 0 to store our running total.
for num in &numbers: This loop iterates over each element in the numbers array. The &numbers creates a slice reference to the array, allowing the loop to borrow its elements. num will be a reference (&i32) to each element.
total_sum_loop += num;: Rust's arithmetic operators are smart. When you add &i32 to i32, it automatically dereferences &i32 to i32 before performing the addition.
fold is a powerful iterator method for reducing an iterator to a single value.
0: This is the initial value for the accumulator (acc).
|acc, &x| acc + x: This is a closure (an anonymous function).
acc: The accumulated sum so far.
&x: The current element from the iterator (it's a reference, so we dereference it with &).
acc + x: The new accumulated sum.
All three methods will produce the same output: 55. The sum() method is generally preferred in Rust for its conciseness and clarity when simply adding up numbers.