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! ![]()
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