1use std::fmt::Display;
2
3#[derive(PartialEq, Clone, Debug)]
5pub struct Point {
6 pub x: i32,
7 pub y: i32,
8}
9
10impl Display for Point {
11 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
12 write!(f, "[ x: {}, y: {} ]", &self.x, &self.y)
13 }
14}
15
16impl Point {
17 pub fn add(point1: &Self, point2: &Self) -> Self {
19 Self {
20 x: point1.x + point2.x,
21 y: point1.y + point2.y,
22 }
23 }
24}
25
26#[cfg(test)]
27mod tests {
28 use crate::Point;
29
30 #[test]
31 fn add_points() {
32 let point1 = Point { x: 1, y: 2 };
33 let point2 = Point { x: 2, ..point1 };
34
35 let result = Point::add(&point1, &point2);
36 assert_eq!(result, Point { x: 3, y: 4 });
37 }
38
39 #[test]
40 fn format_point() {
41 let point1 = Point { x: 1, y: 2 };
42 let result = format!("{}", point1);
43 assert_eq!(result, "[ x: 1, y: 2 ]")
44 }
45}