Error message does not specify where in macro

⚓ Rust    📅 2025-11-06    👤 surdeus    👁️ 9      

surdeus

If I try to compile the program:

macro_rules! macro_with_format { () => {
    fn check_5(arg : usize) -> String {
        let s : &str;
        if arg < 5 {
            s = format!("{arg} < 5");
        } else {
            s = format!("{arg} >= 5");
        }
        String::from(s)
    }
}}

fn main() {
    macro_with_format!();
    println!( "{}", check_5(6) ); 
}

I get the following message (which does not indicate where the problem is in the macro):

 --> src/main.rs:14:5
   |
14 |     macro_with_format!();
   |     ^^^^^^^^^^^^^^^^^^^^ expected `&str`, found `String`
   |
   = note: this error originates in the macro `format` which comes from the expansion of the macro `macro_with_format` (in Nightly builds, run with -Z macro-backtrace for more info)

For more information about this error, try `rustc --explain E0308`.
error: could not compile `package` (bin "package") due to 2 previous errors

5 posts - 4 participants

Read full topic

🏷️ Rust_feed