Course lesson
Rustlings test4: Defining macros that accept arguments
- Duration
- 1 min
- Access
- Free
- Transcript
- Available
Course lesson
Chris Biscardi: Test4 covers modules and macros. We have to write a macro that passes the test. In this case, we have test_my_macro_world with assert_eq! Macro that calls my_macro! With the argument of "world!" and the value of "Hello world!".
Similarly, we have another test on line 21 that tests my_macro with an argument of "goodbye!" and says "Hello goodbye!". We can use the macro_rules! Macro with the name my_macro to define our macro.
In this case, we're saying that we take an argument and we'll want to use the format! Macro. We use the format! Macro with the word "Hello", followed by a replacement where the replacement will be the value that is passed in as an argument.
Note that our tests are now passing. We've written the macro with macro_rules! Called my_macro that takes a value as an argument and returns the value of format! With "Hello" and a default formatted value in that string.
Course lesson