Rust Book, Chapter 6: Enums and Pattern Matching 0 ▲ Old man yells at Internet on Ross A. Baker (English) 22 hours ago · Tech · hide · 0 comments I am still primarily a Scala 2 developer, so I’ll continue to lean into sealed traits in these examples. Scala 3 enum covers many of the same ideas, and in a syntax closer to Rust’s! Defining an enum # A Rust enum implements sum types, as Scala 2 does with sealed traits. The variants V4 and V6 are like the case classes and objects that extend the trait. CC-BY-SA-4.0 enum IpAddrKind { V4, V6, } CC-BY-SA-4.0 sealed trait IpAddrKind object IpAddrKind { case object V4 extends IpAddrKind case object V6 extends IpAddrKind } All the struct types we saw in the previous chapter are available here. The Scala Write is not a zero-cost newtype like Rust’s mostly to avoid a lengthy digression into Scala 2’s various encodings and tradeoffs. CC-BY-SA-4.0 enum Message { Quit, Move { x: i32, y: i32 }, Write(String), ChangeColor(i32, i32, i32), } CC-BY-SA-4.0 sealed trait Message { case object Quit extends Message case class Move(x: Int, y: Int) extends Message case class Write(value: String) extends… No comments yet. Log in to reply on the Fediverse. Comments will appear here.