1. 38
    Rustlings test4: Defining macros that accept arguments
    49s

Rustlings test4: Defining macros that accept arguments

Chris Biscardi
InstructorChris Biscardi
Share this video with your friends

Social Share Links

Send Tweet
Published 4 years ago
Updated 4 years ago

Chris Biscardi: [0:00] 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!".

[0:13] 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.

[0:25] 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.

[0:37] 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.