用 Rust 解释这个结构体的实现

Explain this struct implementation in Rust

// `Inches`, a tuple struct that can be printed
#[derive(Debug)]
struct Inches(i32);

impl Inches {
    fn to_centimeters(&self) -> Centimeters {
        let &Inches(inches) = self;

        Centimeters(inches as f64 * 2.54)
    }
}

我了解函数签名将 Inches 结构的引用作为参数,函数定义中的第一行是什么意思?

let a = b语法中,a不仅可以是新变量的标识符,还可以是模式喜欢 match 武器:

let a = 0;
let (a, c) = (0, 1);
let &a = &0;
let Inches(a) = Inches(0);

所以你在这里看到的是 self 作为 &Inches 匹配并将内部值提取到一个名为“inches”的新变量中。

此声明可能更具普遍可读性:

let inches = self.0;