rust - What is the preferred way of matching a variable with a pattern guard that doesn't use the matched value? -
between 2 snippets below, better / preferred one?
fn main() { let pair = 7; match pair { pair if pair > 5 => println!("yeah"), _ => println!("no"), } } fn main() { let pair = 7; match pair { _ if pair > 5 => println!("yeah"), _ => println!("no"), } } and there better way write this? because doesn't work:
fn main() { let pair = 7; match pair { > 5 => println!("yeah"), _ => println!("no"), } }
the version not bind matched variable preferred:
fn main() { let pair = 7; match pair { _ if pair > 5 => println!("yeah"), _ => println!("no"), } } this shortest version with match. of course, example use if.
i'm surprised first version not give warning unused variables.
ah, me being silly. variable is used here, in pattern guard. ^_^
Comments
Post a Comment