fizzbuzz interview question

This commit is contained in:
Vincent Batts 2023-01-14 15:37:45 -05:00
parent a3136725b5
commit bb36d8fe90
45 changed files with 26 additions and 8 deletions

View File

@ -1,10 +1,28 @@
fn main() {
let s1: &str = "Hello";
println!("s1: {s1}");
let mut s2: String = String::from("Hello");
println!("s2: {s2}");
s2.push_str(s1);
println!("s2: {s2}");
fizzbuzz_to(20); // defined below. No forward declaration needed.
}
fn is_divisible_by(lhs: u32, rhs: u32) -> bool {
// returns a boolean
if rhs == 0 {
return false;
}
return lhs % rhs == 0;
}
fn fizzbuzz(n: u32) -> () {
// this returns nothing, so '()'
match (is_divisible_by(n, 3), is_divisible_by(n, 5)) {
(true, true) => println!("fizzbuzz"),
(true, false) => println!("fizz"),
(false, true) => println!("buzz"),
(false, false) => println!("{n}"),
}
}
fn fizzbuzz_to(n: u32) {
// like above, but you can omit the '()'
for n in 1..=n {
fizzbuzz(n);
}
}

Binary file not shown.