this post was submitted on 05 Jul 2025
305 points (99.0% liked)

Programmer Humor

24815 readers
277 users here now

Welcome to Programmer Humor!

This is a place where you can post jokes, memes, humor, etc. related to programming!

For sharing awful code theres also Programming Horror.

Rules

founded 2 years ago
MODERATORS
you are viewing a single comment's thread
view the rest of the comments
[–] kamikazerusher@lemmy.world 18 points 2 days ago (2 children)

This is my experience every time I return to learning rust. I’m guessing that if I used it more often than once a quarter with hobby projects I’d stop falling into the same traps.

[–] boonhet@sopuli.xyz 11 points 1 day ago (1 children)

I find that the error messages themselves are a great tool for learning when it comes to Rust.

[–] kamikazerusher@lemmy.world 4 points 1 day ago (1 children)

Eh, I’m not entirely sold on that idea.

I think they do a good job of pointing out “this is a behavior/feature of Rust you need to understand.” However they can send you down the wrong path of correction.

The compiler error mentioning static lifetime specifiers of &str demonstrates both. It indicates to the developer that ownership and scopes will play a significant role when defining and accessing data. The error though will guide them towards researching how to define static lifetimes and possibly believe that they will need to set this in their functions and structs. Each time you look at questions about this error in places like Stack Overflow with example code you’ll find suggested solutions explaining that a manually-defined static lifetime isn’t necessary to resolve the problem.

[–] magic_lobster_party@fedia.io 2 points 1 day ago

Static lifetimes confused me when I started learning rust. The error message guides the developer to the wrong direction.

It took me a while to realize that just using Arc is sufficient in most of those cases.

[–] Ephera@lemmy.ml 7 points 1 day ago (1 children)

Yeah, these become a lot less relevant with routine.

  • Avoiding the main-thread panicking is mostly just a matter of not using .unwrap() and .expect().

  • String vs. &str can mostly be solved by generally using owned datatypes (String) for storing in structs and using references (&str) for passing into function parameters. It does still happen that you forget the & at times, but that's then trivial to solve (by just adding the &).

  • "temporary value dropped while borrowed" can generally be avoided by not passing references outside of your scope/function. You want to pass the owned value outside. Clone, if you have to.

  • "missing lifetime specifier" is also largely solved by not storing references in structs.

[–] kamikazerusher@lemmy.world 5 points 1 day ago

The last two points are the kind of design advice I need to see. I’m probably so used to the C/C++ concept of passing by reference to prevent copies of complex data being generated that I forget how Rust’s definition of a reference is different.