Created
August 15, 2017 23:35
-
-
Save gyu-don/d89e084531058b3db02d23d78e43a47e to your computer and use it in GitHub Desktop.
Rust macro for making variable from parsed string.
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! let_from_str { | |
($($a:ident : $t:ty),+ = $s:expr) => { | |
$(let $a: $t;)+ | |
{ | |
let mut _it = $s.split_whitespace(); | |
$($a = _it.next().unwrap().parse().unwrap();)+ | |
assert!(_it.next().is_none()); | |
} | |
}; | |
} | |
fn main() { | |
let_from_str!(a:i32 = "123"); | |
println!("{}", a); | |
let_from_str!(a:i32, b:f64, c:String = "1 2.3 45"); | |
println!("{} {} {}", a, b, c); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment