Avatar ·

كيفية تنفيذ trait Add لمرجع إلى struct في Rust؟

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