Macro usage--What is the difference between two?

Hello,

I am reading the code of the crate Crossterm. What I wonder is why the first code is written $crate::queue!(...). I think $ is unnecessary here, so I wrote second and tested--works well. Is there any reason to use $ here? I have searched "macro in macro" but I couldn't find answer...

// FIRST--Original
#[macro_export]
macro_rules! execute {
    ($writer:expr $(, $command:expr)* $(,)? ) => {{
        use::std::io::Write;

        // Queue each command, then flush
        $crate::queue!($writer $(, $command)*)
            .and_then(|()| {
                ::std::io::Write::flush($writer.by_ref())
            })
    }}
}

// SECOND
#[macro_export]
macro_rules! second {
    ($writer:expr $(, $command:expr)* $(,)?) => {{
        use::std::io::Write;

        queue!($writer $(, $command)*)
                .and_then(|()| {
                    ::std::io::Write::flush($writer.by_ref())
                })
    }}
}

Macros By Example - The Rust Reference (rust-lang.org)

The keyword metavariable $crate can be used to refer to the current crate; see Hygiene below.

1 Like

Thank you

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.