Functions

A Rust version of the famous FizzBuzz interview question:

1. fn main() {
2. fizzbuzz_to(20);   // Defined below, no forward declaration needed
3. }

5. fn is_divisible_by(lhs: u32, rhs: u32) -> bool {
6. if rhs == 0 {
7. return false;  // Corner case, early return
8. }
9. lhs % rhs == 0     // The last expression in a block is the return value
10. }

12. fn fizzbuzz(n: u32) -> () {  // No return value means returning the unit type `()`
13. match (is_divisible_by(n, 3), is_divisible_by(n, 5)) {
14. (true,  true)  => println!("fizzbuzz"),
15. (true,  false) => println!("fizz"),
16. (false, true)  => println!("buzz"),
17. (false, false) => println!("{n}"),
18. }
19. }

21. fn fizzbuzz_to(n: u32) {  // `-> ()` is normally omitted
22. for i in 1..=n {
23. fizzbuzz(i);
24. }
25. }
  • We refer in main to a function written below. Neither forward declarations nor headers are necessary.
  • Declaration parameters are followed by a type (the reverse of some programming languages), then a return type.
  • The last expression in a function body (or any block) becomes the return value. Simply omit the ; at the end of the expression.
  • Some functions have no return value, and return the ‘unit type’, (). The compiler will infer this if the -> () return type is omitted.
  • The range expression in the for loop in fizzbuzz_to() contains =n, which causes it to include the upper bound.
  • The match expression in fizzbuzz() is doing a lot of work. It is expanded below to show what is happening.

(Type annotations added for clarity, but they can be elided.)

let by_3: bool = is_divisible_by(n, 3); let by_5: bool = is_divisible_by(n, 5); let by_35: (bool, bool) = (by_3, by_5); match by_35 { // ...