重构主函数,添加泛型和特征实现,增加 Point 结构体及其方法,添加 Post 和 Weibo 结构体及其摘要功能,新增输出文件

This commit is contained in:
rsgl 2024-11-26 22:47:54 +08:00
parent 5490f28e43
commit 3c4e77cb9d
2 changed files with 150 additions and 41 deletions

192
main.rs
View File

@ -1,58 +1,162 @@
struct Car {
color: String,
transmission: Transmission,
convertible: bool,
age: (Age, u32),
use std::fmt::Display;
fn add<T: std::ops::Add<Output = T>>(a: T, b: T) -> T {
a + b
}
#[derive(PartialEq, Debug)]
enum Age {
New,
Used,
}
fn largest<T: PartialOrd + Copy>(list: &[T]) -> T {
let mut largest = list[0];
#[derive(PartialEq, Debug)]
enum Transmission {
Manual,
SemiAuto,
Automatic,
}
fn car_factory(color: String, transmission: Transmission, convertible: bool, miles: u32) -> Car {
Car {
color,
transmission,
convertible,
age: car_quality(miles),
for &item in list.iter() {
if item > largest {
largest = item;
}
}
fn car_quality(miles: u32) -> (Age, u32) {
if miles > 100 {
return (Age::Used, miles);
largest
}
(Age::New, miles)
fn create_and_print<T>()
where
T: From<i32> + Display,
{
let a: T = 100.into();
println!("{}", a);
}
struct Point<T, U> {
x: T,
y: U,
}
impl<T, U> Point<T, U> {
fn mixup<V, W>(self, other: Point<V, W>) -> Point<T, W> {
Point {
x: self.x,
y: other.y,
}
}
}
impl Point<i32, f64> {
fn distance_from_origin(&self) -> f64 {
((self.x.pow(2) + (self.y as i32).pow(2)) as f64).sqrt()
}
}
// fn display_array(arr: &[i32]) {
// println!("{:?}", arr);
// }
// fn display_array<T: std::fmt::Debug>(arr: &[T]) {
// println!("{:?}", arr);
// }
fn display_array<T: std::fmt::Debug, const N: usize>(arr: &[T; N]) {
println!("{:?}", arr);
}
struct Buffer<const N: usize> {
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<T: Summary>(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::<i64>();
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);
}

5
minigrep/output.txt Normal file
View File

@ -0,0 +1,5 @@
Searching for to
In file poem.txt
Case sensitive
Are you nobody, too?
How dreary to be somebody!