@@ -17,104 +17,95 @@ typedef Counter {
1717 max_value: i32;
1818}
1919
20- // Implement methods for Point
21- impl Point {
22- // Method that calculates distance to another point
23- fn distance_to (other: Point ) - > f32 {
24- let dx = self .x - other .x ;
25- let dy = self .y - other .y ;
26- return dx * dx + dy * dy; // Squared distance (avoiding sqrt)
27- }
28-
29- // Method that modifies the point (moves it)
30- fn mut move_by (dx : f32 , dy : f32 ) {
31- self .x = self .x + dx;
32- self .y = self .y + dy;
33- }
34-
35- // Method that returns a coordinate
36- fn get_x () - > f32 {
37- return self .x ;
38- }
39-
40- fn get_y () - > f32 {
41- return self .y ;
42- }
20+ // Method that calculates distance to another point
21+ fn Point .distance_to (other: Point ) - > f32 {
22+ let dx = self .x - other .x ;
23+ let dy = self .y - other .y ;
24+ return dx * dx + dy * dy; // Squared distance (avoiding sqrt)
25+ }
4326
44- // Method that checks if point is at origin
45- fn is_at_origin () - > f32 {
46- if self .x == 0.0 && self .y == 0.0 {
47- return 1.0 ; // true
48- } else {
49- return 0.0 ; // false
50- }
51- }
27+ // Method that modifies the point (moves it)
28+ fn mut Point .move_by (dx: f32, dy: f32) {
29+ self .x = self .x + dx;
30+ self .y = self .y + dy;
5231}
5332
54- // Implement methods for Rectangle
55- impl Rectangle {
56- // Calculate area
57- fn area () - > f32 {
58- return self .width * self .height ;
59- }
60-
61- // Calculate perimeter
62- fn perimeter () - > f32 {
63- return 2.0 * (self .width + self .height );
64- }
65-
66- // Check if it's a square
67- fn is_square () - > f32 {
68- if self .width == self .height {
69- return 1.0 ; // true
70- } else {
71- return 0.0 ; // false
72- }
73- }
74-
75- // Scale the rectangle
76- fn mut scale (factor : f32 ) {
77- self .width = self .width * factor;
78- self .height = self .height * factor;
79- }
33+ // Method that returns a coordinate
34+ fn Point .get_x () - > f32 {
35+ return self .x ;
36+ }
37+
38+ fn Point .get_y () - > f32 {
39+ return self .y ;
8040}
8141
82- // Implement methods for Counter
83- impl Counter {
84- // Increment counter
85- fn mut increment () {
86- if self .value < self .max_value {
87- self .value = self .value + 1 ;
88- }
42+ // Method that checks if point is at origin
43+ fn Point .is_at_origin () - > f32 {
44+ if self .x == 0.0 && self .y == 0.0 {
45+ return 1.0 ; // true
46+ } else {
47+ return 0.0 ; // false
8948 }
90-
91- // Decrement counter
92- fn decrement () {
93- if self .value > 0 {
94- self .value = self .value - 1 ;
95- }
49+ }
50+
51+ // Calculate area
52+ fn Rectangle .area () - > f32 {
53+ return self .width * self .height ;
54+ }
55+
56+ // Calculate perimeter
57+ fn Rectangle .perimeter () - > f32 {
58+ return 2.0 * (self .width + self .height );
59+ }
60+
61+ // Check if it's a square
62+ fn Rectangle .is_square () - > f32 {
63+ if self .width == self .height {
64+ return 1.0 ; // true
65+ } else {
66+ return 0.0 ; // false
9667 }
97-
98- // Reset counter
99- fn mut reset () {
100- self .value = 0 ;
68+ }
69+
70+ // Scale the rectangle
71+ fn mut Rectangle .scale (factor: f32) {
72+ self .width = self .width * factor;
73+ self .height = self .height * factor;
74+ }
75+
76+ // Increment counter
77+ fn mut Counter .increment () {
78+ if self .value < self .max_value {
79+ self .value = self .value + 1 ;
10180 }
102-
103- // Check if counter is at maximum
104- fn is_at_max () - > f32 {
105- if self .value == self .max_value {
106- return 1.0 ; // true
107- } else {
108- return 0.0 ; // false
109- }
81+ }
82+
83+ // Decrement counter
84+ fn Counter .decrement () {
85+ if self .value > 0 {
86+ self .value = self .value - 1 ;
11087 }
111-
112- // Get percentage
113- fn percentage () - > f32 {
114- return self .value * 100.0 / self .max_value ;
88+ }
89+
90+ // Reset counter
91+ fn mut Counter .reset () {
92+ self .value = 0 ;
93+ }
94+
95+ // Check if counter is at maximum
96+ fn Counter .is_at_max () - > f32 {
97+ if self .value == self .max_value {
98+ return 1.0 ; // true
99+ } else {
100+ return 0.0 ; // false
115101 }
116102}
117103
104+ // Get percentage
105+ fn Counter .percentage () - > f32 {
106+ return self .value * 100.0 / self .max_value ;
107+ }
108+
118109// Using type methods
119110let mut p1 = new Point { x: 1.0 , y: 2.0 };
120111let p2 = new Point { x: 4.0 , y: 6.0 };
0 commit comments