6 ms·
> Rust would give an error when an enum isn't exhausted in a match clause. So the issue being described doesn't exist :D I was going to say something like that
by drivers99 10y ago
> Rust would give an error when an enum isn't exhausted in a match clause. So the issue being described doesn't exist :D
I was going to say something like that, but the equivalent code would have a "_" case in the match statement. I don't know rust yet, but would a rust programmer typically make a point to not use a "_" (default) statement so that you can catch these at compile time and expect you'll never have anything else?
- geofft 10y agoIt's not just typical, it's required. If you add a default match when you've already specified all the possible cases, the compiler gives you an error. Here's an example (https://is.gd/KjSc8B https://is.gd/KjSc8B): enum Foo { Bar(i32), Baz(i32), } fn matchfoo(f: Foo) -> i32 { match f { Foo::Bar(x) => x, Foo::Baz(x) => x, _ => 42 } } gives "unreachable pattern", a hard error, for the "_" case.
- ordu 10y agoYou can also avoid using `default' statement in C, and use gcc -Wall while compiling. You'll get warning for missing enum values in `case' statements. You even can add -Werror and get errors when some enum values left unused. But when you have a really big enum and need to handle only small subset of it's values it didn't work. When _this_ problem can be avoided in Rust, it can be avoided in C also. The only Rust bonus here is compiler default behaviour.