Using enums to represent state in Rust

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

Our great sponsors
  • WorkOS - The modern identity platform for B2B SaaS
  • InfluxDB - Power Real-Time Data Analytics at Scale
  • SaaSHub - Software Alternatives and Reviews
  • 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.

  • runwasi

    Facilitates running Wasm / WASI workloads managed by containerd

  • I wish Go had (real) enums.

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

  • WorkOS

    The modern identity platform for B2B SaaS. The APIs are flexible and easy-to-use, supporting authentication, user identity, and complex enterprise features like SSO and SCIM provisioning.

    WorkOS logo
  • 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...

  • 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.

  • 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.

  • InfluxDB

    Power Real-Time Data Analytics at Scale. Get real-time insights from all types of time series data with InfluxDB. Ingest, query, and analyze billions of data points in real-time with unbounded cardinality.

    InfluxDB logo
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