hello everyone.
Learner question.
I thought I understood the automatic deref but obviously I have not! LOL.
Below is a code sample all using &i32.
The code below works identically and correctly whether I explicitly deferefence &i32 *n or leave as n.
What is the reason Rust does not insist I deference &i32 ?
Sorry about poor indentation, the code is tiny, should be easy to understand, I hope.
// block 1 uses *n deref
let mut myvec = vec![0, 1, 2, 3, 4, 5];
myvec.retain(|n| {
println!("processing number: {}", *n);
*n % 2 == 0 // *n is i32, ok
});
// block 2 uses n (no deref, &i32)
let mut myvec = vec![0, 1, 2, 3, 4, 5];
myvec.retain(|n| {
println!("processing number: {}", n);
n % 2 == 0 // &i32 is OK too
});
// block 3
let n: &i32 = &20;
if *n % 2 == 0 { // deref OK in the if statement
println!("n is: {}", *n); // *n is ok, so is plain n also.
}
// block 4
if n % 2 == 0 { // &i32 is OK in if statement too
println!("n is: {}", n);
}
Thank you in advance.