有没有办法做到这一点?
self.mega_ultra_field = 100500;
print_expr_and_value!(self.mega_ultra_field);
输出:
self.mega_ultra_field 100500
答案 0 :(得分:8)
通过stringify!
:
macro_rules! print_expr_and_value {
($e:expr) => {
println!("{} {}", stringify!($e), $e)
};
}
struct Foo {
mega_ultra_field: i32,
}
impl Foo {
fn bar(&mut self) {
self.mega_ultra_field = 100500;
print_expr_and_value!(self.mega_ultra_field);
}
}
fn main() {
Foo {
mega_ultra_field: 0,
}.bar();
}