twelve_days/
lib.rs

1//! twelve_days
2//!
3//! Helpful functions for the twelve days of Christmas song
4//! coding challenge from the Rust book
5
6#[allow(unused)]
7/// Convert any number from 1-12 to its String equivalent using if/else
8///
9/// # Examples
10/// ```
11/// let num = 11;
12///
13/// let result = twelve_days::make_string(num);
14///
15/// assert_eq!("11th", result);
16/// ```
17pub fn make_string(num: i32) -> String {
18    let first = String::from("1st");
19    let second = String::from("2nd");
20    let third = String::from("3rd");
21    let fourth = String::from("4th");
22    let fifth = String::from("5th");
23    let sixth = String::from("6th");
24    let seventh = String::from("7th");
25    let eighth = String::from("8th");
26    let ninth = String::from("9th");
27    let tenth = String::from("10th");
28    let eleventh = String::from("11th");
29    let twelfth = String::from("12th");
30
31    if num == 1 {
32       first
33    } else if num == 2  {
34        second
35    } else if num == 3  {
36        third
37    } else if num == 4  {
38        fourth
39    } else if num == 5  {
40        fifth
41    } else if num == 6  {
42        sixth
43    } else if num == 7  {
44        seventh
45    } else if num == 8  {
46        eighth
47    } else if num == 9  {
48        ninth
49    } else if num == 10  {
50        tenth
51    } else if num == 11  {
52        eleventh
53    } else if num == 12  {
54        twelfth            
55    }else {
56        num.to_string()
57    }
58}
59/// Convert any number from 1 -12 to its String equivalent using match
60///
61/// # Examples
62/// ```
63/// let num = 5;
64///
65/// let result = twelve_days::match_num(num);
66///
67/// assert_eq!("5th", result);
68/// ```
69pub fn match_num(num: i32) -> String {
70    match num {
71        1 => String::from("1st"),
72        2 => String::from("2nd"),
73        3 => String::from("3rd"),
74        4 => String::from("4th"),
75        5 => String::from("5th"),
76        6 => String::from("6th"),
77        7 => String::from("7th"),
78        8 => String::from("8th"),
79        9 => String::from("9th"),
80        10 => String::from("10th"),
81        11 => String::from("11th"),
82        12 => String::from("12th"),
83        _ => num.to_string(),
84    }
85}
86
87
88#[cfg(test)]
89mod tests {
90    use super::*;
91
92    #[test]
93    fn make_string_twelfth() {
94        let result = make_string(12);
95        assert_eq!(result, "12th");
96    }
97    #[test]
98    fn match_one() {
99        let result = match_num(1);
100        assert_eq!(result, "1st");
101    }
102    #[test]
103    fn match_two() {
104        let result = match_num(2);
105        assert_eq!(result, "2nd");
106    }
107    #[test]
108    fn match_twelve() {
109        let result = match_num(12);
110        assert_eq!(result, "12th");
111    }
112}