-
Notifications
You must be signed in to change notification settings - Fork 18
/
generics_trait.rs
95 lines (78 loc) · 1.79 KB
/
generics_trait.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
/**
* 找数字列表中最大的数字
*/
fn largest(list: &[i32]) -> i32 {
let mut largest = list[0];
for &item in list.iter() {
if item > largest {
largest = item;
}
}
largest
}
/**
* 泛型实现找列表中最大值
*/
fn generics_largest<T: PartialOrd>(list: &[T]) -> &T {
let mut largest = &list[0];
for item in list.iter() {
if item > largest {
largest = item;
}
}
&largest
}
// 结构体泛型
struct Point<T, U> {
x: T,
y: U,
}
// 方法中定义泛型,必须在 impl 后面声明 T
impl<T, U> Point<T, U> {
fn x(&self) -> &T {
&self.x
}
// 结构体定义中的泛型类型参数并不总是与结构体方法签名中使用的泛型是同一类型
fn mixup<V, W>(self, other: Point<V, W>) -> Point<T, W> {
Point {
x: self.x,
y: other.y,
}
}
}
/**
* trait
*/
trait Arithmetic {
fn add(&self) -> i32;
}
struct MyPoint {
x: i32,
y: i32,
}
// 为类型实现trait
impl Arithmetic for MyPoint {
fn add(&self) -> i32 {
self.x + self.y
}
}
// trait 作为参数
fn sum(item: impl Arithmetic) {
println!("sum {}", item.add());
}
fn main() {
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 = generics_largest(&char_list);
println!("The largest char is {}", result);
let p = Point { x: 5, y: 1.0 };
println!("p.x = {}", p.x());
let p2 = Point { x: "Hello", y: 'c'};
let p3 = p.mixup(p2);
println!("p3.x = {}, p3.y = {}", p3.x, p3.y);
let mp = MyPoint { x: 5, y: 10 };
println!("mp.x + mp.y: {}", mp.add());
sum(mp);
}