fizzbuzz interview question
This commit is contained in:
parent
a3136725b5
commit
bb36d8fe90
45 changed files with 26 additions and 8 deletions
34
src/main.rs
34
src/main.rs
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue