Enums

  • Rust supports regular enums:
    enum IpAddr {
      V4,
      V6
    }
    
    IpAddr::V4
    
  • Each enum entry can also have “associated data” that is not necessarily consistent:
    enum IpAddr {
      V4(String),
      V6(String)
    }
    
    IpAddr::V6(String::from("::1"))
    
    enum Message {
      Quit,
      Move { x: i32, y: i32},
      Write(String),
      ChangeColor(i32, i32, i32)
    }
    
    Message::Move { x: 5, y: 6 };
    Message::ChangeColor(255, 255, 0);
    
  • This is almost like each enum variant is a struct-like thing on its own, but it’s also a Message, and so can be passed to anything that expects a Message.
Edit