12345678910111213141516171819202122232425262728293031323334353637383940414243444546 |
- use point::Point;
- fn main() {
- // Defining 2 Points
- let p: Point<i32> = Point::from((3_i32, 2));
- let p1: Point<i32> = Point::from((1_i32, 2));
- // ^
- // Both are i32
- // Use a number to add to both x and y
- let p2 = p.clone() + 1;
- // ^
- // Clone is needed because + operator takes ownership
- // (So we'd lose access to p)
- assert_eq!(p2.x, 4, "p2.x = {} (expected 4)", p2.x);
- assert_eq!(p2.y, 3, "p2.y = {} (expected 3)", p2.y);
- // Or use a tuple of 2 numbers to add to x and y respectively
- let p3 = p1.clone() + (1, 2);
- assert_eq!(p3.x, 2, "p3.x = {} (expected 2)", p3.x);
- assert_eq!(p3.y, 4, "p3.y = {} (expected 4)", p3.y);
- // Let's clone p so we can manipulate it directly
- let mut p4 = p.clone();
- p4 += 2; // Change p4 directly (Clone it if you don't want to modify it directly)
- assert_eq!(p4.x, 5, "p4.x = {} (expected 5)", p4.x);
- assert_eq!(p4.y, 4, "p4.y = {} (expected 4)", p4.y);
- let mut p4 = p1.clone();
- p4 += (-1, 0); // Because it's i32, we can use negative to subtract (or just use -= operator)
- assert_eq!(p4.x, 0, "p4.x = {} (expected 0)", p4.x);
- assert_eq!(p4.y, 2, "p4.y = {} (expected 2)", p4.y);
- // Operators that return a new Point
- // Add +
- // Sub -
- // Mul *
- // Div /
- // Rem %
- // Operators that modify their Point (modifies self)
- // AddAssign +=
- // SubAssign -=
- // MulAssign *=
- // DivAssign /=
- // RemAssign %=
- }
|