I have the following Enum with String values and want to prevent a hard coded into_vec()
function. What should I do to optimize it?
pub enum Topics {
FAVORITE_ANIMAL,
LEISURE_I,
LEISURE_II,
LEISURE_III,
ONLINE_GAMING_FAN,
DIY,
PLASTIC,
NUTRITION
}
impl Topics {
pub fn as_str(&self) -> &'static str {
match self {
Topics::FAVORITE_ANIMAL => "Favorite Animal",
Topics::LEISURE_I => "Leisure I",
Topics::LEISURE_II => "Leisure II",
Topics::LEISURE_III => "Leisure III",
Topics::ONLINE_GAMING_FAN => "Online Gaming Fan",
Topics::DIY => "Do it yourself",
Topics::PLASTIC => "Plastic",
Topics::NUTRITION => "Nutrition"
}
}
// How can I optimize this function to prevent the hardcoded variant
pub fn into_vec(&self) -> Vec<&'static str> {
let mut vec = Vec::new();
let fav_an = Topics::FAVORITE_ANIMAL.as_str();
vec.push(fav_an);
let leisure_1 = Topics::LEISURE_I.as_str();
vec.push(leisure_1);
vec
}
}