? passes an error or a missing value up to the caller, so the function itself must return
T! or T?. Otherwise handle the failure here with else or match.
Wrong#
fn parse(s: str): int {
let n = int.parse(s)?
n
}Fixed#
fn parse(s: str): int {
let n = int.parse(s) else 0
n
}Common causes#
- Forgetting the
!in the return type:fn parse(s: str): int!. fn main()without a return type: writefn main(): ()!to let errors reach the top.?inside a closure passed tomap: the closure's own return type matters.