News for hackers. Stuff that matters.
Re:Why is Rust difficult? by pjmlp on Monday January 22, 2018 @02:20AM (#16202503)
Try to use the borrow checker of this decade to write callbacks in GUI frameworks, without polluting the code with Rc and RefCell, even though it is obvious there is only one path to the data being used.
Re:Why is Rust difficult? by Rusky on Monday January 22, 2018 @03:18AM (#16202762)
Callbacks by definition create multiple paths to the data.

(You can also often get away with Cell rather than RefCell.)

Re:Why is Rust difficult? by pjmlp on Monday January 22, 2018 @03:40AM (#16202874)
> Callbacks by definition create multiple paths to the data.

How come, when you have a struct method, accessing field members only visible to that specific method.

Something that is very easy to do with moving in lambda contexts in C++.

Re:Why is Rust difficult? by Rusky on Monday January 22, 2018 @04:55PM (#16208309)
Because the closure itself contains a reference either to the struct or to its fields. That's the second path, and it can be invalidated by the first path either a) freeing the struct or b) changing its "shape" in memory (resizing a `Vec`, changing enum variants, etc.)

You can alternatively move the struct or its fields into the closure, but then you lose the first path. I know you're not doing that because if you were you wouldn't be having lifetime problems (and you wouldn't be able to share the state across event handlers, so it's not a great solution anyway).

Re:Why is Rust difficult? by pmarcelll on Monday January 22, 2018 @11:23AM (#16205552)
The quoted sentence is not true, but most practical callbacks will create multiple paths to objects via borrows. Or you can move your structs into closures (like in C++). But I think what you originally wrote (using callbacks without Rc/RefCell) is fundamentally hard. You have to guarantee somehow that the struct is still alive when you access it though the borrow in the callback, otherwise you have a memory safety problem.
Re:Why is Rust difficult? by pjmlp on Monday January 22, 2018 @12:39PM (#16206227)
The situation I was having issues is explicitly acknowledged by the NLL RFC as being an issue.

It is related to closure desugaring.

https://github.com/nikomatsakis/nll-rfc/blob/master/0000-non...