Hello out there,
I'm very new to Rust with only a few weeks experience, coming from a professional C++ background. I'm quite excited about the borrow checker, the concurrency aids, and the overall clean appearance of the language. Rustdoc and the crates repository are great. This might be the language I am looking for. We'll see if I can convince my company to rewrite everything in Rust ...
While learning Rust and debugging my code, there is one feature I am missing: a =
specifier for format strings, as I know it from Python.
I would like to be able to say
let answer = 42;
println!("{answer=}");
and it should output answer = 42
.
This feature would save millions of Rust developers even more million hours of time they spend typing debugging/logging code.
The best I have come up with so far is to write a macro like this:
#[macro_export]
macro_rules! inspect {
($expr:expr) => {
format!("{} = {:?}", stringify!($expr), $expr)
};
}
which can be used this way:
let answer = 42;
println!("{}", inspect!(answer));
but this doesn't feel very comfortable, and it might have performance issues.
Is there a better way? Can i extend println!
or format_args!
in some magical way, or is there a place I could make a feature request?
I am aware of the dbg!
macro, but it doesn't really fit my needs. I would prefer a general format-string solution I can use for printing and logging.