rustlings icon indicating copy to clipboard operation
rustlings copied to clipboard

How to solve macros3.rs is unclear

Open udalrich opened this issue 4 years ago • 9 comments

I looked at the hint. I looked in the book. I'm pretty sure the answer is something involving #[macro_use], but the only way I could get it to compile was to take it out of the module. Either the book or the hint needs to provide more of a hint.

udalrich avatar Apr 21 '20 22:04 udalrich

Actually, the hint tells you exactly what you need to do, without actually telling you which specific characters you have to type.

In order to use a macro outside of its module, you need to do something special to the module to lift the macro out into its parent.

The macro is defined inside the module, but you need a way to make it visible outside of it. And it involves something you put into the module. The hint became obvious to me when I looked at the compiler output after trying the only mechanism I'd seen so far to make something accessible outside a module.

Yo clarify "the only mechanism I've seen so far": I haven't looked at the language documentation because I want to see how hard it would be to figure out the language based purely on these exercises and the stuff I can glean from doing them in order. I'm sure that the answer is in the actual documentation on macros and/or modules.

jrial avatar May 01 '20 11:05 jrial

I'm having the same problem. I tried:

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

fn main() {
    macros::my_macro!();
}

and

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

fn main() {
    macros::my_macro!();
}

Neither option compiles.

wjdhamilton avatar Jul 25 '20 19:07 wjdhamilton

@udalrich @wjdhamilton https://doc.rust-lang.org/reference/macros-by-example.html#the-macro_use-attribute


#[macro_use]
mod inner {
    macro_rules! m {
        () => {};
    }
}

m!();

leonelv avatar Jul 26 '20 16:07 leonelv

Thankyou! So we have to get rid of the macros:: syntax to use #[macro_use].

wjdhamilton avatar Jul 27 '20 15:07 wjdhamilton

I had the same issue and ended up figuring it out only after finding this exact issue.

I'd like to propose to either:

  • include link to the Rust Lang Reference's macro_use
  • make a hint to see Rust Lang Reference (since Rust Lang Book doesn't provide neither hint or solution)

My reading material was only the Rust Language book and there are 0 mentions of macro_use there (See: https://doc.rust-lang.org/book/title-page.html?search=macro_use)

Also, not not sure if that's the error message change, but the error message doesn't hint that the macro_use should be used (unless the macros:: piece is removed).

exlee avatar Oct 06 '20 10:10 exlee

Actually, the hint tells you exactly what you need to do, without actually telling you which specific characters you have to type.

In order to use a macro outside of its module, you need to do something special to the module to lift the macro out into its parent.

I wouldn't use "exactly" there.

Anyway, Gentlemen, I read the book's macro section and didn't find anything that points out use_marco attribute that @leonelv mentioned.

moien007 avatar Dec 11 '20 16:12 moien007

There's definitely a weird disconnect of how I am using rustlings (and I think other commenters in this issue) and how it's expected to be used. It wasn't until I realized the README.md that's in each section of rustlings that my frustration with the exercises settled a bit. They sometimes have more information than just a link to the rust doc book. (which is what some of the hints do)

In the exercises/macros/README.md there's a link to the books macro section, which as mentioned by a few others that, this exercise isn't covered by. BUT there's a second link which does have more information on macros.

here's the page that covers information in macros3 exercise: https://danielkeep.github.io/tlborm/book/mbe-min-import-export.html

it would be nice to see a "if you're still stuck, check out the links in the exercises/macros/README.md" in the hints.

JamieCrisman avatar Dec 30 '20 02:12 JamieCrisman

This is also now possible in a different way using current versions of rust(>=1.32).

The following compiles in a fresh cargo new --bin project but does not compile using the built in rustlings watch/run. This definitely caused some confusion for me, as it is a valid solution in a standard rust project.

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

    pub(super) use my_macro; // can also use "pub(crate)", but not just "pub"
}

fn main() {
    macros::my_macro!();
}

parkero avatar Jan 06 '23 21:01 parkero

Ran into the same problem: this was the first exercise where I really had to just try to google the answer, because the hint nor the direct references hinted at the solution. Would suggest adding a link to https://veykril.github.io/tlborm/decl-macros/minutiae/scoping.html Also the solution from @parkero seems nice and perhaps more useful to know.

fibr avatar Dec 09 '23 18:12 fibr