Reputation: 5098
I have a wierdest case yet on my hands. I have an enum that I convert to a string. The enum provided is eg. Green so the string returned from the match is "text-success". Simple right? Turns out that the string returned is always "" regardless of how I obtain it. This makes no sense to me. Please help!
fn bootstrap_table_color (e: Color) -> String {
let s: String = match &e {
White => "".to_string(),
Blue => String::from("table-info"),
Green => "table-success".to_string(),
Yellow => "table-warning".to_string(),
Red => "table-danger".to_string(),
};
println!("bootstrap_table_color ({:?}) -> {:?}", e, s);
return s;
}
bootstrap_table_color (Blue) -> ""
bootstrap_table_color (Green) -> ""
Upvotes: 1
Views: 528
Reputation: 382150
That's because all possible values match the White
variable arm.
You could see it by doing
let s: String = match &e {
White => format!("White={:?}", White),
}
The clean solution is to prefix arm values with your enum name:
let s = match &e {
Color::White => "".to_string(),
Color::Blue => String::from("table-info"),
Color::Green => "table-success".to_string(),
Color::Yellow => "table-warning".to_string(),
Color::Red => "table-danger".to_string(),
};
Another solution would be to do a use Color::*;
but you would be vulnerable to typos or changes.
Upvotes: 4