Vectors are one of Rust's most commonly used collections. They are growable arrays that can hold multiple elements of the same type. They are defined in Rust using the Vec<T> type, where T is the type of elements the vector contains.
In this challenge, you will perform basic operations on a vector, such as adding, removing, and accessing elements. Understanding how to work with vectors is essential for building efficient and idiomatic Rust applications.
Your Task
You are required to implement the following functions:
add_elements: This function takes a mutable reference to a vector of integers and a slice of integers. It appends all elements from the slice to the vector.remove_element: This function takes a mutable reference to a vector of integers and an index. It removes the element at the given index if it exists.get_element: This function takes a reference to a vector of integers and an index. It returns the element at the given index as anOption<i32>.
Hints
<details> <summary>Click here to reveal hints</summary>- You can use
vec.extend_from_slice(elements)to add all elements from a slice to a vector. - Use
Vec::removeto remove an element by index. - To safely access an element by index, use the
getmethod on vectors. - Keep in mind that
Vec::removewill panic if the index is out of bounds, so handle it carefully inremove_element.
← PreviousNext →
pub fn add_elements(vec: &mut Vec<i32>, elements: &[i32]) {// Your code here}pub fn remove_element(vec: &mut Vec<i32>, index: usize) {// Your code here}pub fn get_element(vec: &Vec<i32>, index: usize) -> Option<i32> {// Your code here}// Example usagepub fn main() {let mut vec = vec![1, 2, 3];add_elements(&mut vec, &[4, 5]);assert_eq!(vec, vec![1, 2, 3, 4, 5]);remove_element(&mut vec, 2);assert_eq!(vec, vec![1, 2, 4, 5]);assert_eq!(get_element(&vec, 1), Some(2));assert_eq!(get_element(&vec, 10), None);}