Hone logo
Hone
Problems

Mastering Rust Arrays: Initialization, Access, and Manipulation

Arrays are fundamental data structures in programming, providing a contiguous block of memory to store elements of the same type. In Rust, arrays are fixed-size and known at compile time, making them efficient for many use cases. This challenge will test your understanding of how to initialize, access, and perform basic manipulations on Rust arrays.

Problem Description

Your task is to create a Rust program that demonstrates proficiency with arrays. You will need to:

  1. Initialize an array: Create an array of a specific type and size.
  2. Access elements: Retrieve individual elements from the array using their indices.
  3. Iterate through the array: Traverse all elements of the array.
  4. Modify elements: Change the value of specific elements within the array.
  5. Calculate basic statistics: Find the sum and average of the elements in the array.

You should aim to implement these operations in a clear and idiomatic Rust style.

Examples

Example 1: Input: An integer array [10, 20, 30, 40, 50]

Output:

Initial array: [10, 20, 30, 40, 50]
Element at index 2: 30
Sum of elements: 150
Average of elements: 30.0
Array after modification: [10, 25, 30, 45, 50]
Iterating through the modified array:
10
25
30
45
50

Explanation: The program initializes the array, prints its initial state, accesses the element at index 2, calculates the sum and average, modifies elements at indices 1 and 3, and then iterates through the modified array printing each element.

Example 2: Input: A float array [1.1, 2.2, 3.3]

Output:

Initial array: [1.1, 2.2, 3.3]
Element at index 0: 1.1
Sum of elements: 6.6
Average of elements: 2.2
Array after modification: [1.1, 2.2, 5.5]
Iterating through the modified array:
1.1
2.2
5.5

Explanation: Similar to Example 1, but with floating-point numbers. The array is initialized, elements are accessed, statistics are calculated, and modifications are made before iteration.

Example 3: Edge Case - Empty Array (Conceptually) While Rust arrays must have a size known at compile time, demonstrating handling an array of size 1 is good practice.

Input: An integer array [100]

Output:

Initial array: [100]
Element at index 0: 100
Sum of elements: 100
Average of elements: 100.0
Array after modification: [150]
Iterating through the modified array:
150

Explanation: Handles an array with a single element correctly. Modifications and calculations should still function as expected.

Constraints

  • The array size will be between 1 and 100 inclusive.
  • Array elements will be integers (i32) or floating-point numbers (f64).
  • The input array will always be valid and within the specified size constraints.
  • Your solution should be efficient; avoid unnecessary computations.

Notes

  • Remember that Rust arrays have a fixed size, determined at compile time. You'll need to specify the type and size when declaring them.
  • Consider how you will handle potential division by zero when calculating the average, although with the given constraints, this is unlikely to be an issue for arrays of size 1 or more.
  • Rust's ownership and borrowing rules are important. Be mindful of how you access and modify array elements.
  • Think about the most idiomatic ways to iterate over arrays in Rust.
Loading editor...
rust