Published on
2 min read

Input to Enum in Rust

What are trying to accomplish?

THhis excersize is to convert user input to an enum in Rust. We will verify user input against the following keywords:
  • Off
  • Sleep
  • Reboot
  • Shutdown
  • Hibernate
If the user enters one of the keywords, a message should be printed to the console indicating which action will be taken
Example: If the user types in "shutdown" a message should display such as "shutting down"

Step 1: Create the enum

We will create the enum to hold the keywords.
enum Action {
    Off,
    Sleep,
    Reboot,
    Shutdown,
    Hibernate,
}

Step 2: Create the function to print the message

Now we will create the function that takes the enum as an argument and prints the appropriate message. Don't forget to borrow the enum in the argument.

print.rs

fn print_message(action: &Action) {
    match action {
        Action::Off => println!("Turning off"),
        Action::Sleep => println!("Sleeping"),
        Action::Reboot => println!("Rebooting"),
        Action::Shutdown => println!("Shutting down"),
        Action::Hibernate => println!("Hibernating"),
    }
}

Step 3: Using impl to convert the user input to the enum

Instead of creating another function for this conversion, we will create an impl block for the enum.
We can create the impl after the enum because the enum is defined before the impl, like so.
impl Action {
    fn from(input: &str) -> Action {
        match input {
            "off" => Action::Off,
            "sleep" => Action::Sleep,
            "reboot" => Action::Reboot,
            "shutdown" => Action::Shutdown,
            "hibernate" => Action::Hibernate,
        }
    }
}

Step 4: Getting the user input and printing the message

The last piece of the puzzle is to get the user input and print the message. To make this work we are going to need the std::io lang:rust module.
Although this module is out of the scope of this article, click std::io::stdin to read more about it.

read_input.rs

fn read_input() -> String {
    let mut input = String::new();
    std::io::stdin().read_line(&mut input).expect("Failed to read line");
    input.trim().to_lowercase()
}