What is the difference between `e1` and `&e2` when used as the for-loop variable?
When used in a destructuring pattern binding, the ampersand &
is used to dereference a value:
let a_number: i32 = 42;
let a_reference_to_a_number: &i32 = &a_number;
let another_reference = a_reference_to_a_number;
let &another_number = a_reference_to_a_number;
let () = another_reference; // expected type `&i32`
let () = another_number; // expected type `i32`
This applies anywhere a pattern is accepted, such as in a let
or if let
, as a function argument, the for-loop variable, or a match arm.
While initially confusing to many people, this is actually the language being consistent with how enums and structs are pattern matched and thus removed from the inner variable binding:
let a_value: Option<&i32> = Some(&42);
if let Some(&val) = a_value {
let () = val; // expected type `i32`
}
Note how val
is no longer "wrapped" by the Some
, just like it is no longer "wrapped" by the reference.
This behavior of &
in patterns is why the ref
keyword is needed. The ref
keyword is used to unambiguously introduce a reference in a pattern match.