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
|
#[derive(Debug)]
struct Point<T, U> {
x: T,
y: U,
}
// T and U are declared for impl for it goes with struct
impl<T, U> Point<T,U> {
fn new(value: T, v: U) -> Self {
Point {x: value, y: v}
}
// X1 and Y1 are only relavent to method mixup
fn mixup<X1, Y1>(self, other: Point<X1, Y1>) -> Point<T,Y1> {
Point {x: self.x, y: other.y}
}
}
fn largest<T: std::cmp::PartialOrd>(list: &[T]) -> &T {
let mut largest = &list[0];
for item in list {
if largest < item {
largest = item;
}
}
largest
}
fn main() {
let x = [1,3,5,7,9];
let y = ['a','b','c','d','e'];
let result = largest(&x);
println!("{result}");
let result = largest(&y);
println!("{result}");
let result = Point::new(3.5,7);
println!("{},{}",result.x, result.y);
let result = result.mixup(Point::new("hello",'c'));
println!("{},{}",result.x, result.y);
}
|