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


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/".

The format!() macro is ( - source)

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


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


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.