appendix Exercise solutions
- Fill in the question marks (
???
), and make the following declarative macro compile.
This is one possible solution. ty
seems most appropriate, though ident
—for example—would also work:
macro_rules! hello_world { ($something:ty) => { impl $something { fn hello_world(&self) { println!("Hello world!") } } }; } struct Example {} hello_world!(Example); fn main() { let e = Example {}; e.hello_world(); }
- In our first declarative macro example, we use
expr
in some of our matches. But that was not our only option. Try to replace that withliteral
,tt
,ident
, orty
. Which ones work? Which don’t? Do you understand why?
literal
will work because we are passing in literal values (e.g., my_vec!(1, 2, 3)
). tt
will work as well. As we said, it accepts basically anything. ident
will not work because we are not passing in identifiers. A valid example—that would get accepted—might be NameOfThisStructIDeclaredInMyCode
. ty
will not work either, because we are not passing in types. Valid examples include String
, i32
, etc.
- Allow trailing comments in the
my_vec
macro. You can do this by writing another matcher, but there’s a simple solution with even less repetition. If you need help, take a look at thevec
macro from the standard library for inspiration.
This solution is based on the standard library code: