diff options
author | Adam Carpenter <53hornet@gmail.com> | 2019-03-27 15:32:37 -0400 |
---|---|---|
committer | Adam Carpenter <53hornet@gmail.com> | 2019-03-27 15:32:37 -0400 |
commit | 67cdcc2e12118becb823e20a40cc2687f2b8425a (patch) | |
tree | ed92c3234b89079e6d4cf36f5e80c5ffa79def48 /rust-book/advanced/adv-traits | |
parent | e25482fca375d318a39c3b54db396b0db6e0b263 (diff) | |
download | learning-rust-67cdcc2e12118becb823e20a40cc2687f2b8425a.tar.xz learning-rust-67cdcc2e12118becb823e20a40cc2687f2b8425a.zip |
Started Rust in Action MEAP.
Diffstat (limited to 'rust-book/advanced/adv-traits')
-rwxr-xr-x | rust-book/advanced/adv-traits/Cargo.lock | 4 | ||||
-rwxr-xr-x | rust-book/advanced/adv-traits/Cargo.toml | 7 | ||||
-rwxr-xr-x | rust-book/advanced/adv-traits/src/main.rs | 163 |
3 files changed, 174 insertions, 0 deletions
diff --git a/rust-book/advanced/adv-traits/Cargo.lock b/rust-book/advanced/adv-traits/Cargo.lock new file mode 100755 index 0000000..4fb1ae5 --- /dev/null +++ b/rust-book/advanced/adv-traits/Cargo.lock @@ -0,0 +1,4 @@ +[[package]] +name = "adv-traits" +version = "0.1.0" + diff --git a/rust-book/advanced/adv-traits/Cargo.toml b/rust-book/advanced/adv-traits/Cargo.toml new file mode 100755 index 0000000..b1c5f6a --- /dev/null +++ b/rust-book/advanced/adv-traits/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "adv-traits" +version = "0.1.0" +authors = ["Adam Carpenter <53hornet@gmail.com>"] +edition = "2018" + +[dependencies] diff --git a/rust-book/advanced/adv-traits/src/main.rs b/rust-book/advanced/adv-traits/src/main.rs new file mode 100755 index 0000000..659883c --- /dev/null +++ b/rust-book/advanced/adv-traits/src/main.rs @@ -0,0 +1,163 @@ +//fn main() { +// println!("Hello, world!"); +//} +// +//pub trait Iterator { +// type Item; +// +// fn next(&mut self) -> Option<Self::Item>; +//} +// +// +//#[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, +// } +// } +//} + +//fn main() { +// assert_eq!(Point { x: 1, y: 0 } + Point { x: 2, y: 3 }, +// Point { x: 3, y: 3 }); +// println!("Done."); +//} + +//trait Add<RHS=Self> { +// type Output; +// +// fn add(self, rhs: RHS) -> Self::Output; +//} + +//use std::ops::Add; +// +//struct Millimeters(u32); +//struct Meters(u32); +// +//impl Add<Meters> 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 {}", <Dog as Animal>::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; + +struct Wrapper(Vec<String>); + +impl fmt::Display for Wrapper { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "[{}]", self.0.join(", ")) + } +} + +fn main() { + let w = Wrapper(vec![String::from("hello"), String::from("world")]); + println!("w = {}", w); +} |