this post was submitted on 31 Jan 2026
280 points (99.0% liked)

Programmer Humor

29982 readers
1675 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
 
top 17 comments
sorted by: hot top controversial new old
[–] Ephera@lemmy.ml 46 points 3 weeks ago (3 children)

The rule of thumb I always tell people is that they should generally put owned data into struct fields and references into function parameters.

[–] calcopiritus@lemmy.world 17 points 3 weeks ago

Good rule of thumb. As long as it's not followed blindly of course.

Structs with lifetimes are often quite convenient. Especially for performance.

[–] joyjoy@lemmy.zip 10 points 3 weeks ago (1 children)

I recently learned you can pass a &String to a &str parameter, so that's neat. 

[–] Ephera@lemmy.ml 8 points 3 weeks ago (1 children)

Ah yeah, via deref coercion, which is also called "auto-dereferencing" at times. Not to be confused with "auto-referencing", which is also a thing^1.

You can do some wild shit with deref coercion. And when I say "wild", I guess, I'm talking about the most normal thing for Java devs, because well, it's a lot like inheritance. 😅

Basically, this concept of being able to pass &String into a parameter that takes &str also applies to the self parameter. Or in other words, methods implemented on str can also be called on String, as if String extends str.
And well, obviously you can also make use of that yourself, by writing your own wrapper type. You can even "override" existing methods in a sense by re-defining them in the wrapper type.

I had to play around a bit with it myself, so here's a playground: https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=af65ed396dec88c8406163acaa1f8f8d

[–] Ephera@lemmy.ml 6 points 3 weeks ago

Welp, I posted my hot take that impl Deref is similar to inheritance as a meme in !rust@lemmy.ml: https://lemmy.ml/post/42514248

Now, let's see how many feathers get ruffled. 🙃

[–] harrowhawk 5 points 3 weeks ago

I like this rule of thumb and it goes nicely with the “if you have more than 3 arguments to a function, consider making a struct to pass the arguments in” rule of thumb because

You can have the struct use a named lifetime (or several) for the different parameters so it doesn’t own them!

Best of both worlds.

[–] marcos@lemmy.world 34 points 3 weeks ago (1 children)

That's how the C++ code should have looked all the time. And the amount of people that get surprised and complain about this is just more evidence that nobody should write C++. Ever.

[–] someacnt@sh.itjust.works 13 points 3 weeks ago (1 children)

Exactly, C++ is what came to my mind when I saw this. Some people don't put references??

[–] Ephera@lemmy.ml 13 points 3 weeks ago (1 children)

I guess, if you come from garbage-collected languages, you might be used to not needing the ampersands, because everything is automatically a reference in those...

[–] XPost3000@lemmy.ml 6 points 3 weeks ago (1 children)

Yeah this is how it was for me when I first started C++, I was use to any object beyond a simple 3D vector to always be passed by reference

And then I read a C++ book my uncle gave me during a flight and realized that there isn't any syntax for passing a parameter by copy, so obviously that'd have to be the default behavior and I've been passing by reference ever since

[–] Ephera@lemmy.ml 6 points 3 weeks ago (2 children)

Oh wow, what the hell. I'm not actually familiar with C++ (just with Rust which gets similar reactions with the ampersands), but that's insane that it just copies shit by default. I guess, it comes from a time when people mostly passed primitive data types around the place. But yeah, you won't even notice that you're copying everything, if it just does it automatically.

And by the way, Rust did come up with a third meaning for passing non-references: It transfers the ownership of the object, meaning no copy is made and instead, the object is not anymore allowed to be used in the scope that passed it on.
That's true, except for data types which implement the Copy trait/interface, which is implemented mostly for primitive data types, which do then get treated like C++ apparently treats everything.

[–] zea_64@lemmy.blahaj.zone 6 points 3 weeks ago

It probably comes from C, which also copies by default, though it doesn't have copy constructors.

[–] KmlSlmk64@lemmy.world 4 points 3 weeks ago

There are also the move semantics in C++, which are similar to Rust's ownership transfer, but explicit and with some differences in how the data is actually handled under the hood

[–] kubica@fedia.io 10 points 3 weeks ago

you got a & yes, but what about a getting more & on top of your &'s?

[–] ArcaneSlime@lemmy.dbzer0.com 7 points 3 weeks ago

Hey I get that reference!

[–] HeyThisIsntTheYMCA@lemmy.world 6 points 3 weeks ago

who turned excel into a programming language?

[–] goatinspace@feddit.org 2 points 3 weeks ago

(⚈∇⚈ )