-
Notifications
You must be signed in to change notification settings - Fork 0
/
24_generics.rs
89 lines (71 loc) · 1.67 KB
/
24_generics.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
#[derive(Debug)]
struct Point<T> {
x: T,
y: T,
}
impl <T> Point<T> {
fn x(&self) -> &T {
&self.x
}
}
impl Point<f32> {
fn number(&self) -> f32 {
self.x
}
}
impl Point<i32> {
fn number(&self) -> i32 {
self.x
}
}
trait Summary {
fn summarize(&self) -> String;
}
struct NewsArticle {
headline: String,
location: String,
author: String,
content: String,
}
impl Summary for NewsArticle {
fn summarize(&self) -> String {
format!("{}, by {} ({}) \n {}", self.headline, self.author, self.location, self.content)
}
}
fn main() {
let list = vec![23,54,65,67];
let result = largest(&list);
println!("{}", result);
let list = vec![223,544,655,67];
let result = largest(&list);
println!("{}", result);
let list = vec!['y','t','u'];
let result = largest(&list);
println!("{}", result);
let integer = Point{x:5, y:10};
let float = Point{x:8.0, y:9.4};
println!("{:?}\n{:?}", integer, float);
println!("{:?}", integer.x());
println!("{:?}", float.x());
let n = Point{x:2.2, y: 3.14};
println!("{}", n.number());
let n = Point{x:2, y: 3};
println!("{}", n.number());
// Traits
let news = NewsArticle {
headline: String::from("The title"),
location: String::from("The location"),
author: String::from("The author"),
content:String::from("The content"),
};
println!("News Article \n{}", news.summarize());
}
fn largest<T:PartialOrd+Copy>(list :&[T]) -> T {
let mut largest = list[0];
for n in list {
if n > &largest {
largest = *n;
}
}
largest
}