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.
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 an Option<i32>
.vec.extend_from_slice(elements)
to add all elements from a slice to a vector.Vec::remove
to remove an element by index.get
method on vectors.Vec::remove
will panic if the index is out of bounds, so handle it carefully in remove_element
.