Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use std::fmt;
- pub struct Point<T> where T: Copy + fmt::Display {
- x: T,
- y: T
- }
- impl<T> Point<T> where T: Copy + fmt::Display {
- pub fn new(x: T, y: T) -> Point<T> {
- Point::<T>{ x: x, y: y }
- }
- }
- impl<T> Point<T> where T: Copy + fmt::Display {
- pub fn x(&self) -> T {
- self.x
- }
- }
- impl<T> Point<T> where T: Copy + fmt::Display {
- pub fn y(&self) -> T {
- self.y
- }
- }
- impl<T> fmt::Display for Point<T> where T: Copy + fmt::Display {
- fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
- write!(f, "({}, {})", self.x(), self.y())
- }
- }
- fn main() {
- let p1 = Point::<i32>::new(3, 4);
- println!("{}", p1);
- let p2 = Point::<f64>::new(2.4, 3.6);
- println!("{}", p2);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement