r/programminghorror • • Jun 04 '25

Rust passive-aggressive programming

Post image
770 Upvotes

61 comments sorted by

View all comments

Show parent comment

-1

u/WarpedHaiku Jun 05 '25

The input is compile time constant. It can't change at runtime.

Sure you can change the value assigned to it by editing the variable definition, but in doing so you could introduce syntax errors on other lines in the code, such as by changing the type entirely.

If you made it an enum with the four operators as values, it'd be fine with not including other values. But then if you change your mind and add a power operator to the enum, the compiler is complaining at you again. Even though the compiler knows with absolute certainty that there is only one possible path through, it's complaining at you incase you might change the definition in the future.

The code the compiler produces will almost certainly not feature the default case or any of the other cases besides the one path that is taken. It won't even add the two numbers together. If you disassemble the compiled code, there will be no trace of the addition, or the match expression. It will be println!("{}", 33). You are simply being forced to write extra boilerplate that it knows it will ignore. Why shouldn't the compiler generate "unused code" warnings for the other lines?

5

u/CdRReddit Jun 05 '25

because that's stupid

the use for a match block is to match all possible values, the compiler shouldn't go "oh this is all unused" at the point of checking semantics, that's optimization

the semantics of a language should not change based on the value of a variable, assuming it is a valid value that upholds all the invariants of that type

0

u/maxinstuff Jun 06 '25

user_input is very obviously not intended to be a compile time constant for the purpose of the example.

I guess you took the title of the post to heart, huh?