Skip to content

RFC: Irrefutable patterns in closure argument lists #3586

Closed
@bstrie

Description

@bstrie

Pre-demoding, this sort of thing is valid:

fn main() {
    for ["Alice", "Bob", "Carol"].each |name| {
        do task::spawn {
            let v = rand::Rng().shuffle([1, 2, 3]);
            for v.each |num| {
                io::print(fmt!("%s says: '%d'\n", name, num))
            }
        }
    }
}

This looks very nice.

But post-demoding, it looks like this:

fn main() {
    for ["Alice", "Bob", "Carol"].each |name| {
        let name = *name;  // because `name` is now a pointer
        do task::spawn {
            let v = rand::Rng().shuffle([1, 2, 3]);
            for v.each |num| {
                io::print(fmt!("%s says: '%d'\n", name, *num))  // `num` is also a pointer
            }
        }
    }
}

Compared to the first example, this looks quite poor .

@nikomatsakis has mentioned (here) that if Rust supported irrefutable patterns in argument lists, the example would instead become:

fn main() {
    for ["Alice", "Bob", "Carol"].each |&name| {
        do task::spawn {
            let v = rand::Rng().shuffle([1, 2, 3]);
            for v.each |&num| {
                io::print(fmt!("%s says: '%d'\n", name, num))
            }
        }
    }
}

Which looks rather nice.

The inability to do this has already resulted in #3550 as well as the loss of one very nice front-page code example. I can foresee a lot of grumbling over the lack of this feature.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions