I used to think this is similar to lvalue reference collapsing in C++, so &&T
should be the same as &T
, from perspective of syntax, but I am confused after I compiled the following code:
fn check_ref(x: &i32) -> i32 {
println!("{}", x);
x + 2
}
fn main() {
for i in &[-3, 2, 39] {
// i is &i32
check_ref(i); // this works
check_ref(&i); // this works
check_ref(&&i); // this works
assert_eq!(i, &i); // error[E0277]: can't compare `i32` with `&i32`
}
}
Is it true that multiple references collapse, or does ref-to-ref have some other specific meaning?
Is this compiler error of assert_eq!
only because of some tricks in Rust macros?