I’ve been learning Rust via the book and a great article I found on linked lists [1]. Coming from C++ the lifetimes/borrows concepts make sense at a high level but the practical details seem to get pretty crazy. If anyone here knows Rust well does the OP article have a good take or is it missing something?
For this example id probably accumulate the score total in a local variable. Then once iterating over all the children i would call parent.add_score() with the accumulated total
This article says that the borrow checker doesn't look past functions signatures because of compiler performance. I strongly disagree. The reason is to avoid coupling. If it did, you couldn't swap 2 functions with the same signature because their implementation would have a different borrowing pattern. Very bad.
(Although we're a bit there with functions returning an impl)
It's a bit of both IIRC. You're right that limiting checks to the function signature avoids accidentally leaking implementation details, but it also means that checking functions can be done entirely locally. Not having to recursively inspect called function implementations to determine whether there is a type/borrow checking error scales much worse than only needing to look at function signatures.
5 comments
[ 2.9 ms ] story [ 21.3 ms ] thread[1] https://rust-unofficial.github.io/too-many-lists/
(Although we're a bit there with functions returning an impl)