Seems like what you're going for is a more portable way to format strings and we could use that in various contexts? @afetisov
I really don't know enough about the implementation details to comment on how it would work or forsee all potential issues, all i wanted was a nice way to include the variable names, and I never use dbg, i always use println, so something that only works in debugging to stderr isn't really hitting my use case. Can you give more advice on how that ought to work?
Ok, I apologize profusely for allowing the errors in the previous draft, no excuse for that, I personally rewrote it, here's a more streamlined version, if this is not doable or I screwed up, please reply and let me know.
"""
Title: Enhanced Variable Printing in Macros
- Author(s): bionicles
- Iteration: 3
- Status: Draft
Abstract: This RFC proposes a new shorthand to include variable names alongside values in Rust, inspired by Python's f-string {x=}
.
Motivation: Macro invocations to print variables and their values can be redundant and time-consuming to write and maintain, particularly with long or nested identifiers. The proposed shorthand syntax addresses this issue, making it more convenient to print variables and their values without duplicating variable names.
Currently:
fn main() {
let a_really_long_name = 42;
println!("a_really_long_name = {a_really_long_name}");
// Prints "a_really_long_name = 42" to stdout
dbg!(a_really_long_name);
// Prints "[src/main.rs:5:5] a_really_long_name = 42" to stderr
}
Proposed solution: The proposed shorthand allows using {x=} to print the variable x and its value. The macros expand this to the longer form x={x}. This syntax would become valid in Rust string formatting.
Shorthand:
fn main() {
let a_really_long_name = 42;
println!("{a_really_long_name=}");
// Prints "a_really_long_name = 42" to stdout
dbg!(a_really_long_name);
// Prints "[src/main.rs:5:5] a_really_long_name = 42" to stderr
}
Alternatives: The discussion mentioned several alternatives to the proposed solution, including using dbg!, log::debug!, or introducing a new macro, such as print_var!. The proposed solution was chosen over these alternatives due to simplicity and familiarity.c
Compatibility: The proposed shorthand syntax does not affect backward compatibility, as it introduces a new syntax that doesn't conflict with existing code. However, it's essential to consider potential confusion or misinterpretation, especially for users unfamiliar with Python f-strings.
Implementation: The implementation of the proposed shorthand syntax involves modifying macro expansion rules to recognize and handle the {x=} syntax. One concern could be if many different macros wind up needing to re-implement this feature. One idea to avoid this is to reuse the logic by making a helper macro.
A key challenge is to correctly parse and expand the shorthand in all cases, including when used in combination with other named arguments, dot accessors, formatting specifiers, etc.
References:
"""