Skip to content

Instantly share code, notes, and snippets.

@greyblake
Created July 4, 2017 23:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save greyblake/bb88422ca4b8927d451875fa3e46bbea to your computer and use it in GitHub Desktop.
Save greyblake/bb88422ca4b8927d451875fa3e46bbea to your computer and use it in GitHub Desktop.
macro_rules! list_enum {
( $type:ident, { $($el:ident),* } ) => {
#[derive(Debug,Clone,Copy)]
enum $type{
$($el),*
}
impl $type {
pub fn list() -> &'static[$type] {
const LIST : &'static[$type] = &[$($type::$el),*];
LIST
}
}
}
}
list_enum!(Brand, {
Dell,
Asus,
Sony,
Panosonic,
Apple
});
fn main() {
println!("Brand::list() = {:?}", Brand::list());
for brand in Brand::list() {
println!("brand = {:?}", brand);
}
}
// Brand::list() = [Dell, Asus, Sony, Panosonic, Apple]
// brand = Dell
// brand = Asus
// brand = Sony
// brand = Panosonic
// brand = Apple
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment