| Rust arrays are fixed-size collections of elements of the same type. Operations include accessing elements by index (using bracket notation like array[index]), iterating over elements using loops or iterators, and performing calculations using methods like len() (to get the length) or comparing arrays for equality. However, unlike dynamically sized collections like vectors, you cannot directly add or remove elements from a Rust array after its creation; its size is determined at compile time. You can create new arrays by copying or slicing existing ones. Here's a simple example demonstrating how to work with arrays in Rust: fn main() {
// Defining an array with 5 integers
let numbers: [i32; 5] = [10, 20, 30, 40, 50];
// Accessing array elements
println!("First number: {}", numbers[0]);
println!("Second number: {}", numbers[1]);
// Iterating through the array
println!("Iterating through the array:");
for number in numbers.iter() {
println!("{}", number);
}
// Using array with a fixed value
let repeated_values: [i32; 3] = [42; 3]; // [42, 42, 42]
println!("Array with repeated values: {:?}", repeated_values);
// Checking the length of the array
println!("Length of numbers array: {}", numbers.len());
}
Explanation:- Array Declaration:
let numbers: [i32; 5] = [10, 20, 30, 40, 50]; Declares an array of type i32 with 5 elements.- The size of an array is fixed and must be known at compile time.
- Accessing Elements:
- Use square brackets to access elements, e.g.,
numbers[0].
- Iteration:
- Use
.iter() to get an iterator over the array.
- Repeated Values:
[value; size] initializes an array where all elements are value.
- Length:
- Use
.len() to get the size of the array.
Output:First number: 10
Second number: 20
Iterating through the array:
10
20
30
40
50
Array with repeated values: [42, 42, 42]
Length of numbers array: 5
In Rust, arrays have a fixed size determined at compile time, so you cannot dynamically add or remove elements from them. However, you can use a vector (Vec<T>), which is a dynamically-sized collection type in Rust, for such operations. Example: Using a Vector for Dynamic Growthfn main() {
// Create an empty vector
let mut numbers: Vec<i32> = Vec::new();
// Adding elements to the vector
numbers.push(10);
numbers.push(20);
numbers.push(30);
// Display the vector
println!("Vector: {:?}", numbers);
// Remove the last element
numbers.pop();
println!("After pop: {:?}", numbers);
// Accessing elements
println!("First element: {}", numbers[0]);
// Iterating through the vector
println!("Iterating through the vector:");
for number in &numbers {
println!("{}", number);
}
// Get the length
println!("Vector length: {}", numbers.len());
}
Explanation:- Create a Vector:
Vec::new() creates an empty vector.- Alternatively, you can initialize with values:
let numbers = vec![10, 20, 30];.
- Add Elements:
numbers.push(value); appends value to the end of the vector.
- Remove Elements:
numbers.pop(); removes the last element and returns it as an Option.
- Accessing Elements:
- Use
numbers[index] for direct access or .get(index) for safe access (returns an Option).
- Dynamic Size:
- Vectors grow and shrink as needed.
Output:Vector: [10, 20, 30]
After pop: [10, 20]
First element: 10
Iterating through the vector:
10
20
Vector length: 2
Use vectors when you need dynamic resizing. Arrays are more efficient for fixed-size collections. |