Skip to content

Destructuring slices #15305

Closed
Closed
@pfalabella

Description

@pfalabella

The intention of the second arm of the match below is to match slices with one element only (in this case only the second element in vects should match).

    let vects= [&[1u, 2, 3], &[1], &[]];

    for &vec in vects.iter() {
        let msg= match vec {
            [] => format!("empty"),
            [a]  => format!("only one element: {}", a),
            [a, ..rest] => format!("many elements, first is {}, then there are {}", a, rest) // this is unreachable?
        };

However the compiler complains that the third arm is now unreachable.

The workaround is transforming the second arm in [a,..rest] if rest==[] but I was advised on the #rust IRC channel that the [a] should work and that this is a bug.

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