Skip to content

Syntax for matching on a collection element is not obvious #19177

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
mdinger opened this issue Nov 21, 2014 · 0 comments · Fixed by #19312
Closed

Syntax for matching on a collection element is not obvious #19177

mdinger opened this issue Nov 21, 2014 · 0 comments · Fixed by #19312

Comments

@mdinger
Copy link
Contributor

mdinger commented Nov 21, 2014

The documentation should document how [] changes inside a match as opposed to outside of one. A quick browsing of the guides didn't find this noted anywhere:

fn main() {
    let array = ["a", "b"]; // `[]` denotes `array`
    let v = vec!["match_this", "1"];

    match v {
    //    ^ Change `v` to `v.as_slice()` to make this work. Hard to discover...

        // How does this work though? It appears like this is matching a slice
        // against an array and succeeding. It is not obvious that `[]` inside a
        // match is `slice` syntax and not `array` syntax

        // Now `[]` denotes slice...
        ["match_this", a] => println!("{}", a),
        _ => {},
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants