كيفية تنفيذ trait Add لمرجع إلى struct في Rust؟
مرحباً! أنا أتعلم Rust وأحاول تحميل العامل `+` للـ struct الخاص بي. لدي struct `Point` مع حقلين `x` و `y`. أريد أن أتمكن من جمع مثيلين من `Point` عن طريق تمريرهما كمرجع (reference) لتجنب نقل الملكية (ownership).
إليك الكود الخاص بي:
```rust
struct Point {
x: i32,
y: i32,
}
impl std::ops::Add for Point {
type Output = Point;
fn add(self, other: Point) -> Point {
Point { x: self.x + other.x, y: self.y + other.y }
}
}
```
هذا يعمل، لكنه يستهلك `self` و `other` بالقيمة (by value). أريد أن أتمكن من كتابة:
```rust
let a = Point { x: 1, y: 2 };
let b = Point { x: 3, y: 4 };
let c = &a + &b;
```
حاولت كتابة:
```rust
impl std::ops::Add for &Point {
type Output = Point;