As far as I understand it, saying

let &a = ...

is similar to saying

let a = ...
let a = *a

which generally tries to move `a`. The same is true here:

for &(a, b) in vec.iter()

tries to move `a` and `b`, which I think could be potentially valid if you
had:

let vec: Vec<&(a, b)> = ...
for &(a, b) in vec.into_iter()

To signify that we want to refer to them without moving, we add `ref`. FWIW
it's also not possible to say

for (ref a, ref b) in vec.iter()

because the iter produces a reference to a tuple.

On Mon, Nov 10, 2014 at 9:29 AM, Daniel Trstenjak <
[email protected]> wrote:

>
> Dear rust devs,
>
>     let vec = vec![("a".to_string(), "b".to_string())];
>
>     for &(ref a, ref b) in vec.iter() {
>         println!("{}: {}", a, b);
>     }
>
> I understand that the '&' and 'ref' are needed here, because otherwise
> the 'String' could be moved out of the 'Vec'.
>
> I don't quite understand, why there's the need for these explicit refs,
> why isn't the outer '&' enough to indicate that everything inside of
> the pattern is also referenced?
>
> Why isn't it possible to just have (with the same semantics):
>
>     for &(a, b) in vec.iter() {
>         println!("{}: {}", a, b);
>     }
>
>
> Without these refs pattern matching would be aesthetically more in sync
> with the types, which I would consider very pleasing.
>
>
> Greetings,
> Daniel
> _______________________________________________
> Rust-dev mailing list
> [email protected]
> https://mail.mozilla.org/listinfo/rust-dev
>
_______________________________________________
Rust-dev mailing list
[email protected]
https://mail.mozilla.org/listinfo/rust-dev

Reply via email to