snippetrustCritical
How to not do anything on the "rest case" when matching a string?
Viewed 0 times
howmatchinganythingtherestwhennotstringcase
Problem
I have a string where I want to check if it's a semicolon, comma or colon. If it's not any of those, I don't want to do anything:
This works, but I don't really want to print a bunch of empty lines (cause a lot of the tokens don't match these criteria).
What would be the most correct way to solve this?
match token.as_ref() {
";" => semicolons += 1,
"," => commas += 1,
":" => colons += 1,
_ => println!(""),
}This works, but I don't really want to print a bunch of empty lines (cause a lot of the tokens don't match these criteria).
What would be the most correct way to solve this?
Solution
The Rust Programming Language (2nd edition) states:
The () is just the unit value, so nothing will happen in the
You can also use an empty block expression
let some_u8_value = 0u8;
match some_u8_value {
1 => println!("one"),
3 => println!("three"),
5 => println!("five"),
7 => println!("seven"),
_ => (),
}
The () is just the unit value, so nothing will happen in the
_ case. As a result, we can say that we want to do nothing for all the possible values that we don’t list before the _ placeholder.You can also use an empty block expression
{}.Context
Stack Overflow Q#49510965, score: 186
Revisions (0)
No revisions yet.