How would `move()` expressions work with nested closures and captured variables?
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 119k
- Forks
- 16.1k
- PR merge metrics
- PR metrics pending
Description
@nikomatsakis, I was wondering how move() expression would work with nested closures.
Context: Ergonomic ref-counting: RFC decision and preview (Rust Project Goals page)
Would this work?
let data = Rc::new(vec![1, 2, 3]);
outer_closure(move || {
for i in 0..3 {
inner_closure(move || {
takes_data_by_value(move(data.share()))
});
}
});
When is move(data.share()) evaluated?
Is the data variable moved into the outer closure by the outer move ||, but then move(data.share()) is evaluated each time an inner closure is created (3 times in the loop) (i.e., the outer closure owns data, and each inner closure gets a freshly shared handle).
Or is the move(data.share()) expression evaluated once when the outer closure is created (not when each inner closure is created). This would mean I'd need to add an explicit intermediate binding:
let data = Rc::new(vec![1, 2, 3]);
outer_closure(move || {
let data_handle = move(data.share()); // Evaluated once.
for i in 0..3 {
inner_closure(move || {
takes_data_by_value(move(data_handle.share())) // Evaluated 3 times.
});
}
});
Is one of these the intended behavior?
Also, I wasn't sure where to post this question, so let me know if I should repost it somewhere else.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the linked Ergonomic ref-counting RFC decision and preview and the Rust Project Goals page. Resolve when move(data.share()) is evaluated in nested closures and document the intended behavior for both examples.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- compilers
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Quiet
- Clarity
- Needs clarification
- Newbie friendliness
- 20/100