Mastering Rust Vectors: A Foundation
Vectors are fundamental data structures in Rust, providing dynamically sized, contiguous arrays. This challenge will test your understanding of creating, manipulating, and iterating over vectors, crucial skills for any Rust developer. Successfully completing this challenge demonstrates a solid grasp of Rust's memory management and ownership principles within the context of a common data structure.
Problem Description
You are tasked with implementing a set of basic vector operations in Rust. The goal is to create a module named vector_basics that provides functions for initializing, adding elements, retrieving elements, and calculating the size of a vector. The functions should handle potential errors gracefully, particularly when accessing elements out of bounds.
Key Requirements:
create_vector(size: usize, initial_value: i32): Creates a new vector of the specifiedsize, initialized with the giveninitial_value. Ifsizeis 0, return an empty vector.add_element(vector: &mut Vec<i32>, element: i32): Appends a newelementto the end of the providedvector.get_element(vector: &Vec<i32>, index: usize) -> Option<i32>: Retrieves the element at the givenindexfrom thevector. If theindexis out of bounds, returnNone. Otherwise, returnSome(element).vector_size(vector: &Vec<i32>) -> usize: Returns the number of elements in thevector.
Expected Behavior:
The functions should behave as described above, adhering to Rust's ownership and borrowing rules. Error handling should be done using Option for out-of-bounds access.
Edge Cases to Consider:
- Empty vectors.
- Zero-sized vectors created by
create_vector. - Accessing elements beyond the vector's bounds.
- Large vector sizes (though performance is not a primary concern for this exercise).
Examples
Example 1:
Input: create_vector(3, 5)
Output: vec![5, 5, 5]
Explanation: A vector of size 3 is created, with each element initialized to 5.
Example 2:
Input: vector = vec![1, 2, 3]; add_element(&mut vector, 4);
Output: vector = vec![1, 2, 3, 4]
Explanation: The element 4 is appended to the end of the vector.
Example 3:
Input: vector = vec![10, 20, 30]; get_element(&vector, 1);
Output: Some(20)
Explanation: The element at index 1 (the second element) is 20.
Example 4:
Input: vector = vec![1, 2, 3]; get_element(&vector, 5);
Output: None
Explanation: Index 5 is out of bounds for a vector of size 3.
Example 5:
Input: vector = vec![]; vector_size(&vector);
Output: 0
Explanation: The size of an empty vector is 0.
Constraints
- All vectors will contain
i32elements. sizeincreate_vectorwill be a non-negativeusize.indexinget_elementwill be a non-negativeusize.- The focus is on correctness and clarity, not extreme performance optimization.
Notes
- Consider using the
Vec::new()constructor to create an empty vector. - The
Vec::push()method is useful for adding elements to the end of a vector. - Remember to handle potential out-of-bounds access when retrieving elements.
- Rust's ownership and borrowing rules are crucial here. Pay close attention to how you pass vectors to functions (mutable vs. immutable references).
- The
Optiontype is your friend for handling cases where an element might not exist (e.g., out-of-bounds access).