3 ms·
#![feature(or_patterns)] // Prompt the user whether they want to accept the match pub fn check(x: i32) -> bool { use std::io::{stdin};
by munro 6y ago
#![feature(or_patterns)]
// Prompt the user whether they want to accept the match
pub fn check(x: i32) -> bool {
use std::io::{stdin};
println!("Checking this match: {}. Should we accept it? y/n", x);
let mut s = String::new();
stdin().read_line(&mut s).expect("Did not enter a correct string");
return s.trim() == "y"
}
pub fn main() {
match (1, 2) {
(x, _) | (_, x) if check(x) => {
println!(":)")
}
_ => {
println!(":(")
}
}
}
Reading this really hurts my head, it's so bizarre seeing x used twice, and the if expression called twice for each x. Even the author calls this weird, why would anyone want this?!
https://rust-lang.github.io/rfcs/2535-or-patterns.html https://rust-lang.github.io/rfcs/2535-or-patterns.html
Reading the RFC, it seems like didn't intend for the syntax to be abused like this, so perhaps it needs some refining. The RFC shows much more immediately understandable examples of or_patterns, which does look really nice.
pub fn is_green(self) -> bool {
match self {
| Tile::Suited(Suit::Souzu, 2 | 3 | 4 | 6 | 8)
| Tile::Dragon(Dragon::Green) => true,
_ => false,
}
}