// `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
arms:中一样
let a = 0;
let (a, c) = (0, 1);
let &a = &0;
let Inches(a) = Inches(0);
所以你在这里看到的是CCD_ 4被匹配为CCD_;英寸";。
这句话可能更普遍地可读为:
let inches = self.0;