Last active
January 15, 2016 12:48
-
-
Save psychoss/0ac070397a690859d0d0 to your computer and use it in GitHub Desktop.
Something about Rust Macros
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
macro_rules! test( | |
($x:ident) => ({ | |
let z = concat_idents!(hello_, $x); | |
z(); | |
}) | |
) | |
fn hello_world() { ... } | |
fn main() { | |
test!(world); | |
} |
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
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() {} |
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
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