Created
July 26, 2019 00:15
-
-
Save max6cn/c78fc6f4faf4e000bc11a7494445b4b5 to your computer and use it in GitHub Desktop.
Reading tuple from a line
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
// Reading tuple from a line | |
// Example : read_tuple( "1 ab 3" | |
// ,(i32, String, i32)) | |
// Expected : (1, "ab", 3) | |
macro_rules! read_tuple { | |
( | |
$input:ident, ($($t:ty),*) | |
) => { | |
{ | |
let mut ws = $input.trim().split(" "); | |
( | |
$(ws.next().unwrap().parse::<$t>(),)* | |
) | |
} | |
} | |
} | |
fn main() { | |
let _x = "1 ab 3"; | |
let a = read_tuple!( _x, (i32, String, i32)); | |
print!("{:?}",a); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
if need a default value in case it goes wrong, then