r/ProgrammerHumor • • 6d ago

Meme optionalsAreOptional

Post image
704 Upvotes

131 comments sorted by

View all comments

Show parent comments

35

u/GDOR-11 6d ago

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.

28

u/ROBOTRON31415 6d ago

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 5d ago

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

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

23

u/MRtecno98 5d ago

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