Searching macro to print "varX = 1234" via name!(varX)


This already exists somewhere:

let i = 4;
name!(i); // -> prints "i = 4"

It might have been in a crate, but I can not remember the name and the issue itself seems to be very search engine unfriendly.

macro_rules! name { ($e:expr) => { println!("{} = {}", stringify!($e), $e) } }

Even works with expressions like name(i+1).


Thanks, however I was actually looking for an existing crate which implements it :). Because the macro is that “simple” I fear everyone has it under a different name resulting in mutually unintelligible debug infrastructure.

And IIRC this version could also handle an arbitrary number of parameters, and possibly a prefix string.


Is there any particular reason you want the macro from that crate in particular?


The only one I could find that seemed relevant was inspect,

But I’d expect for really simple macros like this there will be lots of versions out there. Especially for the debug stuff, since that is one area where easy customization can pay off.


Oh, and just because I am physically unable to not attempt any sort of macro challenge:

macro_rules! name {
    (with $prefix:expr, $e:expr) => {
        println!("{}{} = {}", $prefix, stringify!($e), $e);
    (with $prefix:expr, $e:expr, $($es:expr),*) => {
        print!("{}", $prefix);
        print!("{} = {}", stringify!($e), $e);
            print!(", {} = {}", stringify!($es), $es);
    ($($es:expr),*) => { name!(with "", $($es),*) };

fn main() {
    let i = 4;
    name!(i, i+1);
    name!(with "stuff: ", &i as *const _ as usize);


@phaylon That is what I was looking for, thanks! @DanielKeep I’d like to use this and not my own implementation so that the code remains as general as possible, i.e. someone looking over the code doesn’t have to spend time (even if it is just a few seconds) to understand what a certain macro does.

And even though your macro can now do more than the inspect! one (and… how hard would checking for the Display-Trait and using {:?} if it is missing be? :), I’ll stick to the version which might be used and seen by other people elsewhere.


You can’t. You might be able to do it with specialisation (which we don’t have).