为什么在 Rust 中访问指向结构的指针上的字段?

Why does accessing a field on a pointer to a struct work in rust?

我注意到,给定 P<SomeStruct>,直接在指针上访问 SomeStruct 的字段似乎有效,但我不确定为什么会这样。例如,此代码按预期编译和工作(打印“1234”):

#![feature(rustc_private)]

extern crate syntax;
use syntax::ptr::P;

struct Baz {
    id: String,
}

fn foo() {
    let mut struct_pointer: P<Baz> = P(Baz {
        id: "1234".to_string(),
    });
    println!("{}", struct_pointer.id);
}

什么语言功能允许我访问 struct_pointer 绑定上的 id 字段?取消引用?强迫?有什么方法可以通过查看 P?

的文档来判断这类事情是否可行

implemented using the Deref trait

在 Rust 中,. 会在需要时自动取消引用,因此编译器可以将 foo.bar 解释为 (*foo).bar