The documentation for Add
给出以下示例:
Add
为什么文档作者在这里使用use std::ops::Add;
#[derive(Debug, PartialEq)]
struct Point {
x: i32,
y: i32,
}
impl Add for Point {
type Output = Self;
fn add(self, other: Self) -> Self {
Self {
x: self.x + other.x,
y: self.y + other.y,
}
}
}
,而不是用名字提及Self
?有技术上的区别,还是纯粹是风格方面的区别?
主要有两个原因:
Point
比Self
或MyType
短,尤其是SomeOtherType
。另请参见:
ThisTypeWithGenerics<'a, 'b, A, String>