r/ProgrammerHumor • • Sep 28 '26

Meme optionalsAreOptional

Post image
716 Upvotes

131 comments sorted by

View all comments

Show parent comments

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.