3

Let us say I have a function like follows:

fn log(msg: &str) {
    //fancy_output
    println!("{}", msg)
}

Now, if I want to log a variable using the function, I must do it like so:

let x = 5;
log(&format!("{:?}", x)); // Assume some complex data type which implements Debug

Clearly this is a lot of boilerplate. I could remove the & by making the argument a string, but that does not remove my bigger problem: using format!() everywhere.

How can I write a function/macro such that I can do the following or similar:

let x = 5;
log("{:?}", x) // Assume some complex data type which implements Debug

I know a place to start would be looking at the format! source code, but it is quite hard to understand for a beginner like me and how I might implement it here.

Do I use some fancy macro or is there a simpler way?

2
  • format! is macro, not a function. If you want that behaviour, you'll need to write a macro. Commented Oct 22, 2022 at 21:19
  • @PeterHall my bad that is what I meant. I get that but how is the question. I will edit to make it clear that I understand the difference. Commented Oct 22, 2022 at 21:22

1 Answer 1

6

format! is a macro, not a function, which is why it is able to work with variable number of arguments. You can do the same with a declarative macro like this:

macro_rules! log {
    ($($args: tt)*) => {
        println!($($args)*);
    }
}

The $($args: tt)* means that the macro accepts zero or more (*) of any kind of token (tt). Then it just passes these on to the println macro.

Which you can call like this:

fn main() {
    let x = 5;
    log!("{:?}", x);
}
Sign up to request clarification or add additional context in comments.

4 Comments

I guess I need to read the book on the macros again because I tried to implement it anf failed miserably.
But, if I wanted to, could I wrap it in a function like so: fn log2<T>(x: T) {log!(x)}. If so, what would T be?
Perhaps you could look at the source of the log crate for inspiration.
Also found stackoverflow.com/questions/28951503/… documenting for future use) Thanks for the help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.