The standard way to increment is with +=
, so ss += 1;
.
Rust doesn't provide a ++
operator for two primary reasons:
- The primary motivator for
++
is for C-style for(init;condition;increment)
loops, which aren't present at all in Rust. Instead, you use for pattern in iterator
, so there's no need to manually increment your iterator cursor.
- The ordering rules around
++
as an expression are nonobvious, even for experienced developers. On the other hand, +=
is a statement[1] with always clear ordering, and barely longer than ++
as a statement.
Language lawyer minutia and pedantry below.
[1]: Well, in actuality, +=
isn't a statement. place += value
is an expression returning ()
("unit", the type with only one value, the empty tuple). As far as I'm aware, there's only one actual statement in Rust: let
[2]. Even assignment is just a ()
-returning expression.
[2]: And there's experimental work that makes even that not true, allowing the use of let
as an expression (in some restricted contexts).