Hacker News new | ask | show | jobs
by a_humean 2183 days ago
Rust enums aren't like C style enums. They are a kind of algebraic data type: a sum type. Sum types haven't, until recently, been very well represented outside of functional programming languages. In haskell for example you might have a function that accepts a sum type, and to handle it you pattern match each variant and provide an implementation for that variant.

This is pretty core to functional programming languages like haskell, and its very similar to how rust behaves, and is omnipresent much like in haskell. For example, rust does not have exceptions, but repersents errors via the `Result` enum which is analogous to Haskell's `Either` data type. In order to access any value wrapped in an Result you must pattern match on it and explictly handle the possibility of an error.

All of this comes from functional programming languages, but they are starting to become common place in mainstream languages.