From cbfe2f1de7f0e24525e641228fec4c659badde93 Mon Sep 17 00:00:00 2001 From: Adam Carpenter <53hornet@gmail.com> Date: Fri, 22 Mar 2019 16:48:03 -0400 Subject: Added advanced traits, types, functions. --- advanced/adv-traits/src/main.rs | 161 ++++++++++++++++++++++++++++++++++++---- 1 file changed, 145 insertions(+), 16 deletions(-) (limited to 'advanced/adv-traits/src/main.rs') diff --git a/advanced/adv-traits/src/main.rs b/advanced/adv-traits/src/main.rs index 6ec0aac..659883c 100644 --- a/advanced/adv-traits/src/main.rs +++ b/advanced/adv-traits/src/main.rs @@ -8,27 +8,156 @@ // fn next(&mut self) -> Option; //} // -use std::ops::Add; +// +//#[derive(Debug, PartialEq)] +//struct Point { +// x: i32, +// y: i32, +//} +// +//impl Add for Point { +// type Output = Point; +// +// fn add(self, other: Point) -> Point { +// Point { +// x: self.x + other.x, +// y: self.y + other.y, +// } +// } +//} -#[derive(Debug, PartialEq)] -struct Point { - x: i32, - y: i32, -} +//fn main() { +// assert_eq!(Point { x: 1, y: 0 } + Point { x: 2, y: 3 }, +// Point { x: 3, y: 3 }); +// println!("Done."); +//} + +//trait Add { +// type Output; +// +// fn add(self, rhs: RHS) -> Self::Output; +//} + +//use std::ops::Add; +// +//struct Millimeters(u32); +//struct Meters(u32); +// +//impl Add for Millimeters { +// type Output = Millimeters; +// +// fn add(self, other: Meters) -> Millimeters { +// Millimeters(self.0 + (other.0 * 1000)) +// } +//} + + +//trait Pilot { +// fn fly(&self); +//} +// +//trait Wizard { +// fn fly(&self); +//} +// +//struct Human; +// +//impl Pilot for Human { +// fn fly(&self) { +// println!("Because I was inverted."); +// } +//} +// +//impl Wizard for Human { +// fn fly(&self) { +// println!("Fly, you fools."); +// } +//} +// +//impl Human { +// fn fly(&self) { +// println!("*feels real loose like a long-neck goose*"); +// } +//} +// +// +//trait Animal { +// fn baby_name() -> String; +//} +// +//struct Dog; +// +//impl Dog { +// fn baby_name() -> String { +// String::from("Spot") +// } +//} +// +//impl Animal for Dog { +// fn baby_name() -> String { +// String::from("puppy") +// } +//} + + +//fn main() { +// let person = Human; +// person.fly(); +// Pilot::fly(&person); +// Wizard::fly(&person); +// +// +// println!("A baby dog is called a {}", ::baby_name()); +// +// +// println!("Done."); +//} +// + + +//use std::fmt; +// +//trait OutlinePrint: fmt::Display { +// fn outline_print(&self) { +// let output = self.to_string(); +// let len = output.len(); +// println!("{}", "*".repeat(len + 4)); +// println!("*{}*", " ".repeat(len + 2)); +// println!("* {} *", output); +// println!("*{}*", " ".repeat(len + 2)); +// println!("{}", "*".repeat(len + 4)); +// } +//} +// +//struct Point { +// x: i32, +// y: i32, +//} +// +//impl OutlinePrint for Point {} +// +//impl fmt::Display for Point { +// fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { +// write!(f, "({}, {})", self.x, self.y) +// } +//} +// +//fn main() { +// let point = Point { x: 5, y: 6 }; +// point.outline_print(); +//} + +use std::fmt; -impl Add for Point { - type Output = Point; +struct Wrapper(Vec); - fn add(self, other: Point) -> Point { - Point { - x: self.x + other.x, - y: self.y + other.y, - } +impl fmt::Display for Wrapper { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "[{}]", self.0.join(", ")) } } fn main() { - assert_eq!(Point { x: 1, y: 0 } + Point { x: 2, y: 3 }, - Point { x: 3, y: 3 }); - println!("Done."); + let w = Wrapper(vec![String::from("hello"), String::from("world")]); + println!("w = {}", w); } -- cgit v1.2.3