From e3266d06e8ebaf8813951dd599b30f31a8131336 Mon Sep 17 00:00:00 2001 From: kulapoo Date: Mon, 27 Jan 2025 14:11:40 +0800 Subject: [PATCH 01/10] init commit --- exercises/01_my_first_macro/main.rs | 6 +++++- exercises/02_numbers/main.rs | 12 ++++++++++++ exercises/03_literal_variables/main.rs | 9 +++++++++ exercises/03_literal_variables/solutions/main.rs | 2 +- 4 files changed, 27 insertions(+), 2 deletions(-) diff --git a/exercises/01_my_first_macro/main.rs b/exercises/01_my_first_macro/main.rs index a2b2d12..9931ac5 100644 --- a/exercises/01_my_first_macro/main.rs +++ b/exercises/01_my_first_macro/main.rs @@ -6,7 +6,11 @@ fn show_output() { ////////// DO NOT CHANGE ABOVE HERE ///////// // TODO: create `show_output!()` macro. - +macro_rules! show_output { + () => { + show_output() + }; +} ////////// DO NOT CHANGE BELOW HERE ///////// fn main() { diff --git a/exercises/02_numbers/main.rs b/exercises/02_numbers/main.rs index 9cc8355..885803a 100644 --- a/exercises/02_numbers/main.rs +++ b/exercises/02_numbers/main.rs @@ -5,7 +5,19 @@ fn print_result(num: i32) { ////////// DO NOT CHANGE ABOVE HERE ///////// // TODO: create `num!()` macro. +macro_rules! num { + (one) => { + 1 + }; + (two) => { + 2 + }; + + (three) => { + 3 + } +} ////////// DO NOT CHANGE BELOW HERE ///////// fn main() { diff --git a/exercises/03_literal_variables/main.rs b/exercises/03_literal_variables/main.rs index 9e8ddf0..53fd132 100644 --- a/exercises/03_literal_variables/main.rs +++ b/exercises/03_literal_variables/main.rs @@ -7,6 +7,15 @@ fn print_result(num: i32) { // TODO: create `math!()` macro. ////////// DO NOT CHANGE BELOW HERE ///////// +macro_rules! math { + ($a:literal plus $b:literal) => { + $a + $b + }; + (square $a:literal) => { + $a * $a + }; +} + fn main() { print_result(math!(3 plus 5)); diff --git a/exercises/03_literal_variables/solutions/main.rs b/exercises/03_literal_variables/solutions/main.rs index 58a8e83..de32765 100644 --- a/exercises/03_literal_variables/solutions/main.rs +++ b/exercises/03_literal_variables/solutions/main.rs @@ -13,7 +13,7 @@ macro_rules! math { }; } -////////// DO NOT CHANGE BELOW HERE ///////// +////////// DO NOT CHANGE BELOW HERE ///////// fn main() { print_result(math!(3 plus 5)); From c23190397331a30238168977b7e15e0f75aaf288 Mon Sep 17 00:00:00 2001 From: kulapoo Date: Mon, 27 Jan 2025 14:12:21 +0800 Subject: [PATCH 02/10] rvrt --- exercises/03_literal_variables/solutions/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/exercises/03_literal_variables/solutions/main.rs b/exercises/03_literal_variables/solutions/main.rs index de32765..58a8e83 100644 --- a/exercises/03_literal_variables/solutions/main.rs +++ b/exercises/03_literal_variables/solutions/main.rs @@ -13,7 +13,7 @@ macro_rules! math { }; } -////////// DO NOT CHANGE BELOW HERE ///////// +////////// DO NOT CHANGE BELOW HERE ///////// fn main() { print_result(math!(3 plus 5)); From a6ee3070f84798fe2d59086a446ac55af565a0aa Mon Sep 17 00:00:00 2001 From: kulapoo Date: Tue, 28 Jan 2025 23:43:24 +0800 Subject: [PATCH 03/10] update --- exercises/04_expression_variables/main.rs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/exercises/04_expression_variables/main.rs b/exercises/04_expression_variables/main.rs index 1258e12..48c68a1 100644 --- a/exercises/04_expression_variables/main.rs +++ b/exercises/04_expression_variables/main.rs @@ -7,6 +7,15 @@ fn print_result(num: i32) { // TODO: create `math!()` macro. ////////// DO NOT CHANGE BELOW HERE ///////// +macro_rules! math { + ($lhs: expr, plus, $rhs: expr) => { + $lhs + $rhs + }; + (square $arg: expr) => { + $arg * $arg + }; +} + fn main() { let var = 5; From 719cb401ec53d89c9c15cd9267a8d27152726683 Mon Sep 17 00:00:00 2001 From: kulapoo Date: Wed, 29 Jan 2025 15:20:17 +0800 Subject: [PATCH 04/10] solve complex macros example --- exercises/04_expression_variables/main.rs | 2 +- exercises/05_more_complex_example/main.rs | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/exercises/04_expression_variables/main.rs b/exercises/04_expression_variables/main.rs index 48c68a1..bd4c802 100644 --- a/exercises/04_expression_variables/main.rs +++ b/exercises/04_expression_variables/main.rs @@ -13,7 +13,7 @@ macro_rules! math { }; (square $arg: expr) => { $arg * $arg - }; + } } diff --git a/exercises/05_more_complex_example/main.rs b/exercises/05_more_complex_example/main.rs index ed2dafa..ebb87db 100644 --- a/exercises/05_more_complex_example/main.rs +++ b/exercises/05_more_complex_example/main.rs @@ -16,6 +16,22 @@ impl Coordinate { // TODO: Create `for_2d!` macro here. +macro_rules! for_2d { + + ($row: ident <$row_type: ty> in $row_range: expr, $col: ident <$col_type: ty> in $col_range: expr, $block: block) => { + for $row in $row_range { + let $row: $row_type = $row; + + for $col in $col_range { + let $col: $col_type = $col; + + $block + } + + } + }; +} + ////////// DO NOT CHANGE BELOW HERE ///////// fn main() { From cb22d1780587017ffde97e07997c3192166557b8 Mon Sep 17 00:00:00 2001 From: kulapoo Date: Fri, 31 Jan 2025 02:23:20 +0800 Subject: [PATCH 05/10] solve repitition --- exercises/06_repetition/main.rs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/exercises/06_repetition/main.rs b/exercises/06_repetition/main.rs index 890e627..ca2c082 100644 --- a/exercises/06_repetition/main.rs +++ b/exercises/06_repetition/main.rs @@ -5,11 +5,20 @@ fn print_success() { ////////// DO NOT CHANGE ABOVE HERE ///////// // TODO: create `if_any!()` macro. +macro_rules! if_any { + ($($predicate: expr),+;$block: block) => { + if $($predicate)||+ $block + }; +} ////////// DO NOT CHANGE BELOW HERE ///////// + + fn main() { + if_any!(false, 0 == 1, true; { print_success(); - }) + }); + } From bbb6fb6ec4f44b58f93fe7ed2eeb16cf3163092a Mon Sep 17 00:00:00 2001 From: kulapoo Date: Fri, 31 Jan 2025 15:32:34 +0800 Subject: [PATCH 06/10] solve - more repeatition exercise --- .idea/.gitignore | 8 ++++++++ .idea/macrokata.iml | 11 +++++++++++ .idea/modules.xml | 8 ++++++++ .idea/vcs.xml | 6 ++++++ exercises/07_more_repetition/main.rs | 26 ++++++++++++++++++++++++++ 5 files changed, 59 insertions(+) create mode 100644 .idea/.gitignore create mode 100644 .idea/macrokata.iml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/macrokata.iml b/.idea/macrokata.iml new file mode 100644 index 0000000..cf84ae4 --- /dev/null +++ b/.idea/macrokata.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..9156e9b --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/exercises/07_more_repetition/main.rs b/exercises/07_more_repetition/main.rs index 2d18628..c6f31aa 100644 --- a/exercises/07_more_repetition/main.rs +++ b/exercises/07_more_repetition/main.rs @@ -7,7 +7,33 @@ fn print_hashmap(hashmap: &HashMap<&str, &str>) { ////////// DO NOT CHANGE ABOVE HERE ///////// // TODO: create `hashmap!()` macro. +macro_rules! hashmap { + ( + $( + $key: expr => $val: expr, + ) + * + ) => { + { + let mut hm = HashMap::new(); + $( + hm.insert($key, $val); + )* + + hm + } + }; +} +// macro_rules! hashmap { +// ( $($k:literal => $v:expr,)* ) => { +// { +// let mut hm = HashMap::new(); +// $( hm.insert($k, $v); )* +// hm +// } +// } +// } ////////// DO NOT CHANGE BELOW HERE ///////// fn main() { From 41c4c03685ab800a85f97c2834e36c5fdd37bd9b Mon Sep 17 00:00:00 2001 From: kulapoo Date: Fri, 31 Jan 2025 17:01:34 +0800 Subject: [PATCH 07/10] solve nested repition --- exercises/08_nested_repetition/main.rs | 27 ++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/exercises/08_nested_repetition/main.rs b/exercises/08_nested_repetition/main.rs index 71418e9..a6950a1 100644 --- a/exercises/08_nested_repetition/main.rs +++ b/exercises/08_nested_repetition/main.rs @@ -8,6 +8,33 @@ fn print_vec(vec: &Vec) { ////////// DO NOT CHANGE BELOW HERE ///////// +macro_rules! graph { + ( + $( + $key: literal-> ( + $($val: expr),* // "literal" much simpler + ); + ) + * + ) => { + + { + let mut vec = Vec::new(); + $( + $( + vec.push(( + $key, + $val + )); + )* + )* + + vec + } + + }; +} + #[allow(clippy::vec_init_then_push)] fn main() { let my_graph = graph!( From cce62614ff2baf48cb189a8b97c6c967dbc27c55 Mon Sep 17 00:00:00 2001 From: kulapoo Date: Sat, 22 Mar 2025 14:18:42 +0800 Subject: [PATCH 08/10] reset --- exercises/01_my_first_macro/main.rs | 6 +---- exercises/02_numbers/main.rs | 12 ---------- exercises/03_literal_variables/main.rs | 9 -------- exercises/04_expression_variables/main.rs | 9 -------- exercises/05_more_complex_example/main.rs | 16 -------------- exercises/06_repetition/main.rs | 11 +-------- exercises/07_more_repetition/main.rs | 26 ---------------------- exercises/08_nested_repetition/main.rs | 27 ----------------------- 8 files changed, 2 insertions(+), 114 deletions(-) diff --git a/exercises/01_my_first_macro/main.rs b/exercises/01_my_first_macro/main.rs index 9931ac5..a2b2d12 100644 --- a/exercises/01_my_first_macro/main.rs +++ b/exercises/01_my_first_macro/main.rs @@ -6,11 +6,7 @@ fn show_output() { ////////// DO NOT CHANGE ABOVE HERE ///////// // TODO: create `show_output!()` macro. -macro_rules! show_output { - () => { - show_output() - }; -} + ////////// DO NOT CHANGE BELOW HERE ///////// fn main() { diff --git a/exercises/02_numbers/main.rs b/exercises/02_numbers/main.rs index 885803a..9cc8355 100644 --- a/exercises/02_numbers/main.rs +++ b/exercises/02_numbers/main.rs @@ -5,19 +5,7 @@ fn print_result(num: i32) { ////////// DO NOT CHANGE ABOVE HERE ///////// // TODO: create `num!()` macro. -macro_rules! num { - (one) => { - 1 - }; - (two) => { - 2 - }; - - (three) => { - 3 - } -} ////////// DO NOT CHANGE BELOW HERE ///////// fn main() { diff --git a/exercises/03_literal_variables/main.rs b/exercises/03_literal_variables/main.rs index 53fd132..9e8ddf0 100644 --- a/exercises/03_literal_variables/main.rs +++ b/exercises/03_literal_variables/main.rs @@ -7,15 +7,6 @@ fn print_result(num: i32) { // TODO: create `math!()` macro. ////////// DO NOT CHANGE BELOW HERE ///////// -macro_rules! math { - ($a:literal plus $b:literal) => { - $a + $b - }; - (square $a:literal) => { - $a * $a - }; -} - fn main() { print_result(math!(3 plus 5)); diff --git a/exercises/04_expression_variables/main.rs b/exercises/04_expression_variables/main.rs index bd4c802..1258e12 100644 --- a/exercises/04_expression_variables/main.rs +++ b/exercises/04_expression_variables/main.rs @@ -7,15 +7,6 @@ fn print_result(num: i32) { // TODO: create `math!()` macro. ////////// DO NOT CHANGE BELOW HERE ///////// -macro_rules! math { - ($lhs: expr, plus, $rhs: expr) => { - $lhs + $rhs - }; - (square $arg: expr) => { - $arg * $arg - } -} - fn main() { let var = 5; diff --git a/exercises/05_more_complex_example/main.rs b/exercises/05_more_complex_example/main.rs index ebb87db..ed2dafa 100644 --- a/exercises/05_more_complex_example/main.rs +++ b/exercises/05_more_complex_example/main.rs @@ -16,22 +16,6 @@ impl Coordinate { // TODO: Create `for_2d!` macro here. -macro_rules! for_2d { - - ($row: ident <$row_type: ty> in $row_range: expr, $col: ident <$col_type: ty> in $col_range: expr, $block: block) => { - for $row in $row_range { - let $row: $row_type = $row; - - for $col in $col_range { - let $col: $col_type = $col; - - $block - } - - } - }; -} - ////////// DO NOT CHANGE BELOW HERE ///////// fn main() { diff --git a/exercises/06_repetition/main.rs b/exercises/06_repetition/main.rs index ca2c082..890e627 100644 --- a/exercises/06_repetition/main.rs +++ b/exercises/06_repetition/main.rs @@ -5,20 +5,11 @@ fn print_success() { ////////// DO NOT CHANGE ABOVE HERE ///////// // TODO: create `if_any!()` macro. -macro_rules! if_any { - ($($predicate: expr),+;$block: block) => { - if $($predicate)||+ $block - }; -} ////////// DO NOT CHANGE BELOW HERE ///////// - - fn main() { - if_any!(false, 0 == 1, true; { print_success(); - }); - + }) } diff --git a/exercises/07_more_repetition/main.rs b/exercises/07_more_repetition/main.rs index c6f31aa..2d18628 100644 --- a/exercises/07_more_repetition/main.rs +++ b/exercises/07_more_repetition/main.rs @@ -7,33 +7,7 @@ fn print_hashmap(hashmap: &HashMap<&str, &str>) { ////////// DO NOT CHANGE ABOVE HERE ///////// // TODO: create `hashmap!()` macro. -macro_rules! hashmap { - ( - $( - $key: expr => $val: expr, - ) - * - ) => { - { - let mut hm = HashMap::new(); - $( - hm.insert($key, $val); - )* - - hm - } - }; -} -// macro_rules! hashmap { -// ( $($k:literal => $v:expr,)* ) => { -// { -// let mut hm = HashMap::new(); -// $( hm.insert($k, $v); )* -// hm -// } -// } -// } ////////// DO NOT CHANGE BELOW HERE ///////// fn main() { diff --git a/exercises/08_nested_repetition/main.rs b/exercises/08_nested_repetition/main.rs index a6950a1..71418e9 100644 --- a/exercises/08_nested_repetition/main.rs +++ b/exercises/08_nested_repetition/main.rs @@ -8,33 +8,6 @@ fn print_vec(vec: &Vec) { ////////// DO NOT CHANGE BELOW HERE ///////// -macro_rules! graph { - ( - $( - $key: literal-> ( - $($val: expr),* // "literal" much simpler - ); - ) - * - ) => { - - { - let mut vec = Vec::new(); - $( - $( - vec.push(( - $key, - $val - )); - )* - )* - - vec - } - - }; -} - #[allow(clippy::vec_init_then_push)] fn main() { let my_graph = graph!( From d8d05b429e11810161da4382133e1cd0cac7a99a Mon Sep 17 00:00:00 2001 From: kulapoo Date: Sun, 23 Mar 2025 01:40:42 +0800 Subject: [PATCH 09/10] update --- .zed/settings.json | 11 ++++++++++ exercises/01_my_first_macro/main.rs | 6 +++++- exercises/02_numbers/main.rs | 12 ++++++++++- exercises/03_literal_variables/main.rs | 8 +++++++ exercises/04_expression_variables/main.rs | 8 +++++++ exercises/05_more_complex_example/main.rs | 16 +++++++++++++- exercises/06_repetition/main.rs | 6 +++++- exercises/07_more_repetition/main.rs | 10 ++++++++- exercises/08_nested_repetition/main.rs | 13 +++++++++++- exercises/09_ambiguity_and_ordering/main.rs | 23 +++++++++++---------- exercises/10_macros_calling_macros/main.rs | 8 ++++++- 11 files changed, 103 insertions(+), 18 deletions(-) create mode 100644 .zed/settings.json diff --git a/.zed/settings.json b/.zed/settings.json new file mode 100644 index 0000000..85c270d --- /dev/null +++ b/.zed/settings.json @@ -0,0 +1,11 @@ +// Folder-specific settings +// +// For a full list of overridable settings, and general information on folder-specific settings, +// see the documentation: https://zed.dev/docs/configuring-zed#settings-files +{ + "languages": { + "Rust": { + "show_edit_predictions": false + } + } +} diff --git a/exercises/01_my_first_macro/main.rs b/exercises/01_my_first_macro/main.rs index a2b2d12..9931ac5 100644 --- a/exercises/01_my_first_macro/main.rs +++ b/exercises/01_my_first_macro/main.rs @@ -6,7 +6,11 @@ fn show_output() { ////////// DO NOT CHANGE ABOVE HERE ///////// // TODO: create `show_output!()` macro. - +macro_rules! show_output { + () => { + show_output() + }; +} ////////// DO NOT CHANGE BELOW HERE ///////// fn main() { diff --git a/exercises/02_numbers/main.rs b/exercises/02_numbers/main.rs index 9cc8355..5f6c725 100644 --- a/exercises/02_numbers/main.rs +++ b/exercises/02_numbers/main.rs @@ -5,7 +5,17 @@ fn print_result(num: i32) { ////////// DO NOT CHANGE ABOVE HERE ///////// // TODO: create `num!()` macro. - +macro_rules! num { + (one) => { + 1 + }; + (two) => { + 2 + }; + (three) => { + 3 + }; +} ////////// DO NOT CHANGE BELOW HERE ///////// fn main() { diff --git a/exercises/03_literal_variables/main.rs b/exercises/03_literal_variables/main.rs index 9e8ddf0..7f7aa33 100644 --- a/exercises/03_literal_variables/main.rs +++ b/exercises/03_literal_variables/main.rs @@ -5,6 +5,14 @@ fn print_result(num: i32) { ////////// DO NOT CHANGE ABOVE HERE ///////// // TODO: create `math!()` macro. +macro_rules! math { + ($a:literal plus $b:literal) => { + $a + $b + }; + (square $a:literal) => { + $a * $a + }; +} ////////// DO NOT CHANGE BELOW HERE ///////// diff --git a/exercises/04_expression_variables/main.rs b/exercises/04_expression_variables/main.rs index 1258e12..7509d88 100644 --- a/exercises/04_expression_variables/main.rs +++ b/exercises/04_expression_variables/main.rs @@ -5,6 +5,14 @@ fn print_result(num: i32) { ////////// DO NOT CHANGE ABOVE HERE ///////// // TODO: create `math!()` macro. +macro_rules! math { + ($a:expr, plus, $b: expr) => { + $a + $b + }; + (square $a:expr) => { + $a * $a + }; +} ////////// DO NOT CHANGE BELOW HERE ///////// diff --git a/exercises/05_more_complex_example/main.rs b/exercises/05_more_complex_example/main.rs index ed2dafa..4109c71 100644 --- a/exercises/05_more_complex_example/main.rs +++ b/exercises/05_more_complex_example/main.rs @@ -15,7 +15,21 @@ impl Coordinate { ////////// DO NOT CHANGE ABOVE HERE ///////// // TODO: Create `for_2d!` macro here. - +macro_rules! for_2d { + ( + $row: ident <$row_type: ty> in $row_items: expr, + $col: ident <$col_type: ty> in $col_items: expr, + $block: block + ) => { + for $row in $row_items { + let $row: $row_type = $row; + for $col in $col_items { + let $col: $col_type = $col; + $block + } + } + }; +} ////////// DO NOT CHANGE BELOW HERE ///////// fn main() { diff --git a/exercises/06_repetition/main.rs b/exercises/06_repetition/main.rs index 890e627..eaeb522 100644 --- a/exercises/06_repetition/main.rs +++ b/exercises/06_repetition/main.rs @@ -5,7 +5,11 @@ fn print_success() { ////////// DO NOT CHANGE ABOVE HERE ///////// // TODO: create `if_any!()` macro. - +macro_rules! if_any { + ($($predicate: expr),+; $block: block) => { + if $($predicate)||* $block + }; +} ////////// DO NOT CHANGE BELOW HERE ///////// fn main() { diff --git a/exercises/07_more_repetition/main.rs b/exercises/07_more_repetition/main.rs index 2d18628..4bb5115 100644 --- a/exercises/07_more_repetition/main.rs +++ b/exercises/07_more_repetition/main.rs @@ -7,7 +7,15 @@ fn print_hashmap(hashmap: &HashMap<&str, &str>) { ////////// DO NOT CHANGE ABOVE HERE ///////// // TODO: create `hashmap!()` macro. - +macro_rules! hashmap { + ($($key:literal => $value: expr),* $(,)*) => { + { + let mut hm = HashMap::new(); + $(hm.insert($key, $value));*; + hm + } + }; +} ////////// DO NOT CHANGE BELOW HERE ///////// fn main() { diff --git a/exercises/08_nested_repetition/main.rs b/exercises/08_nested_repetition/main.rs index 71418e9..4ab707c 100644 --- a/exercises/08_nested_repetition/main.rs +++ b/exercises/08_nested_repetition/main.rs @@ -5,7 +5,18 @@ fn print_vec(vec: &Vec) { ////////// DO NOT CHANGE ABOVE HERE ///////// // TODO: create `graph!()` macro. - +macro_rules! graph { + ( + $($key:literal -> ($( + $val:expr + ),* + ));* $(;)? + ) => {{ + let mut vec = Vec::new(); + $($(vec.push(($key, $val));)*)* + vec + }}; +} ////////// DO NOT CHANGE BELOW HERE ///////// #[allow(clippy::vec_init_then_push)] diff --git a/exercises/09_ambiguity_and_ordering/main.rs b/exercises/09_ambiguity_and_ordering/main.rs index 23581e7..a038998 100644 --- a/exercises/09_ambiguity_and_ordering/main.rs +++ b/exercises/09_ambiguity_and_ordering/main.rs @@ -1,3 +1,4 @@ +#[allow(dead_code)] ////////// DO NOT CHANGE BELOW HERE ///////// /// This enum should represent what code the user wrote exactly. @@ -24,29 +25,29 @@ impl NumberType { // Sum together at least two expressions. macro_rules! sum { - ($($expr:expr),+ , $lastexpr:expr) => { - $($expr + )+ $lastexpr - } + ($first:expr $(, $expr:expr)*) => { + $first$(+ $expr)* + }; } macro_rules! get_number_type { - ( $e:expr ) => { - NumberType::UnknownBecauseExpr($e) - }; - ( $block:block ) => { - NumberType::UnknownBecauseBlock($block) + (-$negative:literal ) => { + NumberType::NegativeNumber(-$negative) }; ( $positive:literal ) => { NumberType::PositiveNumber($positive) }; - ( -$negative:literal ) => { - NumberType::NegativeNumber(-$negative) + ( $block:block ) => { + NumberType::UnknownBecauseBlock($block) + }; + ( $e:expr ) => { + NumberType::UnknownBecauseExpr($e) }; } ////////// DO NOT CHANGE BELOW HERE ///////// fn main() { - // PositiveNumber + // // PositiveNumber get_number_type!(5).show(); // NegativeNumber diff --git a/exercises/10_macros_calling_macros/main.rs b/exercises/10_macros_calling_macros/main.rs index ea76ba0..26d9f26 100644 --- a/exercises/10_macros_calling_macros/main.rs +++ b/exercises/10_macros_calling_macros/main.rs @@ -34,7 +34,13 @@ macro_rules! digit { ////////// DO NOT CHANGE ABOVE HERE ///////// // TODO: create `number!()` macro. - +macro_rules! number { + ($($num: tt) +) => { // instead of tt, can use literal + concat!($( + digit!($num) + ),+) + }; +} ////////// DO NOT CHANGE BELOW HERE ///////// fn main() { From 066c1b9606c70330017c91f886a981fb55c518fd Mon Sep 17 00:00:00 2001 From: kulapoo Date: Sun, 23 Mar 2025 16:35:36 +0800 Subject: [PATCH 10/10] all completed --- exercises/11_macro_recursion/main.rs | 29 ++++++++++++++++++++++++++++ exercises/12_hygienic_macros/main.rs | 21 +++++++++++++++++--- 2 files changed, 47 insertions(+), 3 deletions(-) diff --git a/exercises/11_macro_recursion/main.rs b/exercises/11_macro_recursion/main.rs index e6942f2..14abe7b 100644 --- a/exercises/11_macro_recursion/main.rs +++ b/exercises/11_macro_recursion/main.rs @@ -1,5 +1,33 @@ +// #![recursion_limit = "256"] // TODO: Create the `curry!()` macro. +macro_rules! curry { + ( + ($ident_arg:ident:$ident_type:ty)=>_, + $block: block + ) => { + move |$ident_arg: $ident_type| { + print_curried_argument($ident_arg); + $block + } + }; + ( + ($ident_arg:ident:$ident_type:ty)=> + $( + ($ident_args:ident:$ident_types:ty) => + )* + _, + $block: block + ) => { + move |$ident_arg: $ident_type| { + print_curried_argument($ident_arg); + curry!( + $(($ident_args:$ident_types)=>)* _, + $block + ) + } + }; +} ////////// DO NOT CHANGE BELOW HERE ///////// fn print_numbers(nums: &Vec) { @@ -16,6 +44,7 @@ fn print_curried_argument(val: impl std::fmt::Debug) { fn main() { println!("=== defining functions ==="); + let is_between = curry!((min: i32) => (max: i32) => (item: &i32) => _, { min < *item && *item < max }); diff --git a/exercises/12_hygienic_macros/main.rs b/exercises/12_hygienic_macros/main.rs index e8418a2..d0d5c88 100644 --- a/exercises/12_hygienic_macros/main.rs +++ b/exercises/12_hygienic_macros/main.rs @@ -1,7 +1,22 @@ macro_rules! coord { - ($x:expr, $y:expr) => {}; - ($x:expr, $y:expr, $z:expr) => {}; - ($x:expr, $y:expr, $z:expr, $t:expr) => {}; + ($x:expr, $y:expr) => { + $crate::Coordinate { x: $x, y: $y } + }; + ($x:expr, $y:expr, $z:expr) => { + $crate::third_dimension::Coordinate { + x: $x, + y: $y, + z: $z, + } + }; + ($x:expr, $y:expr, $z:expr, $t:expr) => { + $crate::fourth_dimension::Coordinate { + x: $x, + y: $y, + z: $z, + t: $t, + } + }; } ////////// DO NOT CHANGE BELOW HERE /////////