avatarJohn Philip

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

1803

Abstract

js-keyword">mod</span> macros { <span class="hljs-built_in">macro_rules!</span> my_macro { () => { <span class="hljs-built_in">println!</span>(<span class="hljs-string">"Check out my macro!"</span>); }; } }

<span class="hljs-keyword">fn</span> <span class="hljs-title function_">main</span>() { my_macro!(); }</pre></div><h2 id="1a8e">Explanation:</h2><p id="44e1">Macros defined in a module are not visible outside that module by default. To make the code compile, we need to make the macro <code>my_macro</code> accessible from the main module. You can do this by using the <code>#[macro_export]</code> attribute on the module that contains the macro definition.</p><h2 id="3531">Solution:</h2><div id="3add"><pre><span class="hljs-keyword">mod</span> macros { <span class="hljs-meta">#[macro_export]</span> <span class="hljs-built_in">macro_rules!</span> my_macro { () => { <span class="hljs-built_in">println!</span>(<span class="hljs-string">"Check out my macro!"</span>); }; } }

<span class="hljs-keyword">fn</span> <span class="hljs-title function_">main</span>() { my_macro!(); }</pre></div><p id="433e">In our solution code:</p><ol><li>We add <code>#[macro_export]</code> to the macro definition to indicate that it should be exported and accessible from other modules.</li><li>In the main function, we access the macro using the module path <code>macros::my_macro!()</code>.</li></ol><p id="a0bd">You can experiment with the code on <a href="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2021&amp;gist=dda8059bb788537198e7eee94109dbd9"><b><i>Rust Playground</i></b></a>.</p><h2 id="26f4">Resources</h2><ul><li><a href="https://doc.rust-lang.org/book/ch19-06-macros.html"><b><i>

Options

Macros</i></b></a></li><li><a href="https://veykril.github.io/tlborm/"><b><i>The Little Book of Rust Macros</i></b></a></li></ul><h2 id="f5a0">Before you go</h2><p id="569a">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="5fb4">Thank you, and we look forward to seeing you for the next challenges!</p><h2 id="be57">More reads</h2><div id="59b6" 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="bca6" class="link-block"> <a href="https://readmedium.com/rustlings-macros2-rs-issue77-macros-in-rust-063f065d4b52"> <div> <div> <h2>Rustlings: macros2.rs #Issue77 — Macros in Rust</h2> <div><h3>Rustlings Challenge: macros2.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: macros3.rs #Issue78 — Macros in Rust

Rustlings Challenge: macros3.rs Solution Walkthrough

Image by Phoomparin Mano

This is the Seventy-eighth (78th) 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 macros3.rs.

Previous challenge #Issue 77

Challenge:

// macros3.rs
//
// Make me compile, without taking the macro out of the module!
//
// Execute `rustlings hint macros3` or use the `hint` watch subcommand for a
// hint.

// I AM NOT DONE

mod macros {
    macro_rules! my_macro {
        () => {
            println!("Check out my macro!");
        };
    }
}

fn main() {
    my_macro!();
}

Explanation:

Macros defined in a module are not visible outside that module by default. To make the code compile, we need to make the macro my_macro accessible from the main module. You can do this by using the #[macro_export] attribute on the module that contains the macro definition.

Solution:

mod macros {
    #[macro_export]
    macro_rules! my_macro {
        () => {
            println!("Check out my macro!");
        };
    }
}

fn main() {
    my_macro!();
}

In our solution code:

  1. We add #[macro_export] to the macro definition to indicate that it should be exported and accessible from other modules.
  2. In the main function, we access the macro using the module path macros::my_macro!().

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