How to get format! macro to add line!() value at the end

Hi.

I try to create a new macro that is basically the same as format!(), but with additional functionality.

What I want is to automatically append the current line of code to the end.

For example,

...
20     my_new_format!("{} {}", 1, "blue") // expect: "1 blue src/main.rs:20".
...

The format!() macro is (macros.rs - source)

#[macro_export]
macro_rules! format_wl {
    ($($arg:tt)*) => {{
        let res = $crate::fmt::format($crate::__export::format_args!($($arg)*));
        res

        
    }}
}

Which parts do I need to edit?

Thanks in advance.

macro_rules! format_with_line {
    ($($arg:tt)*) => (
        ::std::format!("{} {}:{}", ::std::format_args!($($arg)*), ::std::file!(), ::std::line!())
    )
}

Rust Playground

6 Likes

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.