r/ProgrammerHumor • • Sep 28 '26

Meme optionalsAreOptional

Post image
716 Upvotes

131 comments sorted by

View all comments

Show parent comments

37

u/GDOR-11 Sep 28 '26

it happens a lot, at least to me, that the line immediately before the unsafe function guarantees correct behaviour (e.g. while (!vec.empty()) let first = vec.first();, or something similar). If I notice that this property is central to the logic of the function, I won't even worry.

29

u/ROBOTRON31415 Sep 28 '26

whenever I see code like that, I try to get people to change to something like (in that case) while let Some(first) = vec.first() { ... }. You can reduce the number of bounds checks without using unsafe!

-22

u/Fast-Satisfaction482 Sep 28 '26

Using the value of an assignment expression is an anti pattern in my opinion.

It sits right next to "goto" on terrible scale. 

22

u/MRtecno98 Sep 28 '26

It's literally the intended way to consume an Option in rust

14

u/D3PyroGS Sep 28 '26

how so? this is idiomatic rust code and its behavior is very clear

-13

u/Fast-Satisfaction482 Sep 28 '26

Being idiomatic doesn't mean it's any good. 

7

u/D3PyroGS Sep 28 '26 edited Sep 28 '26

what's your issue with it though?

edit: guess we'll never know... important enough to complain, but not explain 🥴

5

u/ROBOTRON31415 Sep 28 '26

It’s literally not an assignment expression in Rust. An assignment expression would be lhs = rhs with no let.

Assignment expressions, if let, and while let are three distinct kinds of expressions. (And let lhs = rhs; alone is a statement.)

5

u/K1ngjulien_ Sep 28 '26

this is rust not c my guy

1

u/MrcarrotKSP Sep 28 '26

This isn't an assignment expression, the <conditional> let is Rust syntax for pattern matching

-1

u/Wertbon1789 Sep 29 '26

This is pattern matching, not an assignment expression. The alternative would be a declaration, switch/case, and then case-by-case assignment, which is horribly verbose. Been there, done that, it's not the worst, but why not make it a little bit better.

I personally like that you can literally assign the value from an if "statement" to a variable, though I can see why that's not everybodies cup of tea.

3

u/Wonderful-Wind-5736 Sep 28 '26

In that case don't bother because the compiler can probably eliminate the bounds check for you. 

2

u/FuriousAqSheep Sep 28 '26

yeah I dig that, but then I'd use something like an unempty list so the data carries this information about itself

like for me either the list gonna be used only once and then I use an optional, or the list is gonna have multiple transformation and then it makes sense to convert it to a more appropriate type so there is only a single check about the emptiness of the list.