diff --git a/book/aggregator/Cargo.toml b/book/aggregator/Cargo.toml new file mode 100644 index 0000000..27d4889 --- /dev/null +++ b/book/aggregator/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "aggregator" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/book/aggregator/src/lib.rs b/book/aggregator/src/lib.rs new file mode 100644 index 0000000..241f81c --- /dev/null +++ b/book/aggregator/src/lib.rs @@ -0,0 +1,49 @@ +pub trait Summary { + fn summarise_author(&self) -> String; + + fn summarise(&self) -> String { + format!("(Read more from {}...)", self.summarise_author()) + } +} + +pub struct NewsArticle { + pub headline: String, + pub location: String, + pub author: String, + pub content: String, +} + +impl Summary for NewsArticle { + fn summarise_author(&self) -> String { + format!("@{}", self.author) + } + // fn summarise(&self) -> String { + // format!("{}, by {} ({})", self.headline, self.author, self.location) + // } +} + +pub struct Tweet { + pub username: String, + pub content: String, + pub reply: bool, + pub retweet: bool, +} + +impl Summary for Tweet { + fn summarise_author(&self) -> String { + format!("@{}", self.username) + } + // + // fn summarise(&self) -> String { + // format!("{}: {}", self.username, self.content) + // } +} + +fn returns_summarise() -> impl Summary { + Tweet { + username: String::from("horse_ebooks"), + content: String::from("of course, as you probably already know, people"), + reply: false, + retweet: false, + } +} \ No newline at end of file diff --git a/book/aggregator/src/main.rs b/book/aggregator/src/main.rs new file mode 100644 index 0000000..3b74c23 --- /dev/null +++ b/book/aggregator/src/main.rs @@ -0,0 +1,56 @@ +use aggregator::{NewsArticle, Summary, Tweet}; + +fn largest(list: &[T]) -> T { + let mut largest = list[0]; + + for &item in list { + if item > largest { + largest = item; + } + } + + largest +} + +fn largest_no_copy(list: &[T]) -> &T { + let mut largest = list.get(0).expect("oh no"); + + for item in list { + if item > largest { + largest = item; + } + } + + largest +} + +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: \n\t{}", tweet.summarise()); + + 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: \n\t{}", article.summarise()); + + let number_list = vec![34, 50, 25, 100, 65]; + let result = largest_no_copy(&number_list); + println!("The largest number is {}", result); + + let char_list = vec!['y', 'm', 'a', 'q']; + let result = largest_no_copy(&char_list); + println!("The largest char is {}", result); +} \ No newline at end of file