Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Consume owned data while borrowing local state

For a callback that also borrows local state, invoke it in that local scope:

def main() -> Result[(), AllocError | IoError | OverflowError[i64]]:
    mut count = 0
    values = [8]?
    take = def once [values, &mut count]() -> Result[list[i64], OverflowError[i64]]:
        count = count + 1
        Ok(values)
    result = take()?
    print(count)?
    print(result)?
    Ok(())
1
[8]

The borrow ends after the call. This callback cannot be passed to callbacks.run because a borrowing environment cannot move across function boundaries.