10

With new versions of Rust, you can simplify structure initialization like this:

Foo {
    a: a,
    b: b,
}

to this

Foo { a, b }

Is it possible to do something similar for format!/println!-like macros?

For now I need to write it like this:

let a = "a";
let b = "b";
write!(file, "{a} is {b}", a = a, b = b).unwrap();

Is it possible to write my own macros with an API like this:

let a = "a";
let b = "b";
my_write!(file, "{a} is {b}", a, b).unwrap();
8
  • I'm not sure that I understand what you are asking, but you don't need named formatting arguments, you can stick to positional arguments: write!(file, "{} is {}", a, b), or write!(file, "{0} is {1}", a, b). Commented Jul 27, 2017 at 17:03
  • @E_net4 No, I not need numbers, I want names, in have ~ 5 arguments so I need name for them, like {long_word}, {another_long_word} Commented Jul 27, 2017 at 17:05
  • You may wish to read the documentation on std::fmt. The code that you wrote does not seem to do what you want. Can you create a minimal reproducible example that shows how you are currently formatting the struct? Commented Jul 27, 2017 at 17:08
  • I think that using the variable a with the string value "a" is likely confusing your example, especially with the text "{a} is {b}". Try using something concrete like name and age instead. Commented Jul 27, 2017 at 17:16
  • 1
    @E_net4 The question is, does the formatter allow a to be intepreted as a = a in formatters, like it allows a to be short for a: a in struct initializers. The answer is No AFAIK? Commented Jul 27, 2017 at 17:16

2 Answers 2

12

RFC 2795 has been accepted and implemented. Starting in Rust 1.58, you will be able to go beyond your desired syntax:

write!(file, "{a} is {b}").unwrap();

Before then, you can write your own wrapper around println! and friends:

macro_rules! myprintln {
    ($fmt:expr, $($name:ident),*) => { println!($fmt, $($name = $name),*) }
}

fn main() {
    let a = "alpha";
    let b = "beta";
    myprintln!("{a} is {b}", a, b);
}

This will likely always be limited compared to the full formatter macro, but it may be sufficient for your case.

Sign up to request clarification or add additional context in comments.

Comments

-1

As of 1.5 this is the closest you can get natively.

my_write!(file, "{} is {}", a, b).unwrap();

Additionally as Shepmaster pointed out my answer will eventually become obsolete (I'll try to remember to remove it when that happens). If you want to keep an eye on the (to be implemented) syntax they propose you can check this github ticket they linked

7 Comments

OP asked this question on 2017-07-27; Rust 1.18 was the stable release at the time. In the question, they are already using the syntax you propose (write!(file, "{a} is {b}", a = a, b = b).unwrap();). The specific question is about removing the duplication of a = a.
I gave my upvote because I just wanted to know this answer and google found it :)
@Shepmaster that is not the syntax I demonstrated, though I'm open to making adjustments since it seems you may have misunderstood (I feel clarity is important). If you look the X does not use the = allowing for a more concise declaration. Granted it is without the explicit naming (which prevents repetition and out of order assigning) but the op didn't request that. Additionally I explicitly mentioned this is for 1.5 (not the 1.18 the OP was using). I added my answer because this still shows up in searches on google.
but the op didn't request that — sure they did: "Is there a way to pass named arguments to format macros". The syntax {} isn't a named argument, it's a positional argument. They even show what example they'd like: my_write!(file, "{a} is {b}", a, b).
I explicitly mentioned this is for 1.5 (not the 1.18 the OP was using) — sure, but why? The syntax you show has existed since 1.0. Why the arbitrary choice of 1.5?
|

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.