for (contact, &number) in contacts.iter() {
println!("Calling {}: {}", contact, call(number));
}
somehow number is referenced with an & while contact is not, and why are they handled differently. So I've started tweaking this code, and all four possible permutations print exactly the same output:
contact, number
contact, &number
&contact, number
&contact, &number
(snip)
Calling Daniel: Hi! Who is this again?
Calling Robert: Hi! Who is this again?
Calling Katie: Hi! Who is this again?
Can someone explain why adding/removing an ampersand does not change code's meaning? TIA.
First let's make sure we understand what types we're dealing with:
let _: () = contacts.iter().next();
40 | let _: () = contacts.iter().next();
| -- ^^^^^^^^^^^^^^^^^^^^^^ expected `()`, found enum `Option`
| |
| expected due to this
|
= note: expected unit type `()`
found enum `Option<(&&str, &&str)>`
So here, we're binding against a (&&str, &&str):
for (contact, &number) in contacts.iter() {
// similar to
// let (contact, &number) = (&"some literal str", &"another literal");
So contact is a &&str and number is a &str. In all your variations, the types of the bindings are some combination of &str and &&str. With this understanding, the question becomes: Why do &str and &&str display the same?
And as was already answered, Rust tends to "see through" indirection like this, at least for things like Eq or Display. Thus they ultimately do the same thing when you print them.