From faa108dc5c84f5c4aecee35a2f490eeedc85cb0d Mon Sep 17 00:00:00 2001 From: garhve Date: Tue, 3 Jan 2023 19:05:48 +0800 Subject: add files --- .../generic-type/Cargo.toml | 8 ++++ .../generic-type/src/main.rs | 45 ++++++++++++++++++++++ .../non-generic-largest.rs | 34 ++++++++++++++++ .../traits/Cargo.toml | 8 ++++ .../traits/src/lib.rs | 29 ++++++++++++++ .../traits/src/main.rs | 23 +++++++++++ 6 files changed, 147 insertions(+) create mode 100644 rust/theBook/chapter-10-generic-types-traits-lifetimes/generic-type/Cargo.toml create mode 100644 rust/theBook/chapter-10-generic-types-traits-lifetimes/generic-type/src/main.rs create mode 100644 rust/theBook/chapter-10-generic-types-traits-lifetimes/non-generic-largest.rs create mode 100644 rust/theBook/chapter-10-generic-types-traits-lifetimes/traits/Cargo.toml create mode 100644 rust/theBook/chapter-10-generic-types-traits-lifetimes/traits/src/lib.rs create mode 100644 rust/theBook/chapter-10-generic-types-traits-lifetimes/traits/src/main.rs (limited to 'rust') diff --git a/rust/theBook/chapter-10-generic-types-traits-lifetimes/generic-type/Cargo.toml b/rust/theBook/chapter-10-generic-types-traits-lifetimes/generic-type/Cargo.toml new file mode 100644 index 0000000..8f1e556 --- /dev/null +++ b/rust/theBook/chapter-10-generic-types-traits-lifetimes/generic-type/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "generic-type" +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/rust/theBook/chapter-10-generic-types-traits-lifetimes/generic-type/src/main.rs b/rust/theBook/chapter-10-generic-types-traits-lifetimes/generic-type/src/main.rs new file mode 100644 index 0000000..8a3ef34 --- /dev/null +++ b/rust/theBook/chapter-10-generic-types-traits-lifetimes/generic-type/src/main.rs @@ -0,0 +1,45 @@ +#[derive(Debug)] +struct Point { + x: T, + y: U, +} + +// T and U are declared for impl for it goes with struct +impl Point { + fn new(value: T, v: U) -> Self { + Point {x: value, y: v} + } + + // X1 and Y1 are only relavent to method mixup + fn mixup(self, other: Point) -> Point { + Point {x: self.x, y: other.y} + } +} + +fn largest(list: &[T]) -> &T { + let mut largest = &list[0]; + + for item in list { + if largest < item { + largest = item; + } + } + largest +} + +fn main() { + let x = [1,3,5,7,9]; + let y = ['a','b','c','d','e']; + + let result = largest(&x); + println!("{result}"); + + let result = largest(&y); + println!("{result}"); + + let result = Point::new(3.5,7); + println!("{},{}",result.x, result.y); + + let result = result.mixup(Point::new("hello",'c')); + println!("{},{}",result.x, result.y); +} diff --git a/rust/theBook/chapter-10-generic-types-traits-lifetimes/non-generic-largest.rs b/rust/theBook/chapter-10-generic-types-traits-lifetimes/non-generic-largest.rs new file mode 100644 index 0000000..149561e --- /dev/null +++ b/rust/theBook/chapter-10-generic-types-traits-lifetimes/non-generic-largest.rs @@ -0,0 +1,34 @@ +fn largest_i32(list: &[i32]) -> &i32 { + let mut largest = &list[0]; + + for item in list { + if largest < item { + largest = item; + } + } + + largest +} + +fn largest_char(list: &[char]) -> &char { + let mut largest = &list[0]; + + for item in list { + if largest < item { + largest = item; + } + } + + largest +} + +fn main() { + let l1 = [1,3,5,7,9]; + + let l2 = ['a','b','c','d','e']; + + let m1 = largest_i32(&l1); + let m2 = largest_char(&l2); + + println!("{m1}, {m2}"); +} diff --git a/rust/theBook/chapter-10-generic-types-traits-lifetimes/traits/Cargo.toml b/rust/theBook/chapter-10-generic-types-traits-lifetimes/traits/Cargo.toml new file mode 100644 index 0000000..460f45f --- /dev/null +++ b/rust/theBook/chapter-10-generic-types-traits-lifetimes/traits/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "traits" +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/rust/theBook/chapter-10-generic-types-traits-lifetimes/traits/src/lib.rs b/rust/theBook/chapter-10-generic-types-traits-lifetimes/traits/src/lib.rs new file mode 100644 index 0000000..45c93c8 --- /dev/null +++ b/rust/theBook/chapter-10-generic-types-traits-lifetimes/traits/src/lib.rs @@ -0,0 +1,29 @@ +pub trait Summary { + fn summarize (&self) -> String; +} + +pub struct NewsArticle { + pub author: String, + pub headline: String, + pub location: String, + pub content: String, +} + +impl Summary for NewsArticle { + fn summarize(&self) -> String { // return String ownership. &self means self: &Self + format!("{} by {} ({})", self.headline, self.author, self.location) + } +} + +pub struct Tweet { + pub username: String, + pub content: String, + pub retweet: bool, + pub reply: bool, +} + +impl Summary for Tweet { + fn summarize(&self) -> String { // &Self is the type owns impl + format!("{}: {}",self.username, self.content) + } +} diff --git a/rust/theBook/chapter-10-generic-types-traits-lifetimes/traits/src/main.rs b/rust/theBook/chapter-10-generic-types-traits-lifetimes/traits/src/main.rs new file mode 100644 index 0000000..f0229a0 --- /dev/null +++ b/rust/theBook/chapter-10-generic-types-traits-lifetimes/traits/src/main.rs @@ -0,0 +1,23 @@ +use traits::{Tweet, Summary, NewsArticle}; + +fn main() { + let tweet = Tweet { + username: String::from("horse_ebook"), + content: String::from( + "of course, as you probably already know, people." + ), + reply: false, + retweet: false, + }; + + println!("1 new tweet: {}", tweet.summarize()); + + let article = NewsArticle { + author: String::from("garhve"), + headline: String::from("how to die"), + location: String:: from("United Kingdom"), + content: String::from("This is how we gonna die"), + }; + + println!("1 new article: {}",article.summarize()); +} -- cgit v1.2.3-70-g09d2