m51
1
Is there a more concise way to express this match?
fn main() {
let s = Stuff::Foo(1);
match s {
Stuff::Foo(_) => (),
_ => println!("Doing stuff")
}
}
enum Stuff{
Foo(u8),
Bar(u16),
Baz,
Etc,
}
My actual enum has more discriminants so combining them with | in an if let binding is not really better.
alice
2
How about this?
fn main() {
let s = Stuff::Foo(1);
if !s.is_foo() {
println!("Doing stuff");
}
}
enum Stuff{
Foo(u8),
Bar(u16),
Baz,
Etc,
}
impl Stuff {
fn is_foo(&self) -> bool {
match self {
Stuff::Foo(_) => true,
_ => false,
}
}
}
jer
3
if !matches!(s, Stuff::Foo(_)) {
println!("Doing stuff")
}
using the new matches!
macro, stable since 1.42.0
3 Likes
L.F
4
If you can't tolerate !double! !exclamation! !marks!, here's an alternative:
use std::ops::Not;
if matches!(s, Stuff::Foo(_)).not() {
println!("Doing stuff");
}
(for fun)
2 Likes
system
Closed
5
This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.