add macros examples

pull/57/head
QuietMisdreavus 8 years ago
parent 2079503b21
commit 248d87a2dd

@ -0,0 +1,62 @@
// Make me compile! Scroll down for hints :)
macro_rules! my_macro {
() => {
println!("Check out my macro!");
};
}
fn main() {
my_macro();
}
// When you call a macro, you need to add something special compared to a regular function call. If
// you're stuck, take a look at what's inside `my_macro`.

@ -0,0 +1,72 @@
// Make me compile! Scroll down for hints :)
fn main() {
my_macro!();
}
macro_rules! my_macro {
() => {
println!("Check out my macro!");
};
}
// Macros don't quite play by the same rules as the rest of Rust, in terms of what's available
// where.
// Unlike other things in Rust, the order of "where you define a macro" versus "where you use it"
// actually matters.

@ -0,0 +1,74 @@
// Make me compile, without taking the macro out of the module! Scroll down for hints :)
mod macros {
macro_rules! my_macro {
() => {
println!("Check out my macro!");
};
}
}
fn main() {
my_macro!();
}
// In order to use a macro outside of its module, you need to do something special to the module to
// lift the macro out into its parent.
// The same trick also works on "extern crate" statements for crates that have exported macros, if
// you've seen any of those around.
Loading…
Cancel
Save