I´m trying to understand the shared reference behabior on the following functions, can someone please why do i need the for &item in list.iter()
?
fn largest(list: &[i32]) -> i32 {
let mut largest = list[0]; // shouldn´t largest be a &i32?
for &item in list.iter() { // if list is a &[i32]
// shouldn´t item be a &32 (just item, not &item)
// for item in list.iter() this will trow a mismatch type error on the line below
if item > largest {
largest = item;
}
}
largest
}
Why do i need to use &item in a list thats already a &[i32] ?
That´s an example from Generic Types, Traits, and Lifetimes - The Rust Programming Language