Using enums to represent state in Rust

This page summarizes the projects mentioned and recommended in the original post on news.ycombinator.com

SaaSHub - Software Alternatives and Reviews
SaaSHub helps you find the best software and product alternatives
www.saashub.com
featured
  1. cheats.rs

    Rust Language Cheat Sheet - https://cheats.rs

    Reposting: https://cheats.rs/#memory-layout

    > Does the value take up the same width regardless of state?

    Yes. As the other commenter mentioned, it's the size of the largest variant (same as a union in C) + a tag (almost the same as an enum in C). In some cases, the compiler even manages to optimize out the tag.

  2. SaaSHub

    SaaSHub - Software Alternatives and Reviews. SaaSHub helps you find the best software and product alternatives

    SaaSHub logo
  3. runwasi

    Facilitates running Wasm / WASI workloads managed by containerd

    I wish Go had (real) enums.

    https://github.com/containerd/runwasi/blob/ba5ab5ada5a401762...

  4. higher-kinded-lifecycle

    A demonstration of an idea about how to represent the lifecycle of entities in Scala using higher-kinded types

    Time to re-pimp my idea of doing this using a single type parameterised to reflect different states:

    https://github.com/tim-group/higher-kinded-lifecycle/blob/ma...

  5. ceylon-lang.org

    Discontinued Awestruct built static website for ceylon-lang.org

    > One thing Rust could really use are anonymous unions (A | B |C instead of E::A(A), E::B(B), E::C(C)). They are to enums what tuple types are to structs.

    Ceylon had union types, which is the only place i've seen these: https://github.com/eclipse-archived/ceylon-lang.org/blob/mas...

    Another thing Rust enums are missing is having each variant be a type. If you have an enum Shape with variants Circle, Rectangle, and Polygon, there is no way to write a function which only takes a Circle. So you end up defining a struct for each case, then making your enum a trivial wrapper round the three structs. You end up with Shape::Circle and Circle, which are different things, and writing code like c.0.radius to get at the fields. It's rather inelegant. So either variants should be types in their own right, or an enum should be defined as a composition of existing types.

  6. lang-team

    Home of the Rust lang team

    I haven't been following this closely, so I looked it up and it looks like that's not going to happen for the foreseeable future unfortunately:

    https://github.com/rust-lang/lang-team/issues/122

    Kind of a shame, but wrapper types work well enough that I understand. It does look like if there was someone with enough resources to make it happen that they'd be receptive to it.

NOTE: The number of mentions on this list indicates mentions on common posts plus user suggested alternatives. Hence, a higher number means a more popular project.

Suggest a related project

Related posts