Return expressions
Syntax
ReturnExpression → return Expression?
return expression evaluates its operand and returns that value from the current function using copy/move semantics. return; returns the unit value (). Function tail expressions provide the return value when execution reaches the end of the function body without returning early. In all cases, the returned value must be compatible with the function’s declared return type.
A return expression diverges and has type !,
regardless of the type of the returned value.
fn absolute(x: i32) -> i32 {
if x < 0 {
return -x; // explicit early return
}
x // tail expression return
}
See Never type for typing and reachability.