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 3 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.

egghead
egghead
~ 13 minutes ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today