this post was submitted on 31 Jan 2025
74 points (98.7% liked)

Rust

6299 readers
89 users here now

Welcome to the Rust community! This is a place to discuss about the Rust programming language.

Wormhole

[email protected]

Credits

  • The icon is a modified version of the official rust logo (changing the colors to a gradient and black background)

founded 2 years ago
MODERATORS
you are viewing a single comment's thread
view the rest of the comments
[–] [email protected] 7 points 1 day ago

In a struct, you typically want only owned data types. If you're passing something as a parameter to a function, you typically want only references.

In a sense, that pushes your program to be very tree-shaped. Your data is initialized and held at the root of the tree (or a sub-tree) and only temporarily passed down as a reference.

In my experience, following this rule gets you there for 80% of programs. Only really more complex programs or libraries may need to deal with lifetimes and the various cop-out mechanisms.
If you're hurting your brain, that's probably because you're experienced in other languages and have design patterns in mind that don't work with this model.