Rust 宏魔法之默认参数
use std::any::Any; macro_rules! requests{ ( $($key:expr=> $value:expr);* $(;)? ) => { let mut url: Box<dyn Any> = Box::new(""); let mut method: Box<dyn Any> = Box::new(""); let mut timeout: Box<dyn Any> = Box::new(0); $( match $key { "url" => { url = Box::new($value); }, "method" => { method = Box::new($value); }, "timeout" => { timeout = Box::new($value); }, _ => { panic!("Error key: {:?}", $key); } } )* let mut url: &str = url.downcast_ref::<&str>().unwrap(); let mut method: &str = method.downcast_ref::<&str>().unwrap(); let mut timeout: u64 = *timeout.downcast_ref::<i32>().unwrap() as u64; println!("url-> {}", url); println!("method-> {}", method); println!("timeout-> {}", timeout); } } fn main() { requests! { "url"=> "https://www.baidu.com/"; "method"=> "GET"; "timeout"=> 10; }; }