blob: 811c569507739162619fcbcf9961e5770f290d58 (
plain) (
blame)
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
|
struct Drops {
num: u32,
drops: Option<String>
}
impl Drops {
pub fn new(num: u32) -> Drops {
Drops { num, drops: None }
}
pub fn add_if_factor(&mut self, div: u32, suffix: &str) {
if self.num % div == 0 {
self.drops.get_or_insert(String::new()).push_str(suffix);
}
}
pub fn get(self) -> String {
self.drops.unwrap_or(self.num.to_string())
}
}
pub fn raindrops(n: u32) -> String {
let mut drops = Drops::new(n);
drops.add_if_factor(3, "Pling");
drops.add_if_factor(5, "Plang");
drops.add_if_factor(7, "Plong");
drops.get()
}
|