| ▲ | uecker 2 days ago | |||||||
And I like using enums in C ;-) The compiler tells you to cover all branches. | ||||||||
| ▲ | kace91 2 days ago | parent | next [-] | |||||||
Rust is a bit smarter than that, in that it covers exhaustiveness of possible states, for more than just enums: fn g(x: u8) { match x { 0..=10 => {}, 20..=200 => {},
}That for example would complain about the ranges 11 to 19 and 201 to 255 not being covered. You could try to map ranges to enum values, but then nobody would guarantee that you covered the whole range while mapping to enums so you’d be moving the problem to a different location. Rust approach is not flawless, larger data types like i32 or floats can’t check full coverage (I suppose for performance reasons) but still quite useful. | ||||||||
| ||||||||
| ▲ | rundev 2 days ago | parent | prev [-] | |||||||
The compiler also tells you that even if you cover all enum members, you still need a `default` to cover everything, because C enums allow non-member values. | ||||||||