Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use std::ops::{Add, Mul, Div};
- #[derive(Copy, Debug, Clone)]
- struct Point<T> {
- x : T,
- y : T,
- }
- impl<T> Point<T> where T : Copy {
- fn new(x : T, y : T) -> Point<T> {
- Point {x, y}
- }
- fn midpoint(p0 : Point<T>, p1 : Point<T>) -> Point<T>
- where T : Copy + Add<Output=T> + Div<Output=T> {
- (p0 + p1) / 2 as T
- }
- }
- impl<T> Add for Point<T> where T : Add<Output=T> + Copy {
- type Output = Point<T>;
- fn add(self, rhs : Point<T>) -> Point<T> {
- Point::new(self.x + rhs.x, self.y + rhs.y)
- }
- }
- type PointI32 = Point<i32>;
- type PointF32 = Point<f32>;
- fn main() {
- let p0 = PointF32::new(1f32, 5f32);
- let p1 = PointF32::new(-3f32, 4f32);
- let res = PointF32::midpoint(p0, p1);
- println!("{:?}", res);
- // println!("Hello, world!");
- }
Add Comment
Please, Sign In to add comment