test4 macro module

This commit is contained in:
zhihanz 2020-05-03 23:46:01 -07:00
parent d8429301db
commit 30286f3b20
7 changed files with 16 additions and 18 deletions

View File

@ -1,8 +1,8 @@
// macros1.rs // macros1.rs
// Make me compile! Execute `rustlings hint macros1` for hints :) // Make me compile! Execute `rustlings hint macros1` for hints :)
// I AM NOT DONE // I am a rooky in metaprogramming
// strongly recommend to read and practice on https://danielkeep.github.io/tlborm/book/README.html
macro_rules! my_macro { macro_rules! my_macro {
() => { () => {
println!("Check out my macro!"); println!("Check out my macro!");
@ -10,5 +10,5 @@ macro_rules! my_macro {
} }
fn main() { fn main() {
my_macro(); my_macro!();
} }

View File

@ -1,12 +1,11 @@
// macros2.rs // macros2.rs
// Make me compile! Execute `rustlings hint macros2` for hints :) // Make me compile! Execute `rustlings hint macros2` for hints :)
// I AM NOT DONE
fn main() { fn main() {
my_macro!(); my_macro!();
} }
#[macro_export]
macro_rules! my_macro { macro_rules! my_macro {
() => { () => {
println!("Check out my macro!"); println!("Check out my macro!");

View File

@ -2,9 +2,9 @@
// Make me compile, without taking the macro out of the module! // Make me compile, without taking the macro out of the module!
// Execute `rustlings hint macros3` for hints :) // Execute `rustlings hint macros3` for hints :)
// I AM NOT DONE //export works even with no pub mod
mod macros { mod macros {
#[macro_export]
macro_rules! my_macro { macro_rules! my_macro {
() => { () => {
println!("Check out my macro!"); println!("Check out my macro!");

View File

@ -1,15 +1,14 @@
// macros4.rs // macros4.rs
// Make me compile! Execute `rustlings hint macros4` for hints :) // Make me compile! Execute `rustlings hint macros4` for hints :)
// I AM NOT DONE
macro_rules! my_macro { macro_rules! my_macro {
() => { () => {
println!("Check out my macro!"); println!("Check out my macro!");
} };
($val:expr) => { ($val:expr) => {
println!("Look at this other macro: {}", $val); println!("Look at this other macro: {}", $val);
} };
} }
fn main() { fn main() {

View File

@ -1,10 +1,9 @@
// modules1.rs // modules1.rs
// Make me compile! Execute `rustlings hint modules1` for hints :) // Make me compile! Execute `rustlings hint modules1` for hints :)
// I AM NOT DONE
mod sausage_factory { pub mod sausage_factory {
fn make_sausage() { pub fn make_sausage() {
println!("sausage!"); println!("sausage!");
} }
} }

View File

@ -1,11 +1,10 @@
// modules2.rs // modules2.rs
// Make me compile! Execute `rustlings hint modules2` for hints :) // Make me compile! Execute `rustlings hint modules2` for hints :)
// I AM NOT DONE
mod delicious_snacks { pub mod delicious_snacks {
use self::fruits::PEAR as fruit; pub use self::fruits::PEAR as fruit;
use self::veggies::CUCUMBER as veggie; pub use self::veggies::CUCUMBER as veggie;
mod fruits { mod fruits {
pub const PEAR: &'static str = "Pear"; pub const PEAR: &'static str = "Pear";

View File

@ -5,7 +5,9 @@
// Write a macro that passes the test! No hints this time, you can do it! // Write a macro that passes the test! No hints this time, you can do it!
// I AM NOT DONE macro_rules! my_macro {
($str: expr) => {format!("Hello {}", $str)};
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {