Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

That's right. It's unfortunate that Rust calls them "enums", since as you noted that term already had a well-established meaning in other languages, and the concept that Rust calls "enums" also had several existing names (sum type, coproduct type, disjoint union, and, more generally, algebraic data type) in the literature and in prior languages.

Language designers: stop changing the meanings of words! I know you mean well and you're trying to tie unfamiliar ideas to familiar ones for beginners, but you end up causing more confusion for everyone in the long run.



Rust enums are also enums:

  enum Colour {
    Red,
    Blue,
    Green,
  }
is fine.


When you say "Rust enums are also enums", that's not true in general. What is true is that _some_ Rust "enums" (like your `Colour` example) can be represented as traditional enums. You don't prove a universal quantification with a single example.


All enums are expressible as Rust enums.


More than that:

    #[derive(Debug)]
    enum Colour {
        Red = 1,
        Blue = 2,
        Green = 4,
    }

    fn main() {
        println!("{:?}", Colour::Blue);
        println!("{}", Colour::Blue as u8);
    }


> It's unfortunate that Rust calls them "enums"

Rust calls them enum to be familiar to people coming from C-family languages, which is a large target.

> since as you noted that term already had a well-established meaning in other languages

Rust enums "degenerate" to a C-style enum (except typesafe) as it can be repr'd to a number and it's possible to select the discriminant (if there's no associated data).

> the concept that Rust calls "enums" also had several existing names (sum type, coproduct type, disjoint union, and, more generally, algebraic data type) in the literature and in prior languages.

Pretty much none of which are actually part of the language e.g. in Haskell or OCaml the designator is `type`, and it's used for both sum types and product types: the sum type simply has a single constructor.

But Rust doesn't use `type`, it uses `struct`. And a `struct` with multiple variants doesn't make sense.


Can't edit but

> the sum type simply has a single constructor.

this should be "product" not "sum"


Enum has one meaning in C family languages. Other languages may have different meanings. For example, in Haskell something is an enum if there is an invertible mapping between that type and (a contiguous subset of) the machine integers (with complications).

In other languages the concept might correspond to (finite) recursively enumerable sets (ie you can list all their elements).




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: