Beginner: What does the number reference (e.g. `&2`) mean?

I came across the following code from

fn main() {
    println!("{:?}", (0..).contains(&2));

I get that contains() expects a reference but having the reference symbol (&) in front of a number looked odd. Aren't numbers just copied by value? What does it mean to have a reference to it? Does that mean all numbers we use throughout the program are stored somewhere so that the same number can have the same reference?


It's a reference to a constant number which is stored in your program's binary. If you have a literal like 2 (or true, or "hello"), it gets put in the binary in a section of constants; the reference is a pointer to that, which you can see here.

1 Like

Thanks. That makes sense. But here is a follow up question.

Then shouldn't (0..) (&0, &1, &2, &3, ...) be put in the constants section as well? Otherwise, how does it compare &2 with the elements in (0..)?

When you compare two references to the same type, you can think of it as the equivalence operator (PartialEq implementation, for complex types, or compiler intrinsic, for primitives) being able to read the values behind the references and compare them, not the references themselves.

1 Like

The contains method requires a reference to an integer, and as such, you must pass it a reference. It requires a reference, because the method is generic, and works for both Copy and non-Copy types.

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.