#E0020: 尾隨閉包中的 return
陳述式的尾隨閉包中單獨的 return 只會離開閉包,而不會離開外層的函式。請用 return@name 離開閉包(name 是接收這個閉包的函式),要離開外層函式則在呼叫之後寫程式碼處理。
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
用 return@each 離開閉包,名稱取自接收這個閉包的函式:
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
相關錯誤碼:E0059 用在其語境之外的關鍵字
發現錯誤? 在 GitHub 上編輯此頁 最後更新: