Introduction
In this lab, we learn how to write functions in Rust. Functions are declared using the fn
keyword, and their arguments are type annotated. The return type, if applicable, is specified after an arrow ->
. The final expression in the function serves as the return value, but the return
statement can be used to return a value earlier. The lab provides an example of rewriting the FizzBuzz program using functions, demonstrating the use of functions that return boolean values, functions that return the unit type ()
, and functions without specified return types.
Note: If the lab does not specify a file name, you can use any file name you want. For example, you can use
main.rs
, compile and run it withrustc main.rs && ./main
.