11

Is anyone else super annoyed that rust doesn’t have an increment operator??? Why did they get rid of it????

Comments
  • 3
    The most funny part is the explanation “the probability of making mistakes is lower”. 😂
  • 11
    Two possible bug classes come to mind:

    1) mixing up *ptr++ and (*ptr)++
    2) mistaking var++ as atomic operation.
  • 5
    @JS96 Post-increment is a very weird operator and I still fail to see why it was a good idea, but it's one of those things you learn to watch out for and it ceases to be a problem after a while.
  • 3
    @homo-lorens It's useful for pointer load/store operations with post increment, which is often a CPU instruction, and some CPUs had inc/dec as opcode.

    Early C compilers were not able to see that optimisation with the increment moved out to a separate statement.
  • 8
    There's actually a good reason Rust doesn't have an increment operator

    They didn't want *both* post-increment and pre-increment because they easily lead to errors and Rust is all about correctness. So if they only implemented one, pre-increment would have been the obvious choice, but then you run into the problem of choosing between `++x`, which is quite unintuitive when you don't also have `x++`,
    or making `x++` be pre-increment but then that's the opposite of what you would expect, again leading to bugs
  • 3
    Yes. I’m also angry that Ruby doesn’t implement it.
  • 1
    @homo-lorens indeed it is just sugar syntax
  • 5
    And Rust++ was born
  • 1
    Not really. It's a bad operator, for reasons outlined above.

    I strongly dislike rust but they got this decision right, and for the right reasons.
  • 3
    Oh, and there's another one: side effects as arguments in macros. That's a common pitfall in C/C++, in particular because you sometimes don't know whether something is implemented as function or macro, or that might change due to code changes elsewhere.

    The solution is "don't use side effects in arguments", but that relies on the dev's discipline or experience.
  • 1
    @Fast-Nop assert() is a great example of this being an issue. Absolutely good point.
  • 1
    @junon Or stuff like isalpha(), tolower() etc. which may be functions, macros, or -hold on- even both (see https://softwareengineering.stackexchange.com/...).

    That's fun when you port existing code from a platform where that used to be a function to another target where it happens to evaluate to a macro.
Add Comment