5 ms·
Also interesting is to figure out how Rust deals with closures, which reference the parent scopes, and how the corresponding (cyclic) data structures are manage
by amelius 9y ago
Also interesting is to figure out how Rust deals with closures, which reference the parent scopes, and how the corresponding (cyclic) data structures are managed.
Does Rust eliminate the cycles by copying? (expensive, and doesn't allow for writing)
- Retra 9y agoWhy would there be cycles? Rust captures closure environments however you like. You can copy them, share them, reference them, whatever.
- kazinator 9y agoWhy would be there be cycles when you have closures? Because a function's environment can end up having a reference back to the same function. This can be set up without assignment, given just a lambda operator. Hint: look in domain name in the URL in the browser address bar.
- viraptor 9y agoIf you used it in a way that allows the closure to escape the scope, you'd get one of the "foo can't outlive bar in score ...". (On a mobile, can't write an example easily)
- deleted 9y ago[deleted]
- kazinator 9y agoIf I can't have escaping closures, I want to be working in C.
- bombela 9y agoyou can have "escaping" closure. But you cannot return something that outlives it's lifetime. So the closure must own data, not merely refer to it. The online Rust book should habe the details.
- viraptor 9y agoYou can use unsafe if you want to feel like working with C :-) There's nothing stopping you from doing things you know are correct. You just sometimes need to tell the compiler that you know better and guarantee that the code is fine.
- pjmlp 9y agoA good example are event handlers on GUI widgets.
- nicoburns 9y agoIt tracks the individual variables in the scope that the closure captures. This shouldn't introsuce cycles in general, no?
- kazinator 9y agoAn individual variable, as such, can be a function value, such that the variable is visible in that function's environment.
- gsg 9y agoMutually recursive functions can be closure converted without cycles by deriving closure values from each other rather than storing them in each other.
- kazinator 9y agoEnvironment copying ruses are revealed when something mutates a lexical variable, and the mutation doesn't appear everywhere as it should. (So you have to ban that.)
- gsg 9y agoFirst, deriving is just pointer arithmetic and doesn't copy anything. Second, standard flat closure representations already involve copying parts of environments, with any sharing problems addressed by assignment conversion (turning variables that are assigned to into mutable cells, a reference to which can be copied into however many environments is necessary).
- kazinator 9y ago> standard flat closure representations already involve copying parts of environments, with any sharing problems addressed by assignment conversion (turning variables that are assigned to into mutable cells) In fact, the simple assoc list representation of environments (whereby simple consing extends the environment) does this. It doesn't eliminate circularity. If a function has a certain binding in scope, and that binding refers back to the function, you can shuffle that binding around between different environment vectors all you want. Wherever you stick that binding, as long as the binding is in scope of that function, you have circularity.
- dbaupp 9y agoA closure in Rust is just a struct containing its captures either by value or as & or &mut references pointing to the values. Thus, making/avoiding a cycle with them is the same as doing it for "normal" values. One can't accidentally get a reference-counting cycle (values aren't reference counted unless they're put into a pointer explicitly), and a cycle of shared/mutable references won't stop deallocation (and, if it's a cycle that's unsafe, it won't compile).