summaryrefslogtreecommitdiff
path: root/rust-book/traits
diff options
context:
space:
mode:
authorAdam Carpenter <gitlab@53hor.net>2019-07-09 15:14:04 -0400
committerAdam Carpenter <gitlab@53hor.net>2019-07-09 15:14:04 -0400
commit7e8ee5ed9cad6484e9f13f81731b102ced58402e (patch)
tree5395402ab07bbb5a659dbd68c701e22a1227202f /rust-book/traits
downloadlearning-rust-7e8ee5ed9cad6484e9f13f81731b102ced58402e.tar.xz
learning-rust-7e8ee5ed9cad6484e9f13f81731b102ced58402e.zip
Init.
Diffstat (limited to 'rust-book/traits')
-rwxr-xr-xrust-book/traits/Cargo.lock4
-rwxr-xr-xrust-book/traits/Cargo.toml7
-rwxr-xr-xrust-book/traits/src/main.rs114
3 files changed, 125 insertions, 0 deletions
diff --git a/rust-book/traits/Cargo.lock b/rust-book/traits/Cargo.lock
new file mode 100755
index 0000000..3eff52b
--- /dev/null
+++ b/rust-book/traits/Cargo.lock
@@ -0,0 +1,4 @@
+[[package]]
+name = "traits"
+version = "0.1.0"
+
diff --git a/rust-book/traits/Cargo.toml b/rust-book/traits/Cargo.toml
new file mode 100755
index 0000000..461bdcd
--- /dev/null
+++ b/rust-book/traits/Cargo.toml
@@ -0,0 +1,7 @@
+[package]
+name = "traits"
+version = "0.1.0"
+authors = ["Adam Carpenter <adam.carpenter@adp.com>"]
+edition = "2018"
+
+[dependencies]
diff --git a/rust-book/traits/src/main.rs b/rust-book/traits/src/main.rs
new file mode 100755
index 0000000..770caa9
--- /dev/null
+++ b/rust-book/traits/src/main.rs
@@ -0,0 +1,114 @@
+use std::fmt::Display;
+
+struct Pair<T> {
+ x: T,
+ y: T,
+}
+
+impl<T> Pair<T> {
+ fn new(x: T, y: T) -> Self {
+ Self {
+ x,
+ y,
+ }
+ }
+}
+
+impl<T: Display + PartialOrd> Pair<T> {
+ fn cmp_display(&self) {
+ if self.x >= self.y {
+ println!("{}", self.x);
+ }
+ else {
+ println!("{}", self.y);
+ }
+ }
+}
+
+pub trait Summary {
+ fn summarize(&self) -> String {
+ format!("(read more from {}...)", self.summarize_author())
+ }
+
+ fn summarize_author(&self) -> String;
+}
+
+pub struct NewsArticle {
+ pub headline: String,
+ pub location: String,
+ pub author: String,
+ pub content: String,
+}
+
+impl Summary for NewsArticle {
+// fn summarize(&self) -> String {
+// format!("{}, by {} ({})", self.headline, self.author, self.location)
+// }
+
+ fn summarize_author(&self) -> String {
+ format!("{}", self.author)
+ }
+
+}
+
+pub struct Tweet {
+ pub username: String,
+ pub content: String,
+ pub reply: bool,
+ pub retweet: bool,
+}
+
+impl Summary for Tweet {
+// fn summarize(&self) -> String {
+// format!("{}: {}", self.username, self.content)
+// }
+
+ fn summarize_author(&self) -> String {
+ format!("@{}", self.username)
+ }
+}
+
+pub fn notify(item: impl Summary) {
+ println!("breaking news: {}", item.summarize());
+}
+
+fn returns_summarizable() -> impl Summary {
+
+ NewsArticle {
+ headline: String::from("Penguins win the stanley cup championship!"),
+ location: String::from("Pittsburgh, PA, USA"),
+ author: String::from("Iceburgh"),
+ content: String::from("The pittsburgh penguins are winners I guess."),
+ }
+// Tweet {
+// username: String::from("horse_ebooks"),
+// content: String::from("of course blargh"),
+// reply: false,
+// retweet: false,
+// }
+}
+
+fn main() {
+
+ let tweet = Tweet {
+ username: String::from("horse_ebooks"),
+ content: String::from("of course, as you probably already know, people"),
+ reply: false,
+ retweet: false,
+ };
+
+ //dbg!(tweet.summarize());
+
+ let article = NewsArticle {
+ headline: String::from("Penguins win the stanley cup championship!"),
+ location: String::from("Pittsburgh, PA, USA"),
+ author: String::from("Iceburgh"),
+ content: String::from("The pittsburgh penguins are winners I guess."),
+ };
+
+ //dbg!(article.summarize());
+
+ //notify(tweet);
+
+ returns_summarizable();
+}