Skip to content
Playground(another site)

bynk.types.question_outside_result

Error ? used in a function that does not return a Result.

? returns early with the Err, so the function around it must be able to return one: its return type has to be a Result, or an Effect of one. In a function returning Int the error has nowhere to go. Change the return type to Result[T, E] and wrap the final value in Ok, or deal with the Result where it arises, with a match or .getOrElse(…).

Example

fn checked(n: Int) -> Result[Int, String] {
if n >= 0 { Ok(n) } else { Err("negative") }
}
fn run(n: Int) -> Int {
let v = checked(n)? // ✗ `run` does not return a `Result`
v + 1
}
fn run(n: Int) -> Result[Int, String] {
let v = checked(n)? // ✓
Ok(v + 1)
}

In the Book

See also

  • The grammar rule it governs: question_expr.
  • Every code in its category, Type checking, on the diagnostic index.
  • bynk explain bynk.types.question_outside_result shows the same from the installed toolchain, without a network.