repl.it
@shahinism/

FizzBuzz Rust

Rust

No description

fork
loading
Files
  • main.rs
  • main
main.rs
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
fn is_divisible(number: i32, by: i32) -> bool {
    number % by == 0
}

fn to_fizz_buzz<'a>(number: i32) -> String {
    match number {
        n if is_divisible(n, 3) && is_divisible(n, 5) => "FizzBuzz".to_string(),
        n if is_divisible(n, 3) => "Fizz".to_string(),
        n if is_divisible(n, 5) => "Buzz".to_string(),
        n => n.to_string()
    }
}

pub fn fizz_buzz(number: i32) -> Vec<String> {
    let range: Vec<i32> = (1..number + 1).collect();
    range.into_iter().map(to_fizz_buzz).collect()
}


fn main() {
    assert_eq!(fizz_buzz(15), vec!["1", "2", "Fizz", "4", "Buzz", "Fizz", "7", "8",
                                       "Fizz", "Buzz", "11", "Fizz", "13", "14", "FizzBuzz"]);
    println!("Success: testing compress!");
}
Fetching token
?