Issue
Excerpt from Huon Wilson's Finding Closure in Rust:
Capturing entirely by value is also strictly more general than capturing by reference: the reference types are first-class in Rust, so "capture by reference" is the same as "capture a reference by value". Thus, unlike C++, there’s little fundamental distinction between capture by reference and by value, and the analysis Rust does is not actually necessary: it just makes programmers’ lives easier.
I struggle to wrap my head around this. If you capture a reference "by value", don't you capture the data that is stored on the heap? Or does it refer to the pointer value of the reference, which is found on the stack?
Solution
Or does it refer to the pointer value of the reference, which is found on the stack?
Yes, ish.
In Rust, references are reified, they're an actual thing you manipulate. So when you capture a reference by value, you're capturing the reference itself (the pointer, which is what a Rust reference really is), not the referee (the pointee).
Capturing by reference basically just implicitly creates a reference and captures it by value.
Answered By - Masklinn Answer Checked By - Gilberto Lyons (PHPFixing Admin)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.