Rust Macros, strange error

https://exercism.org/tracks/rust/exercises/macros/edit
My code:

#[macro_export]
macro_rules! hashmap {
    () => {
        std::collections::HashMap::new()
    };

    ($x:expr => $y:expr $(,)?) => {
        std::collections::HashMap::from([($x, $y)])
    };

    ($x:expr => $y:expr , $( $rest_x:expr => $rest_y:expr ),* $(,)? ) => {{
        let mut map = hashmap!($( $rest_x => $rest_y ),*);
        if !map.contains_key(&$x) {
            map.insert($x, $y);
        }
        map
    }};
}

It works locally so I really don’t know what’s the problem.
I have tried changing std::collections::HashMap to HashMap but it still doesn’t work.
Please help me — thanks! :pleading_face:

We received the following error when we ran your code:
   Compiling macros v0.1.0 (exercism-iteration)
error: cannot find macro `hashmap` in this scope
  --> tests/macros.rs:86:30
   |
86 |         let _with_trailing = macros::hashmap!(23 => 623, 34 => 21,);
   |                              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
   = note: this error originates in the macro `macros::hashmap` (in Nightly builds, run with -Z macro-backtrace for more info)
help: consider importing one of these macros
   |
71 +     use crate::hashmap;
   |
71 +     use macros::hashmap;
   |

error: cannot find macro `hashmap` in this scope
  --> tests/macros.rs:85:30
   |
85 |         let _without_comma = macros::hashmap!(23=> 623, 34 => 21);
   |                              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
   = note: this error originates in the macro `macros::hashmap` (in Nightly builds, run with -Z macro-backtrace for more info)
help: consider importing one of these macros
   |
71 +     use crate::hashmap;
   |
71 +     use macros::hashmap;
   |

error[E0433]: failed to resolve: use of undeclared type `HashMap`
  --> tests/macros.rs:76:59
   |
76 |         let _empty: ::std::collections::HashMap<(), ()> = hashmap!();
   |                                                           ^^^^^^^^^^ use of undeclared type `HashMap`
   |
   = note: this error originates in the macro `hashmap` (in Nightly builds, run with -Z macro-backtrace for more info)
help: consider importing one of these structs
   |
71 +     use crate::HashMap;
   |
71 +     use std::collections::HashMap;
   |

error[E0433]: failed to resolve: use of undeclared type `HashMap`
  --> tests/macros.rs:77:30
   |
77 |         let _without_comma = hashmap!(23=> 623, 34 => 21);
   |                              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ use of undeclared type `HashMap`
   |
   = note: this error originates in the macro `hashmap` (in Nightly builds, run with -Z macro-backtrace for more info)
help: consider importing one of these structs
   |
71 +     use crate::HashMap;
   |
71 +     use std::collections::HashMap;
   |

error[E0433]: failed to resolve: use of undeclared type `HashMap`
  --> tests/macros.rs:78:30
   |
78 |         let _with_trailing = hashmap!(23 => 623, 34 => 21,);
   |                              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ use of undeclared type `HashMap`
   |
   = note: this error originates in the macro `hashmap` (in Nightly builds, run with -Z macro-backtrace for more info)
help: consider importing one of these structs
   |
71 +     use crate::HashMap;
   |
71 +     use std::collections::HashMap;
   |

error[E0433]: failed to resolve: use of undeclared type `HashMap`
  --> tests/macros.rs:84:59
   |
84 |         let _empty: ::std::collections::HashMap<(), ()> = macros::hashmap!();
   |                                                           ^^^^^^^^^^^^^^^^^^ use of undeclared type `HashMap`
   |
   = note: this error originates in the macro `macros::hashmap` (in Nightly builds, run with -Z macro-backtrace for more info)
help: consider importing one of these structs
   |
71 +     use crate::HashMap;
   |
71 +     use std::collections::HashMap;
   |

For more information about this error, try `rustc --explain E0433`.
error: could not compile `macros` due to 6 previous errors
1 Like

cc @senekor Maybe @IsaacG can move it to the Rust category?

Moved to the rust category

You’re calling a macro recursively. Problem is, the recursive call is resolved at the call site, where your hashmap macro may not be in scope. To refer to things inside the crate in which the macro is defined, use $crate. So, in this case, $crate::hashmap!(...) for the recursive macro call.

Also, it’s good practice to use ::std to refer to items in the standard library. (same goes for any other library). If the call site shadows the name of std, your macro wouldn’t work anymore if you don’t use the :: prefix.