IMPORTANT: To view this page as Markdown, append `.md` to the URL (e.g. /docs/manual/basics.md). For the complete Mojo documentation index, see llms.txt.
Skip to main content
Version: Nightly
For the complete Mojo documentation index, see llms.txt. Markdown versions of all pages are available by appending .md to any URL (e.g. /docs/manual/basics.md).

@__copy_capture

You can add the @__copy_capture decorator on a legacy closure to capture register-passable values by copy. This decorator causes a nested function to copy the value of the indicated variable into the closure object at the point of formation instead of capturing that variable by reference. This allows you to pass the closure as a parameter, but lifetimes aren't guaranteed to be respected.

def foo(x: Int):
var z = x

@__copy_capture(z)
@__parameter
def formatter() -> Int:
return z
z = 2
print(formatter())

def main():
foo(5)