Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to pass named arguments to format macros without repeating the variable names?

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();
like image 757
user1244932 Avatar asked Jul 27 '17 17:07

user1244932


1 Answers

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.

like image 70
Shepmaster Avatar answered Nov 15 '22 09:11

Shepmaster