Same macro expansion and different result

⚓ Rust    📅 2025-09-13    👤 surdeus    👁️ 10      

surdeus

Warning

This post was published 60 days ago. The information described in this article may have changed.

I do not understand why Program One is failing ?

While trying to understand what is going on, I tired Program Two,
which does not fail.

If I run cargo expand with Program One or Program Two as main.rs,
I get the same standard output (standard error is different).

I also do not understand why two programs can have the same macro expansion,
and one fail while the other does not ?

Program One:

macro_rules! set_value {
    (one) => {
        let value = 1.0;
    };
    (two) => {
        let value = 2.0;
    };
}
fn main() {
    set_value!(one);
    println!( "{}", value);
}

Result One:

  --> src/main.rs:11:21
   |
11 |     println!( "{}", value);
   |                     ^^^^^ not found in this scope

Program Two:

macro_rules! set_value {
    (one) => {
        let value = 1.0;
        println!( "{}", value);
    };
    (two) => {
        let value = 2.0;
        println!( "{}", value);
    };
}
fn main() {
    set_value!(one);
}  

Result Two:

1

2 posts - 2 participants

Read full topic

🏷️ Rust_feed