Initial commit
This commit is contained in:
14
exercises/21_macros/README.md
Normal file
14
exercises/21_macros/README.md
Normal file
@@ -0,0 +1,14 @@
|
||||
# Macros
|
||||
|
||||
Rust's macro system is very powerful, but also kind of difficult to wrap your
|
||||
head around. We're not going to teach you how to write your own fully-featured
|
||||
macros. Instead, we'll show you how to use and create them.
|
||||
|
||||
If you'd like to learn more about writing your own macros, the
|
||||
[macrokata](https://github.com/tfpk/macrokata) project has a similar style
|
||||
of exercises to Rustlings, but is all about learning to write Macros.
|
||||
|
||||
## Further information
|
||||
|
||||
- [Macros](https://doc.rust-lang.org/book/ch19-06-macros.html)
|
||||
- [The Little Book of Rust Macros](https://veykril.github.io/tlborm/)
|
||||
10
exercises/21_macros/macros1.rs
Normal file
10
exercises/21_macros/macros1.rs
Normal file
@@ -0,0 +1,10 @@
|
||||
macro_rules! my_macro {
|
||||
() => {
|
||||
println!("Check out my macro!");
|
||||
};
|
||||
}
|
||||
|
||||
fn main() {
|
||||
// TODO: Fix the macro call.
|
||||
my_macro!();
|
||||
}
|
||||
10
exercises/21_macros/macros2.rs
Normal file
10
exercises/21_macros/macros2.rs
Normal file
@@ -0,0 +1,10 @@
|
||||
// TODO: Fix the compiler error by moving the whole definition of this macro.
|
||||
macro_rules! my_macro {
|
||||
() => {
|
||||
println!("Check out my macro!");
|
||||
};
|
||||
}
|
||||
|
||||
fn main() {
|
||||
my_macro!();
|
||||
}
|
||||
17
exercises/21_macros/macros3.rs
Normal file
17
exercises/21_macros/macros3.rs
Normal file
@@ -0,0 +1,17 @@
|
||||
// TODO: Fix the compiler error without taking the macro definition out of this
|
||||
// module.
|
||||
// #[macro_use]
|
||||
mod macros {
|
||||
macro_rules! my_macro {
|
||||
() => {
|
||||
println!("Check out my macro!");
|
||||
};
|
||||
}
|
||||
pub(crate) use my_macro;
|
||||
}
|
||||
|
||||
use macros::my_macro;
|
||||
|
||||
fn main() {
|
||||
my_macro!();
|
||||
}
|
||||
15
exercises/21_macros/macros4.rs
Normal file
15
exercises/21_macros/macros4.rs
Normal file
@@ -0,0 +1,15 @@
|
||||
// TODO: Fix the compiler error by adding one or two characters.
|
||||
#[rustfmt::skip]
|
||||
macro_rules! my_macro {
|
||||
() => {
|
||||
println!("Check out my macro!");
|
||||
};
|
||||
($val:expr) => {
|
||||
println!("Look at this other macro: {}", $val);
|
||||
};
|
||||
}
|
||||
|
||||
fn main() {
|
||||
my_macro!();
|
||||
my_macro!(7777);
|
||||
}
|
||||
Reference in New Issue
Block a user