Skip to content

Instantly share code, notes, and snippets.

@gyu-don
Created August 15, 2017 23:35
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 gyu-don/d89e084531058b3db02d23d78e43a47e to your computer and use it in GitHub Desktop.
Save gyu-don/d89e084531058b3db02d23d78e43a47e to your computer and use it in GitHub Desktop.
Rust macro for making variable from parsed string.
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