Skip to content

Make array patterns match on [T, ..n] and [T] only #478

@ftxqxd

Description

@ftxqxd

Array patterns currently match on either &[T] or [T, ..n]:

let x: &[int] = &[1, 2, 3];
let y: Box<[int]> = box [1, 2, 3];
let z: [int, ..3] = [1, 2, 3];

// Does not work
match x {
    &[1, 2, 3] => {},
    _ => {},
}

// Does not work
match y {
    box [1, 2, 3] => {},
    _ => {},
}

// Does work
match z {
    [1, 2, 3] => {},
    _ => {},
}

// Does work
match x {
    [1, 2, 3] => {},
    _ => {},
}

Logically, array patterns should only work on the bare array types, [T, ..n] and [T]. That would mean that all array patterns that match on &[T] would have to be prefixed by &, but it would also allow matching on Box<[T]> (and hypothetically, Rc<[T]>, Arc<[T]> etc. eventually). Has this already been considered (perhaps as part of DST)?

Metadata

Metadata

Assignees

No one assigned

    Labels

    T-langRelevant to the language team, which will review and decide on the RFC.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions