-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Feature request: wildcards in tuple struct patterns #552
Comments
I took this out from the main request, but it also makes sense to support this for regular tuples: let quint = (1i,2i,3i,4i,5i);
println!("This quintet {}", match quint {
(1,2,..) => "goes one two!",
(..,5) => "ends in a 5",
(..,4,_) => "has a penultimate 4",
(4,..,4) => "starts and ends in 4...",
_ => "is a bit weird..."
}); |
This seems doable for tuple structs, because, as you have noted yourself, their arity is fixed. As for tuples, that's a different story entirely, unless a cc @nikomatsakis can you confirm we can do this? (modulo that VG part) |
Tuples of different arity are different types, and the value being matched has a single type, so all arities are known at compile time. |
@SimonSapin I was talking about type inference, the following example works today: match std::default::Default::default() {
(1, 2, 3i) => 42,
(x, y, z) => x + y + z
} There is a single complete type there, |
Any news on this? It is really a bit inconsistent that you sometimes may use struct TPerson(&'static str, u8);
struct SPerson {
name: &'static str,
age: u8
}
fn main() {
let person = TPerson("John", 39);
let TPerson (name, ..) = person; // Invalid
let person = SPerson {name:"John", age:39};
let SPerson {name, ..} = person; // Valid
} |
Triage: Fixed by #1492 |
Yay! |
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:
The text was updated successfully, but these errors were encountered: