Hi, Daniel,

Consider this example:

    let vec = vec![(1u, 2u)];

    for &(a, b) in vec.iter() {
        println!("{}: {}", a, b);
    }

a and b here are of type uint, which is implicitly copyable. It is not
natural to take references to it when passing around its values. That
is, variables under & pattern are not *always* taken as a reference.

Another reason is consistency. Patterns and corresponding constructor
expressions are meant to be inverses of each other:

    let x = &(1u, 2i);
    match x {
        &(a, b) => ...
    }

See how nicely `&(a, b)` and `&(1, 2)` correspond to each other. `a`
is of type uint, just like the literal 1u, and `b` is int, like 2i.
This expands to other types too, including complex ones like `Box`.
With automatic references there won't be such consistency.

2014-11-10 20:29 GMT+03:00 Daniel Trstenjak <daniel.trsten...@gmail.com>:
>
> 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
> Rust-dev@mozilla.org
> https://mail.mozilla.org/listinfo/rust-dev
_______________________________________________
Rust-dev mailing list
Rust-dev@mozilla.org
https://mail.mozilla.org/listinfo/rust-dev

Reply via email to