docs

You are reading the docs for v0.1.0. Nabi 0.1.0 is no longer supported. Latest release: v0.1.3 →

#E0020: return in a trailing closure

A bare return in a statement's trailing closure would leave only the closure, not the function around it. Leave the closure with return@name (name is the function the closure is passed to), and the function with code after the call.

nabi
fn each(xs: List<i32>, f: fn(i32) -> ()) { for x in xs { f(x); } }

fn main() {
    each([1, 2, 3]) |x| {
        if x == 2 { return; }
        println(f"{x}");
    }
}
error[E0020]: a bare `return` in a statement's trailing closure: it would leave only the closure, not the function
  --> main.nabi:5:21
   |
 5 |         if x == 2 { return; }
   |                     ^^^^^^
   |
   = help: write `return@each` to end this call of the closure; to leave the function, write a `for` loop instead of the call

Leave the closure with return@each, named after the function it is passed to:

nabi
fn each(xs: List<i32>, f: fn(i32) -> ()) { for x in xs { f(x); } }

fn main() {
    each([1, 2, 3]) |x| {
        if x == 2 { return@each; }
        println(f"{x}");
    }
}
1
3

Related: E0059 keyword outside its context

Found a mistake? Edit this page on GitHub. Last updated