summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJordan Gong <jordan.gong@protonmail.com>2020-09-02 21:00:12 +0800
committerJordan Gong <jordan.gong@protonmail.com>2020-09-02 21:00:12 +0800
commit5626a54d626cfe520ba879c42fc9dd0331c76eb9 (patch)
tree3ede9f0f8c3f2121a30d59e644f641c2a0c1d45e
parenta8a40420b4106d66bdf1c447388404896dccd494 (diff)
Share behavior with traits
-rw-r--r--traits/.gitignore18
-rw-r--r--traits/Cargo.toml9
-rw-r--r--traits/src/lib.rs78
-rw-r--r--traits/src/main.rs26
4 files changed, 131 insertions, 0 deletions
diff --git a/traits/.gitignore b/traits/.gitignore
new file mode 100644
index 0000000..e629269
--- /dev/null
+++ b/traits/.gitignore
@@ -0,0 +1,18 @@
+
+# Created by https://www.toptal.com/developers/gitignore/api/rust
+# Edit at https://www.toptal.com/developers/gitignore?templates=rust
+
+### Rust ###
+# Generated by Cargo
+# will have compiled files and executables
+/target/
+
+# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
+# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
+Cargo.lock
+
+# These are backup files generated by rustfmt
+**/*.rs.bk
+
+# End of https://www.toptal.com/developers/gitignore/api/rust
+
diff --git a/traits/Cargo.toml b/traits/Cargo.toml
new file mode 100644
index 0000000..a1712e6
--- /dev/null
+++ b/traits/Cargo.toml
@@ -0,0 +1,9 @@
+[package]
+name = "traits"
+version = "0.1.0"
+authors = ["Jordan Gong <jordan.gong@protonmail.com>"]
+edition = "2018"
+
+# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
+
+[dependencies]
diff --git a/traits/src/lib.rs b/traits/src/lib.rs
new file mode 100644
index 0000000..a0255ea
--- /dev/null
+++ b/traits/src/lib.rs
@@ -0,0 +1,78 @@
+pub trait Summary {
+ fn summarize(&self) -> String {
+ // default implementarion
+ format!("(Read more from {}...)", self.summarize_author())
+ }
+
+ fn summarize_author(&self) -> String {
+ String::from("Anonymous")
+ }
+}
+
+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)
+ }
+}
+
+// Traits as parameter
+// pub fn notify(item: &impl Summary) {
+// println!("Breaking news! {}", item.summarize());
+// }
+
+// Trait bound syntax
+pub fn notify<T: Summary>(item: &T) {
+ println!("Breaking news! {}", item.summarize());
+}
+
+// statements below are equivalent
+// pub fn nofity(item1: &impl Summary, item2: Summary) {}
+// pub fn nofity<T: Summary>(item1: &T, item2: &T) {}
+
+// use std::fmt::Display;
+// multiple bound
+// pub fn notify(item: &(impl Summary + Display)) {}
+// or
+// pub fn notify<T: Summary + Display>(item: &T) {}
+// where clause
+// pub fn notify<T>(item: &T)
+// where T: Summary + Display
+// {}
+
+// return impl Trait
+fn return_summarizble() -> impl Summary {
+ Tweet {
+ username: String::from("horse_ebooks"),
+ content: String::from("of course, as you probably already know, people"),
+ reply: false,
+ retweet: false,
+ }
+}
diff --git a/traits/src/main.rs b/traits/src/main.rs
new file mode 100644
index 0000000..02e408b
--- /dev/null
+++ b/traits/src/main.rs
@@ -0,0 +1,26 @@
+use traits::{self, NewsArticle, Summary, Tweet, notify};
+
+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,
+ };
+
+ println!("1 new tweet: {}", 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 once again are the best \
+ hockey team in the NHL.",
+ ),
+ };
+
+ println!("New article available! {}", article.summarize());
+
+ notify(&article);
+}