docs

你正在阅读 v0.1.0 的文档。Nabi 0.1.0 已不再提供支持。 最新版本: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