this post was submitted on 15 May 2024
47 points (100.0% liked)

Programmer Humor

32557 readers
311 users here now

Post funny things about programming here! (Or just rant about your favourite programming language.)

Rules:

founded 5 years ago
MODERATORS
 

cross-posted from: https://lemmy.zip/post/15495372

you are viewing a single comment's thread
view the rest of the comments
[–] [email protected] 1 points 6 months ago (2 children)

Sounds better than coming up with the most mindfucking ways to please the borrowchecker in a large rust codebase

(Skill issue probably, i know)

[–] [email protected] 3 points 6 months ago

Rule of thumb, which I feel gets you 80% there:

If you store data in a struct, you want that struct to have ownership of that data. So, avoid storing references in structs.
If you need to pass data into a function, you usually want to pass it as a reference.

This makes it so you have your data stored in some place with the ownership and from there you just pass data down into functions as references. It forces you to structure your program like a tree (which is often a very good idea to begin with).

[–] [email protected] 1 points 6 months ago

Yeah, it took me a bit to wrap my head around it. It's worth it to avoid subtle, weird, and hard to diagnose bugs later on.