Functions use fn, parameter types, and return types. The last expression without semicolon is the return value—use return for early exit.
Example
fn add(a: i32, b: i32) -> i32 { a + b }
Important interview questions and answers
- Q: Expression vs statement?
A: Expressions produce values; adding;turns the last line into a statement returning().
Self-check
- How do you return a value without
return?
Ownership in parameters
Parameters can take ownership (String), borrow immutably (&T), or borrow mutably (&mut T). The signature documents the contract—callers know if they lose ownership.
Pitfall
Adding ; to the last expression returns unit () instead of the value you meant to return.