use std::collections::HashMap; macro_rules! cfg { [$( {$arg:literal, $var:ident, $t:ty $(,)? } $(,)? )* ] => { struct Config { args: HashMap, } impl Config { $( pub fn $var (&self) -> Option<$t> { self.args.get($arg).map(|e| e.parse::<$t>().unwrap()) } )* } }; } cfg! [ { "test", test, i32 }, ]; fn main() { let mut map = HashMap::new(); map.insert("test".to_string(),"12".to_string()); let cfg = Config{ args: map, }; println!("{}", cfg.test().unwrap()); }