I'm confused why the function get
works with both Vec<T>
and &Vec<T>
. I know that &Vec<T>
automatically converts to &[T]
so in some sense, the question is why it works on Vec<T>
as well as &[T]
. Obviously, get
works with &[T]
, so is it separately implemented for Vec<T>
in addition to the implementation for &[T]
? Looking at the docs, it doesn't seem like it, there is only one get
implementation: https://doc.rust-lang.org/std/vec/struct.Vec.html#method.get
In the following code, get
is acting on Vec<T>
.
fn first<T: PartialOrd + Copy>(list: Vec<T>) -> T {
*list.get(0).unwrap()
}
fn main() {
let number_list = vec![34, 50, 25, 100, 65];
let result = first(number_list);
println!("The first number is {}", result);
}
In this code, it is acting on &Vec<T>
(aka &[T]
):
fn first<T: PartialOrd + Copy>(list: &Vec<T>) -> T {
*list.get(0).unwrap()
}
fn main() {
let number_list = vec![34, 50, 25, 100, 65];
let result = first(&number_list);
println!("The first number is {}", result);
}