Skip to content

Feature request: wildcards in tuple struct patterns #552

Closed
@ColonelJ

Description

@ColonelJ

Allow wildcards in tuple structs to leave the majority of elements unspecified, which makes a lot of sense in tuple structs where the arity is never in question! This allows you to put the data you want to match on at the start (or the end) of the tuple struct, and simplify your code.

Here's some sample code to demonstrate what should be possible, and what's possible already with arrays:

#![feature(advanced_slice_patterns)]
struct Thing(int,int,int,int,int);

fn main() {
    let arr = [1i,2,3,4,5];
    println!("This array {}", match arr {
        [1,2,..] => "goes one two!", // This one works as standard
        [..,5] => "ends in a 5",
        [..,4,_] => "has a penultimate 4",
        [4,..,4] => "starts and ends in 4...",
        _ => "is a bit weird..."
    }); // This works already using advanced_slice_patterns feature!

    let thing = Thing(1,2,3,4,5);
    println!("This thing {}", match thing {
        Thing(1,2,..) => "goes one two!",
        Thing(..,5) => "ends in a 5",
        Thing(..,4,_) => "has a penultimate 4",
        Thing(4,..,4) => "starts and ends in 4...",
        _ => "is a bit weird..."
    }); // Currently only Thing(..) is supported
}

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