This repository has been archived on 2025-04-22. You can view files and clone it, but cannot push or open issues or pull requests.
rust-test/main.rs

163 lines
3.4 KiB
Rust

use std::fmt::Display;
fn add<T: std::ops::Add<Output = T>>(a: T, b: T) -> T {
a + b
}
fn largest<T: PartialOrd + Copy>(list: &[T]) -> T {
let mut largest = list[0];
for &item in list.iter() {
if item > largest {
largest = item;
}
}
largest
}
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() {
// let a = 1;
// let b = 1;
let a = 1.2;
let b = 1.4;
println!("{}", add(a, b));
create_and_print::<i64>();
let p1 = Point { x: 5, y: 10.4 };
let p2 = Point { x: "Hello", y: 'c' };
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);
}