Rustlings: Live!

Goals:

Share knowledge

Ask Questions

Pair

Grow

Social Rules

https://www.recurse.com/manual

Have Fun!

What is an `enum`?

An enumeration, also referred to as enum is a simultaneous definition of a nominal enumerated type as well as a set of constructors, that can be used to create or pattern-match values of the corresponding enumerated type.

Simple Enums

enum Direction { 
    Up,
    Down,
    Left,
    Right
}

Simple Enums

enum Direction { 
    Up,
    Down,
    Left,
    Right
}

let move = Direction::Left;

Simple Enums

enum KeyboardControls {
    Quit,
    KeyPress { 
        w: bool, a: bool, s: bool, d: bool
    },
    Direction(String),
    Move(i32, i32),
} 
enum KeyboardControls {
    Quit,
    KeyPress { 
        w: bool, a: bool, s: bool, d: bool
    },
    Direction(String),
    Move(i32, i32),
} 
struct Quit;
struct KeyPress {
    w: bool, a: bool, s: bool, d: bool
}
struct Direction(String);
struct Move(i32, i32);

Enums vs Structs

Using Simple Enums

enum Direction {
    Up,
    Down,
    Left,
    Right
}

impl Direction {
    fn move_character(&self) {
        println!("{:?}", self);
    }
}

Using Simple Enums

Option Enums

Rust doesn't have a concept of "null". The problem with null values is that if you try to use a null value as a not-null value, you’ll get an error of some kind. Because this null or not-null property is pervasive, it’s extremely easy to make this kind of error.

 

However, Rust has an enum that can encode the concept of a value being present or absent. This enum is Option<T>

Option Enums

enum Option<T> {
    Some(T),
    None,
}

 <T> means the Some variant of the Option enum can hold one piece of data of any type.

Option Enums

The compiler can’t infer the type that the Some variant will hold by looking only at a None value.

Option Enums

Option Enums

enum Colors {
    Red,
    Blue,
    Green,
    Yellow
}


fn hex_from_color(colors: Colors) -> String {
    match colors {
        Colors::Red => String::from("#FF0000"),
        Colors::Blue => String::from("#0000FF"),
        Colors:: Green => String::from("#00FF00"),
        Colors::Yellow => String::from("#FFFF00")
    }
}

Option Enums

Option Enums

enum Colors {
    Red,
    Blue,
    Green,
    Yellow
}


fn hex_from_color(colors: Colors) -> String {
    match colors {
        Colors::Red => String::from("#FF0000"),
        Colors::Blue => String::from("#0000FF"),
        Colors:: Green => String::from("#00FF00"),
        Colors::Yellow => String::from("#FFFF00")
    }
}
enum ShadesOfBlue {
    AliceBlue,
    Turqoise
}

enum Colors {
    ...
    Blue(ShadesOfBlue),
}

fn hex_from_color(colors: Colors) -> String {
    match colors {
        Colors::Blue(shade) => {
            println!("Shade of blue: {:?}", shade);
            String::from("#0000FF")
        }
    }
}

Option Enums

Exercise

Solution

Let's Pair!

Rustlings

https://github.com/rust-lang/rustlings

Rustlings

Structs and Enums

$ rustlings run enums1 
$ rustlings hint enums1
Complete Exercises 
Enum 1, 2, 3

Rustlings: Live! Enum edition

By shortdiv

Rustlings: Live! Enum edition

In this deck we cover enums!

  • 605