“Macros” exercise: chance of a non-compiling code
Consider a solution from a student:
#[macro_export]
macro_rules! hashmap {
() => {
HashMap::new()
};
($($key:expr => $value:expr),+) => {
{
let mut hm = HashMap::new();
$( hm.insert($key, $value); )*
hm
}
};
($($key:expr => $value:expr),+,) => {
hashmap!($($key => $value),*)
};
}
While it passes the tests, the code will fail to compile if HashMap is not imported to a scope first. (Playground)
I think we need to tweak tests to teach students about such case.
Would you like me to fill a PR?
Your playground link points to irrelevant code.
Note that the playground doesn't always generate a new unique share link, for example in cases where you click the share button a second time too soon after the first time.
I recommend fixing the playground link so that others can understand the issue.
I think I managed to understand it.
It seems it would be valuable to test for this. What would that look like? You could send the PR to demonstrate. If the full product would be too large and you wanted to show a small sample first to get an initial comment on it, your PR could contain only the small sample.
This has been up for a month. The playground link has not been fixed. I do not understand the thrust of the problem. I will close this issue now.
I was just wondering where this issue has gone.
Here's an updated playground link The problem is that the macro will generate the code exactly as written in the definition and if the types aren't in scope at the use site, then Rust will throw compile errors. The test is easy, simply invoke the macro somewhere where the HashMap hasn't been imported.