Writing &5 to take the address of a constant could seem odd, but in this case the compiler decides to use a storage (in the constant section) and store this value in order to be able to take its address.
If this happens several times in the code, there is no need to have separate storages, all of them holding the same value.
If you try the following code in godbolt, you will see that the constant 5 is placed once in memory and the linker refers to it from several places.
In is_five(): lea rax, [rip + .L__unnamed_1]
In test2(): lea rdi, [rip + .L__unnamed_1]
Thus &5 refers to the same address, but &x, which is a local variable, refers to another storage for x initialised with the same value (5).
In test1(): lea rdi, [rsp + 4]
pub fn is_five(x: &i32) -> bool {
x as *const i32 == &5 as *const i32
}
pub fn test1() -> bool {
let x = 5;
is_five(&x)
}
pub fn test2() -> bool {
is_five(&5)
}
If distinction between reference and pointer comparisons is not clear, this documentation can help.