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) => {
<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>) =></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&mode=debug&edition=2021&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>