-
Notifications
You must be signed in to change notification settings - Fork 0
/
Homework4.rs
135 lines (114 loc) · 2.56 KB
/
Homework4.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/**
@author 韩峰 - 08 - Team1
@date 2021-08-13
*/
use std::f64::consts::PI;
use std::fmt::Debug;
// HW1:为枚举交通信号灯实现一个trait,trait里包含一个返回时间的方法,不同灯持续时间不同
pub trait Time {
fn time(&self) -> u8;
}
pub enum TrafficLight {
Red,
Green,
Yellow,
}
impl Time for TrafficLight {
fn time(&self) -> u8 {
match self {
Self::Red => 70,
Self::Green => 60,
Self::Yellow => 3,
}
}
}
//HW2:实现一个函数,为u32类型的整数集合求和,参数类型为&[u32],返回类型为Option<u32>,溢出时返回None
pub fn sum(arr: &[u32]) -> Option<u32> {
arr.iter().try_fold(0u32,|acc,&x| acc.checked_add(x))
}
//HW3:实现一个打印图形面积的函数,接收一个可以计算面积的类型作为参数,需要用到泛型和泛型约束
pub fn print_area<T: Area + Debug>(item: T) {
println!("{:?} area is {}",item, item.area());
}
pub trait Area {
fn area(&self) -> f64;
}
#[derive(Debug)]
pub struct Circle {
radius: f64,
}
impl Circle {
pub fn new(radius: f64) -> Self {
Circle {
radius,
}
}
}
impl Area for Circle {
fn area(&self) -> f64 {
PI * self.radius.powf(2.0)
}
}
#[derive(Debug)]
pub struct Square {
width: f64,
length: f64,
}
impl Square {
pub fn new(width: f64,length:f64) -> Self {
Square {
width,
length,
}
}
}
impl Area for Square {
fn area(&self) -> f64 {
self.width*self.length
}
}
#[derive(Debug)]
pub struct Triangel {
base: f64,
height: f64,
}
impl Triangel {
pub fn new(base: f64,height:f64) -> Self {
Triangel {
base,
height,
}
}
}
impl Area for Triangel{
fn area(&self) -> f64 {
self.base*self.height*0.5f64
}
}
//HW1
pub fn test_traffic_light() {
let light = TrafficLight::Red;
println!("red time is {}", light.time());
let light = TrafficLight::Green;
println!("green time is {}", light.time());
let light = TrafficLight::Yellow;
println!("yellow time is {}", light.time());
}
//HW2
pub fn test_sum() {
let arr1: [u32; 5] = [1,2,3,4,5];
assert_eq!(Some(15), sum(&arr1));
let arr2 : [u32; 2] = [1, u32::max_value()];
assert_eq!(None, sum(&arr2));
}
//HW3
pub fn test_print_area() {
print_area(Circle::new(5.0));
print_area(Square::new(4.0, 5.0));
print_area(Triangel::new(4.0, 5.0));
}
fn main() {
test_traffic_light();
test_sum();
test_print_area();
}