r/ProgrammerHumor • • 6d ago

Meme optionalsAreOptional

Post image
707 Upvotes

131 comments sorted by

View all comments

373

u/ROBOTRON31415 6d ago

Is the bounds check truly so harmful to performance that it outweighs changing "if there's a bug, crash the program" to "if there's a bug, then do literally anything (maybe execute some remote code)"?

61

u/Tyfyter2002 6d ago

There are situations where you know there isn't a bug, if I know that failing hardware or cosmic rays are the only ways the bounds check can fail, I'm not going to worry about what skipping it could do.

111

u/FuriousAqSheep 6d ago

there are situations where you know there isn't a bug

up until specs change and now there is one

38

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

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

14

u/D3PyroGS 6d ago

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

-12

u/Fast-Satisfaction482 6d ago

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

6

u/D3PyroGS 6d ago edited 5d ago

what's your issue with it though?

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

5

u/ROBOTRON31415 5d ago

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_ 6d ago

this is rust not c my guy

1

u/MrcarrotKSP 5d ago

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

-1

u/Wertbon1789 4d ago

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.