Curriculum Foundations Functions and expressions
Functions and expressions
Rust functions use explicit parameter and return types. Blocks can produce values, and the final expression has no semicolon. This is the first theme where a missing or extra semicolon changes meaning. Planned exercises: 1. choose fn syntax. 2. recognise -> return types. 3. choose final-expression return. 4. fill a parameter type. 5. fill a return type. 6. write a simple pure function. 7. write an if expression return. 8. translate TS helper functions. 9. fix a function where a semicolon accidentally returns ().
Exercises
9 ready
01
pick one
Pick the idiomatic Rust function declaration. Rust uses fn,
02
pick one
Pick the function that returns () — the unit type. A function
03
pick one
Pick the idiomatic Rust function body. The last expression
04
fill blanks
Fill the parameter type. The function adds one to a 32-bit
05
fill blanks
Fill the punctuation that introduces a function's return type.
06
type one line
Write the Rust function signature line. triple takes an
07
type one line
Write the tail expression of abs_diff — the function's return
08
write a program
Translate the TypeScript helper into a complete Rust program.
09
write a program
The TS reference computes a discount. Translate it into Rust.