如何查看导致Rust编译错误的宏展开结果?
我是Rust新手,正在尝试使用宏来生成代码。我使用了流行的crate `serde`,并编写了以下代码:
```rust
use serde::{Serialize, Deserialize};
#[derive(Serialize, Deserialize)]
struct User {
name: String,
age: u32,
email: String,
}
fn main() {
let user = User {
name: "Alice".to_string(),
age: 30,
email: "alice@example.com".to_string(),
};
let json = serde_json::to_string(&user).unwrap();
println!("{}", json);
}
```
一切正常。但当我尝试编写自己的宏来自动实现trait时:
```rust
macro_rules! impl_display {
($type:ty, $format:expr) => {
impl std::fmt::Display for $type {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, $format, self)
}
}
};
}
struct Point {
x: i32,
y: i32,
}
impl_display!(Point, "Point({}, {})");
```