This is supplementary/separate from the Twitch Streams (see sidebar for links), intended for discussion here on lemmy.
The idea being, now that both twitch streams have read Chapter 4, we can have a discussion here and those from the twitch streams can have a retrospective or re-cap on the topic.
This will be a regular occurrence for each discrete set of topics coming out of The Book as the twitch streams cover them
Ownership and the borrow checker are obviously a fundamental and unique topic to rust, so it’s well worth getting a good grounding in AFAICT.
- Anyone up to trying to summarise or explain ownership/borrow-checker in rust?
- it can be a good exercise to test your understanding and get feedback/clarification from others … as well as probably a good way to teach others
- Any persistent gripes, difficulties or confusions?
- Any of the quizzes from The Book stump you?
- Any hard learnt lessons? Or tried and true tips?
I said this during my stream of 4.2 (I think): reading about the explicit “Flow” permission was a wonderful validation of my own internalized representation of how variables/lifetimes behave with regards to function calls. Things “flow” into functions, and the only things that “flow out” are what is part of the explicit return value. Deriving this base set of assumptions gives you why you can’t just return, from a function, a reference/borrow of data created / memory allocated inside the function call: you need to have the referenced data “flow out” as well.
Persistent gripes
So much time is spent talking about double frees, use-after-frees, and pointers in general yet we never stop to acquire or review what they definitively look like in practice. It feels to me like The Book ends up specifically assuming you have some prior knowledge of low-level/assembly and/or experience implementing a compiler(s), despite it claiming to be agnostic as to your prior programming language in its intro:
I understand if the rust internals are too complex to serve as support for introducing lifetimes, but I wish we got equivalent C code or maybe were shown the compiled output for examples illustrating each part of the chapter. For example, if we could be shown how function calls result in stack frames being pushed & popped beyond just the (more abstract) diagrams we already have, or see some
malloc()
and more importantlyfree()
calls. Or, at least, see some example memory addresses used in the diagrams so that we can figure out for ourselves which pointers are invalid when, instead of having the arrows in the diagrams keep track of the addresses for us.tried and true tips
^ this really seems to keep ownership problems: to a minimum, and non-existant during exploratory coding / brainstorming phases.
hard learnt lessons
I am not smart enough to expect to be able to write, first try, rust code that does 0 superfluous copies of data. Attempting to do so always results in going in circles fighting the borrow checker for up to an entire day, before I give up and take the approach I mention above [and often enough end up solving the problem in under half an hour].
Yep. I’m with you on all of that!
The pitching of The Book is definitely off (this my attempt to write a basic intro to the borrow checker, just to see where my own brain was at but also out of a somewhat fanciful interest in what a better version could look like).
I wonder if the lack of C or assembly equivalents is because the internals aren’t stable??
And yea, optimising data copies on the first go seems to be a trap (for me too!)
Do you know if there are any good tools for analysing the hot spots of data copying?
@Jayjader @brokenix The ownership notion :
This is yet another key information regarding data that :
• A CPU is completely ignorant / agnostic about it.
• Nor the address space notion glued to the CPU bus notion.
• Even at MMU / PMMU level, it’s still unknown.
CPU are such a low level engine concept, same as CPU buses. When you think that all this ownership notion is just an abstract one maintained only through code self-generated by the C compiler. So dangerous. So risky.
@Jayjader @brokenix
cc FYI : The intetesting short thread above, regarding C compilers.
@theruran @haitchfive @50htz @vidak
@Jayjader @brokenix @theruran @haitchfive @50htz @vidak Here I’m not revolting at compilers, I’m revolting at all of is who never complained about this.
The only excuse we have is that by that time, making our own CPU/VLSI was so costy that this was like a glass ceiling for us not to challenge the CPU concept. I want you to step back and realize how this fact just completely jailed our minds with current CPU concept, pushing
@Jayjader @brokenix @theruran @haitchfive @50htz @vidak all of us to accept the inacceptable with both CPU and compilers.
Because if you step back, ownship should never have been implemented in software code tricks by a compiler, it’s an ugly thing, because insecure. These things should be securely handled in hardware, by the CPU. It’s a design fault.
@Jayjader @brokenix @theruran @haitchfive @50htz @vidak I want you to realize HOW FAR the idea we couldn’t not invent, design, route, produce our own CPU has deeply limited our perception of what computers could alternatively be. This fact has put us into high security conceptual jails for decades. And we have just escaped and are still in a fully readaptation phase with this new freedom acquired.