Dealing with a `match` statement in a function that returns an Enum

I'm pretty sure there's a simple solution to my problem, but I'm not seeing it. Here is some simplified code similar to what I'm trying to get working:

enum Flower {
    Daffodil(String),
    Tulip(i32),
    Rose(char),
}

fn main()
{
    garden();
    println!( "\n And we're done \n" );
}


fn garden() -> Flower {
    let choice = 2;
    match choice {
        1 => {
            println!("Daffodil");
            Daffodil("This is a String".to_string())
        }
        2 => {
            println!("\n Tulip");
            Tulip(12)
        }
        3 => {
            println!("Rose");
            Rose('R')
        }
        _ => {}
    }
}

The compiler complains with this error:

   |
35 |         _ => {}
   |              ^^ expected `Flower`, found `()`

How do I fix this?

Either return an Option<Flower> or if you're sure another choice can't be made use unreachable!()

1 Like

unreachable!() -- I hadn't yet run across that macro. Thanks! :>)

You should probably handle the case of an unforeseen choice though ^^ that will just crash the program if the user makes a choice that isn't 1, 2 or 3

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.