avatarJohn Philip

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

1718

Abstract

built_in">macro_rules!</span> my_macro { () => { <span class="hljs-built_in">println!</span>(<span class="hljs-string">"Check out my macro!"</span>); } (val:expr) =&gt; { <span class="hljs-built_in">println!</span>(<span class="hljs-string">"Look at this other macro: {}"</span>, val); } }

<span class="hljs-keyword">fn</span> <span class="hljs-title function_">main</span>() { my_macro!(); my_macro!(<span class="hljs-number">7777</span>); }</pre></div><h2 id="817f">Explanation:</h2><p id="89db">Well this one is a bit tricky and easy at the same time. When we have various macro arms to be matched, we need to add comma after every arm<code>;</code> .</p><h2 id="2733">Solution:</h2><div id="7a4b"><pre>#[<span class="hljs-attr">rustfmt</span>::skip] macro_rules! my_macro { <span class="hljs-function">() =></span> { println!(<span class="hljs-string">"Check out my macro!"</span>); }; <span class="hljs-function">(<span class="hljs-params">val:expr</span>) =&gt;</span> { println!(<span class="hljs-string">"Look at this other macro: {}"</span>, val); } }

fn <span class="hljs-title function_">main</span>(<span class="hljs-params"></span>) { my_macro!(); my_macro!(<span class="hljs-number">7777</span>); }</pre></div><p id="686e">You can experiment with the code on <a href="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2021&amp;gist=a6d10a62b912e06da93d03f2b1442522"><b><i>Rust Playground</i></b></a>.</p><h2 id="2ed9">Resources</h2><ul><li><a href="https://doc.rust-lang.org/book/ch19-06-macros.html"><b><i>Macros</i></b></a></li><li><a href="https://veykril.github.io/tlborm/"><b><i>The Litt

Options

le Book of Rust Macros</i></b></a></li></ul><h2 id="fd41">Before you go</h2><p id="7472">Thank you for taking the time to read through this challenge. We invite you to share your knowledge of Rust as well. If you found this article valuable, please don’t hesitate to share it with others. Don’t forget to follow the publication and give the article some claps 👏.</p><p id="3f4f">Thank you, and we look forward to seeing you for the next challenges!</p><h2 id="6868">More reads</h2><div id="c177" class="link-block"> <a href="https://readmedium.com/write-for-us-at-rustancean-121ed52eae"> <div> <div> <h2>Write for Us at Rustaceans</h2> <div><h3>Join Our Rustaceans Community: Share Your Expertise and Inspire Learning</h3></div> <div><p>medium.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/0*c-hqTpwb0rml3dxS)"></div> </div> </div> </a> </div><div id="cbaf" class="link-block"> <a href="https://readmedium.com/rustlings-macros3-rs-issue78-macros-in-rust-4cb2a688e653"> <div> <div> <h2>Rustlings: macros3.rs #Issue78 — Macros in Rust</h2> <div><h3>Rustlings Challenge: macros3.rs Solution Walkthrough</h3></div> <div><p>medium.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/1*O91T72-Ou-5iT10T96CgGQ.png)"></div> </div> </div> </a> </div></article></body>

Rustlings: macros4.rs #Issue79 — Macros in Rust

Rustlings Challenge: macros4.rs Solution Walkthrough

Image by Phoomparin Mano

This is the Seventy-ninth (79th) issue of the Rustlings series. In this issue, we provide solutions to Rustlings exercises along with detailed explanations. In this issue we will solve the challenge on macros4.rs.

Previous challenge #Issue 78

Challenge:

// macros4.rs
//
// Execute `rustlings hint macros4` or use the `hint` watch subcommand for a
// hint.

// I AM NOT DONE

#[rustfmt::skip]
macro_rules! my_macro {
    () => {
        println!("Check out my macro!");
    }
    ($val:expr) => {
        println!("Look at this other macro: {}", $val);
    }
}

fn main() {
    my_macro!();
    my_macro!(7777);
}

Explanation:

Well this one is a bit tricky and easy at the same time. When we have various macro arms to be matched, we need to add comma after every arm; .

Solution:

#[rustfmt::skip]
macro_rules! my_macro {
    () => {
        println!("Check out my macro!");
    };
    ($val:expr) => {
        println!("Look at this other macro: {}", $val);
    }
}

fn main() {
    my_macro!();
    my_macro!(7777);
}

You can experiment with the code on Rust Playground.

Resources

Before you go

Thank you for taking the time to read through this challenge. We invite you to share your knowledge of Rust as well. If you found this article valuable, please don’t hesitate to share it with others. Don’t forget to follow the publication and give the article some claps 👏.

Thank you, and we look forward to seeing you for the next challenges!

More reads

Rust
Rustlang
Rust Programming Language
Rustling
Programming
Recommended from ReadMedium