Created
January 24, 2016 02:30
-
-
Save anonymous/aa5452d7d48d376d685d to your computer and use it in GitHub Desktop.
Shared via Rust Playground
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
#![feature(type_macros)] | |
#[derive(Debug)] | |
struct Tuple<T, N> { | |
data: T, | |
next: N, | |
} | |
macro_rules! tuple { | |
($y:ty, $($x:ty),+) => { | |
Tuple<$y, tuple!($($x),+)> | |
}; | |
($y:ty) => {$y}; | |
} | |
macro_rules! mk_tuple { | |
($y:expr, $($x:expr),+) => { | |
Tuple{ data: $y, next: mk_tuple!($($x),+) } | |
}; | |
($y:expr) => {$y}; | |
} | |
#[derive(Debug)] | |
struct Foo(u8); | |
#[derive(Debug)] | |
struct Bar(u8); | |
#[derive(Debug)] | |
struct Baz(u8); | |
fn main() { | |
let x = mk_tuple!(Foo(3), Bar(4), Baz(5)); | |
print_my_tuple(x); | |
} | |
fn print_my_tuple(tup: tuple!(Foo, Bar, Baz)) { // can specify it as a type too | |
println!("{:?}", tup) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment