Skip to content

Commit b221b54

Browse files
committed
use match instead of if-else
1 parent 99302ad commit b221b54

File tree

1 file changed

+30
-104
lines changed

1 file changed

+30
-104
lines changed

src/majcn/math.rs

+30-104
Original file line numberDiff line numberDiff line change
@@ -4,116 +4,42 @@ pub trait DigitCounter {
44

55
impl DigitCounter for u32 {
66
fn count_digits(self) -> usize {
7-
if self < 10 {
8-
return 1;
7+
match self {
8+
0..10 => 1,
9+
10..100 => 2,
10+
100..1000 => 3,
11+
1000..10000 => 4,
12+
10000..100000 => 5,
13+
100000..1000000 => 6,
14+
1000000..10000000 => 7,
15+
10000000..100000000 => 8,
16+
_ => 9,
917
}
10-
11-
if self < 100 {
12-
return 2;
13-
}
14-
15-
if self < 1000 {
16-
return 3;
17-
}
18-
19-
if self < 10000 {
20-
return 4;
21-
}
22-
23-
if self < 100000 {
24-
return 5;
25-
}
26-
27-
if self < 1000000 {
28-
return 6;
29-
}
30-
31-
if self < 10000000 {
32-
return 7;
33-
}
34-
35-
if self < 100000000 {
36-
return 8;
37-
}
38-
39-
9
4018
}
4119
}
4220

4321
impl DigitCounter for u64 {
4422
fn count_digits(self) -> usize {
45-
if self < 10 {
46-
return 1;
47-
}
48-
49-
if self < 100 {
50-
return 2;
51-
}
52-
53-
if self < 1000 {
54-
return 3;
55-
}
56-
57-
if self < 10000 {
58-
return 4;
59-
}
60-
61-
if self < 100000 {
62-
return 5;
63-
}
64-
65-
if self < 1000000 {
66-
return 6;
23+
match self {
24+
0..10 => 1,
25+
10..100 => 2,
26+
100..1000 => 3,
27+
1000..10000 => 4,
28+
10000..100000 => 5,
29+
100000..1000000 => 6,
30+
1000000..10000000 => 7,
31+
10000000..100000000 => 8,
32+
100000000..1000000000 => 9,
33+
1000000000..10000000000 => 10,
34+
10000000000..100000000000 => 11,
35+
100000000000..1000000000000 => 12,
36+
1000000000000..10000000000000 => 13,
37+
10000000000000..100000000000000 => 14,
38+
100000000000000..1000000000000000 => 15,
39+
1000000000000000..10000000000000000 => 16,
40+
10000000000000000..100000000000000000 => 17,
41+
100000000000000000..1000000000000000000 => 18,
42+
_ => 19,
6743
}
68-
69-
if self < 10000000 {
70-
return 7;
71-
}
72-
73-
if self < 100000000 {
74-
return 8;
75-
}
76-
77-
if self < 1000000000 {
78-
return 9;
79-
}
80-
81-
if self < 10000000000 {
82-
return 10;
83-
}
84-
85-
if self < 100000000000 {
86-
return 11;
87-
}
88-
89-
if self < 1000000000000 {
90-
return 12;
91-
}
92-
93-
if self < 10000000000000 {
94-
return 13;
95-
}
96-
97-
if self < 100000000000000 {
98-
return 14;
99-
}
100-
101-
if self < 1000000000000000 {
102-
return 15;
103-
}
104-
105-
if self < 10000000000000000 {
106-
return 16;
107-
}
108-
109-
if self < 100000000000000000 {
110-
return 17;
111-
}
112-
113-
if self < 1000000000000000000 {
114-
return 18;
115-
}
116-
117-
19
11844
}
11945
}

0 commit comments

Comments
 (0)