Skip to content

Instantly share code, notes, and snippets.

@psychoss
Last active January 15, 2016 12:48
Show Gist options
  • Save psychoss/0ac070397a690859d0d0 to your computer and use it in GitHub Desktop.
Save psychoss/0ac070397a690859d0d0 to your computer and use it in GitHub Desktop.
Something about Rust Macros
macro_rules! test(
($x:ident) => ({
let z = concat_idents!(hello_, $x);
z();
})
)
fn hello_world() { ... }
fn main() {
test!(world);
}
macro_rules! iterator{
($ty:ty, $ident:ident;
$($state_ident:ident: $state_ty:ty), *;
$next:expr) => (
struct $ident {
$($state_ident: $state_ty), *
}
impl Iterator for $ident {
type Item = $ty;
fn next(&mut self) -> Option<$ty> {
$next(self)
}
}
);
}
iterator!(i32, TestIterator; index: i32; |me: &mut TestIterator| {
let value = Some(me.index);
me.index += 1;
value
});
fn main() {}
macro_rules! sorted_vec {
($($x:tt)*) => {
{
let mut v = vec![$($x)*];
v.sort();
v
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment