Skip to content

Instantly share code, notes, and snippets.

Created January 24, 2016 02:30
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/aa5452d7d48d376d685d to your computer and use it in GitHub Desktop.
Save anonymous/aa5452d7d48d376d685d to your computer and use it in GitHub Desktop.
Shared via Rust Playground
#![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