use std::fmt; use std::ops::{Add, Div, Mul, Sub}; #[derive(Copy, Clone, Debug)] pub struct Vector { pub x: T, pub y: T, } impl fmt::Display for Vector where T: fmt::Display, { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { if let Some(prec) = f.precision() { write!(f, "[{:.*}, {:.*}]", prec, self.x, prec, self.y) } else { write!(f, "[{}, {}]", self.x, self.y) } } } impl Vector { pub fn new(x: T, y: T) -> Self { Vector { x, y } } } impl Vector { pub fn from_polar(r: f64, theta: f64) -> Self { Vector { x: r * theta.cos(), y: r * theta.sin(), } } } impl Add for Vector where T: Add, { type Output = Self; fn add(self, other: Self) -> Self::Output { Vector { x: self.x + other.x, y: self.y + other.y, } } } impl Sub for Vector where T: Sub, { type Output = Self; fn sub(self, other: Self) -> Self::Output { Vector { x: self.x - other.x, y: self.y - other.y, } } } impl Mul for Vector where T: Mul + Copy, { type Output = Self; fn mul(self, scalar: T) -> Self::Output { Vector { x: self.x * scalar, y: self.y * scalar, } } } impl Div for Vector where T: Div + Copy, { type Output = Self; fn div(self, scalar: T) -> Self::Output { Vector { x: self.x / scalar, y: self.y / scalar, } } } fn main() { use std::f64::consts::FRAC_PI_3; println!("{:?}", Vector::new(4, 5)); println!("{:.4}", Vector::from_polar(3.0, FRAC_PI_3)); println!("{}", Vector::new(2, 3) + Vector::new(4, 6)); println!("{:.4}", Vector::new(5.6, 1.3) - Vector::new(4.2, 6.1)); println!("{:.4}", Vector::new(3.0, 4.2) * 2.3); println!("{:.4}", Vector::new(3.0, 4.2) / 2.3); println!("{}", Vector::new(3, 4) / 2); }