Skip to content

Commit 4f770e8

Browse files
Using generics on structs
1 parent 88783b7 commit 4f770e8

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

rustlang_book/generics/src/main.rs

+20
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
1+
struct Point<X1, Y1> {
2+
x: X1,
3+
y: Y1,
4+
}
5+
6+
impl<X1,Y1> Point<X1,Y1> {
7+
fn mixup<X2, Y2> (self, other: Point<X2,Y2>) -> Point<X1, Y2> {
8+
Point {
9+
x: self.x,
10+
y: other.y,
11+
}
12+
}
13+
}
114
fn main() {
215
let number_list = vec![34,57,87,64,98,100];
316

@@ -8,6 +21,13 @@ fn main() {
821

922
let result = largest(&char_list);
1023
println!("The largest char is {}", result);
24+
25+
let p1 = Point { x: 5, y: 10.4};
26+
let p2 = Point { x: "Hello", y: 'c' };
27+
28+
let p3 = p1.mixup(p2);
29+
30+
println!("p3.x = {}, p3.y = {}", p3.x, p3.y);
1131
}
1232

1333
fn largest<T: std::cmp::PartialOrd>(list: &[T]) -> &T {

0 commit comments

Comments
 (0)