diff --git a/main.rs b/main.rs index 4c11629..d3f51c5 100644 --- a/main.rs +++ b/main.rs @@ -1,58 +1,162 @@ -struct Car { - color: String, - transmission: Transmission, - convertible: bool, - age: (Age, u32), +use std::fmt::Display; + +fn add>(a: T, b: T) -> T { + a + b } -#[derive(PartialEq, Debug)] -enum Age { - New, - Used, +fn largest(list: &[T]) -> T { + let mut largest = list[0]; + + for &item in list.iter() { + if item > largest { + largest = item; + } + } + + largest } -#[derive(PartialEq, Debug)] -enum Transmission { - Manual, - SemiAuto, - Automatic, +fn create_and_print() +where + T: From + Display, +{ + let a: T = 100.into(); + println!("{}", a); } -fn car_factory(color: String, transmission: Transmission, convertible: bool, miles: u32) -> Car { - Car { - color, - transmission, - convertible, - age: car_quality(miles), +struct Point { + x: T, + y: U, +} + +impl Point { + fn mixup(self, other: Point) -> Point { + Point { + x: self.x, + y: other.y, + } } } -fn car_quality(miles: u32) -> (Age, u32) { - if miles > 100 { - return (Age::Used, miles); +impl Point { + fn distance_from_origin(&self) -> f64 { + ((self.x.pow(2) + (self.y as i32).pow(2)) as f64).sqrt() } +} - (Age::New, miles) +// fn display_array(arr: &[i32]) { +// println!("{:?}", arr); +// } +// fn display_array(arr: &[T]) { +// println!("{:?}", arr); +// } +fn display_array(arr: &[T; N]) { + println!("{:?}", arr); +} + +struct Buffer { + data: [u8; N], +} + +const fn compute_buffer_size(factor: usize) -> usize { + 1024 * factor +} + +pub trait Summary { + fn summarize(&self) -> String; +} + +pub struct Post { + pub title: String, + pub author: String, + pub content: String, +} + +impl Summary for Post { + fn summarize(&self) -> String { + format!("{} by {}", self.title, self.author) + } +} + +pub struct Weibo { + pub username: String, + pub content: String, + pub created_at: String, +} + +impl Summary for Weibo { + fn summarize(&self) -> String { + format!("{}: {}", self.username, self.content) + } +} + +pub fn notify(item: &impl Summary) { + println!("Breaking news! {}", item.summarize()); +} + +pub fn notify_plus(item: &T, item2: &T) { + println!("Breaking news! {}", item.summarize()); + println!("Breaking news! {}", item2.summarize()); } fn main() { - println!("Hello, world"); - let colors = ["Blue", "Green", "Red", "Silver"]; - let mut car = car_factory(String::from(colors[0]), Transmission::Automatic, false, 0); - println!( - "Car order 1: {:?}, Hard top = {}, {:?}, {}, {} miles", - car.age.0, car.color, car.convertible, car.color, car.age.1 - ); + // let a = 1; + // let b = 1; + let a = 1.2; + let b = 1.4; + println!("{}", add(a, b)); + create_and_print::(); - car = car_factory(String::from(colors[1]), Transmission::SemiAuto, false, 1); - println!( - "Car order 1: {:?}, Hard top = {}, {:?},{:?}, {}, {} miles", - car.age.0, car.color, car.convertible, car.transmission, car.color, car.age.1 - ); + let p1 = Point { x: 5, y: 10.4 }; + let p2 = Point { x: "Hello", y: 'c' }; - car = car_factory(String::from(colors[2]), Transmission::Manual, false, 101); - println!( - "Car order 1: {:?}, Hard top = {}, {:?}, {}, {} miles", - car.age.0, car.color, car.convertible, car.color, car.age.1 - ); + let p3 = p1.mixup(p2); + println!("p3.x = {}, p3.y = {}", p3.x, p3.y); + + let p4 = Point { x: 5, y: 10.4 }; + println!("{}", p4.distance_from_origin()); + + let arr = [1, 2, 3, 4, 5]; + display_array(&arr); + + let arr: [i32; 3] = [1, 2, 3]; + display_array(&arr); + + const SIZE: usize = compute_buffer_size(10); + let buffer = Buffer { data: [0; SIZE] }; + println!("{:?}", buffer.data.len()); + + let post = Post { + title: String::from("Post Title"), + author: String::from("Post Author"), + content: String::from("Post Content"), + }; + println!("{}", post.summarize()); + + let weibo = Weibo { + username: String::from("Weibo User"), + content: String::from("Weibo Content"), + created_at: String::from("2021-01-01"), + }; + println!("{}", weibo.summarize()); + notify(&post); + notify(&weibo); + + let post2 = Post { + title: String::from("Post Title 2"), + author: String::from("Post Author 2"), + content: String::from("Post Content 2"), + }; + // notify_plus(&post, &weibo); + notify_plus(&post, &post2); + + let number_list = vec![34, 50, 25, 100, 65]; + + let result = largest(&number_list); + println!("The largest number is {}", result); + + let char_list = vec!['y', 'm', 'a', 'q']; + + let result = largest(&char_list); + println!("The largest char is {}", result); } diff --git a/minigrep/output.txt b/minigrep/output.txt new file mode 100644 index 0000000..45a4c42 --- /dev/null +++ b/minigrep/output.txt @@ -0,0 +1,5 @@ +Searching for to +In file poem.txt +Case sensitive +Are you nobody, too? +How dreary to be somebody!