Created
July 4, 2017 23:10
-
-
Save greyblake/bb88422ca4b8927d451875fa3e46bbea to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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