本頁尚未翻譯,以下為英文原文。
#E0039: nested item uses an outer local
A nested fn, struct or impl cannot use the locals or generic parameters of the function around it. Pass the value as a parameter, or use a closure, which can capture it.
nabi
fn wrap<T>(t: T) -> i32 {
struct Holder { v: T; }
0
}
error[E0039]: `Holder` is declared inside `wrap` and can't use `T`, a generic parameter of `wrap`; declare its own instead: `struct Holder<T>` --> main.nabi:2:24 | 2 | struct Holder { v: T; } | ^
Give the nested item its own parameter, or declare it outside the function:
nabi
struct Holder<T> { v: T; }
fn wrap<T>(t: T) -> Holder<T> {
Holder { v: t }
}
println(f"{wrap(5).v}");
5
A nested fn that uses a local of the function around it gets the same code; a closure (let add = |x: i32| x + base;) can capture the local.
Related: E0027 cannot find value or function, E0058 invalid closure capture
Found a mistake? Edit this page on GitHub. Last updated