aboutsummaryrefslogtreecommitdiff
path: root/rust/raindrops/src
diff options
context:
space:
mode:
Diffstat (limited to 'rust/raindrops/src')
-rw-r--r--rust/raindrops/src/lib.rs28
1 files changed, 28 insertions, 0 deletions
diff --git a/rust/raindrops/src/lib.rs b/rust/raindrops/src/lib.rs
new file mode 100644
index 0000000..811c569
--- /dev/null
+++ b/rust/raindrops/src/lib.rs
@@ -0,0 +1,28 @@
1struct Drops {
2 num: u32,
3 drops: Option<String>
4}
5
6impl Drops {
7 pub fn new(num: u32) -> Drops {
8 Drops { num, drops: None }
9 }
10
11 pub fn add_if_factor(&mut self, div: u32, suffix: &str) {
12 if self.num % div == 0 {
13 self.drops.get_or_insert(String::new()).push_str(suffix);
14 }
15 }
16
17 pub fn get(self) -> String {
18 self.drops.unwrap_or(self.num.to_string())
19 }
20}
21
22pub fn raindrops(n: u32) -> String {
23 let mut drops = Drops::new(n);
24 drops.add_if_factor(3, "Pling");
25 drops.add_if_factor(5, "Plang");
26 drops.add_if_factor(7, "Plong");
27 drops.get()
28}