Never Type
The never type, written !, is the type of expressions that never produce a value.
Expressions of type ! include:
returnexpressionsbreakexpressionscontinueexpressions- Infinite loops
Type Coercion
A type coercion is an implicit type conversion that occurs automatically during type checking. Rue has exactly one coercion: the never type coerces to any type.
When type checking requires a value of type T, a value of type ! is accepted. This allows diverging expressions to appear in any context where a value is expected.
fn test(x: i32) -> i32 {
// `return 100` has type !, which coerces to i32
let y = if x > 5 { return 100 } else { x };
y * 2
}
fn main() -> i32 {
test(3) + test(10) // 6 + 100 = 106
}
When both branches of an if expression or all arms of a match expression have type !, the entire expression has type !.
fn diverges(x: i32) -> i32 {
// Both branches return, so the if has type !
// This coerces to i32 (the function's return type)
if x > 0 { return 1 } else { return 0 }
}
fn main() -> i32 { diverges(5) }
Diverging Functions
A function with return type ! never returns normally.
Memory Representation
The never type is a zero-sized type. See Zero-Sized Types for the general definition.