Small Example

Here is a small example program in Rust:

  1. fn main() { // Program entry point
  2. let mut x: i32 = 6; // Mutable variable binding
  3. print!("{x}"); // Macro for printing, like printf
  4. while x != 1 { // No parenthesis around expression
  5. if x % 2 == 0 { // Math like in other languages
  6. x = x / 2;
  7. } else {
  8. x = 3 * x + 1;
  9. }
  10. print!(" -> {x}");
  11. }
  12. println!();
  13. }

The code implements the Collatz conjecture: it is believed that the loop will always end, but this is not yet proved. Edit the code and play with different inputs.

Key points:

  • Explain that all variables are statically typed. Try removing i32 to trigger type inference. Try with i8 instead and trigger a runtime integer overflow.

  • Change let mut x to let x, discuss the compiler error.

  • Show how print! gives a compilation error if the arguments don’t match the format string.

  • Show how you need to use {} as a placeholder if you want to print an expression which is more complex than just a single variable.

  • Show the students the standard library, show them how to search for std::fmt which has the rules of the formatting mini-language. It’s important that the students become familiar with searching in the standard library.