forked from alihoseiny/Rust-tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGenericStructExample.rs
More file actions
48 lines (37 loc) · 881 Bytes
/
GenericStructExample.rs
File metadata and controls
48 lines (37 loc) · 881 Bytes
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
use std::mem;
struct Point<T> {
x: T,
y: T
}
impl<Type> Point<Type> {
fn swap_coordinates(&mut self) {
mem::swap(&mut self.x, &mut self.y);
}
}
fn main() {
let pointer_u8 = Point::<u8> {
x: 10,
y: 12
};
let float_pointer = Point::<f32> {
x: 0.0,
y: 666.32
};
let detect_my_type = Point {
x: 10,
y: 11
};
let mut point_integer = Point {
x: 10,
y: 11
};
println!("Before swapping x: {} y: {}", point_integer.x, point_integer.y);
point_integer.swap_coordinates();
println!("After swapping x: {} y: {}", point_integer.x, point_integer.y);
let my_point2 = Point::<u8> {
x: 10,
y: 12
};
let swapped_point = swap_point::<u8>(my_point2);
println!("swapped point x: {}, y: {}", swapped_point.x, swapped_point.y);
}