1 use std::ops::{Add, Sub}; 2 3 /// 二维向量 4 #[derive(Copy, Clone, Debug, Default)] 5 pub struct Vector2 { 6 /// x坐标 7 pub x: i32, 8 /// y坐标 9 pub y: i32, 10 } 11 12 impl Vector2 { new(x: i32, y: i32) -> Self13 pub fn new(x: i32, y: i32) -> Self { 14 Vector2 { x: x, y: y } 15 } 16 } 17 18 impl Add for Vector2 { 19 type Output = Vector2; 20 add(self, other: Vector2) -> Self::Output21 fn add(self, other: Vector2) -> Self::Output { 22 Vector2 { 23 x: self.x + other.x, 24 y: self.y + other.y, 25 } 26 } 27 } 28 29 impl Sub for Vector2 { 30 type Output = Vector2; 31 sub(self, other: Vector2) -> Self::Output32 fn sub(self, other: Vector2) -> Self::Output { 33 Vector2 { 34 x: self.x - other.x, 35 y: self.y - other.y, 36 } 37 } 38 } 39