fizzbuzz interview question
This commit is contained in:
parent
a3136725b5
commit
bb36d8fe90
34
src/main.rs
34
src/main.rs
|
@ -1,10 +1,28 @@
|
||||||
fn main() {
|
fn main() {
|
||||||
let s1: &str = "Hello";
|
fizzbuzz_to(20); // defined below. No forward declaration needed.
|
||||||
println!("s1: {s1}");
|
}
|
||||||
|
|
||||||
let mut s2: String = String::from("Hello");
|
fn is_divisible_by(lhs: u32, rhs: u32) -> bool {
|
||||||
println!("s2: {s2}");
|
// returns a boolean
|
||||||
|
if rhs == 0 {
|
||||||
s2.push_str(s1);
|
return false;
|
||||||
println!("s2: {s2}");
|
}
|
||||||
|
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.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading…
Reference in New Issue