Summary
Add an n++ operator, but not a ++n operator, to Rust.
Motivation
Rust already has n+=1, which is the ++n operator. This is increment first, then return the incremented value.
Increment and then return the un-incremented value is a more verbose affair requiring the allocation of a temporary variable.
In Rust, allocating something like an index from a free list can be done as such:
fn allocate(&mut self) -> u16 {
if let Some(id) = self.free_list.pop() {
id
} else {
let id = self.counter;
self.counter += 1;
id
}
}
This could be more concise:
fn allocate(&mut self) -> u16 {
if let Some(id) = self.free_list.pop() {
id
} else {
self.counter++
}
}
Explanation
Because Rust provides n+=1 already, we don't need ++n. Besides redundancy, ++n and n++ look similar; if the goal is code readability by removing unnecessary verbosity, then implementing only n++ gives two clearly-different statements, avoiding the need to remember the semantic difference for whether the ++ is before or after the variable.
In other words, ++n is unnecessary and so implementing only n++ gives the unary ++ operator exactly one meaning, instead of two possible meanings. This meaning is also slightly more immediately recognizable than three lines of syntax with two different variables interspersed.
Do note foo = a + (n+=1) is not legal and there's no particular reason foo = a + n++ should be legal either. Such syntax can be problematic, especially in weird uses like foo = a + n++ + b*(n++) - (n++)/(n++). It's probably best to just treat n++ as an assignment (i.e. ++ is an assignment operator like +=).
There are other syntaxes that could express the same, such as n=+1 and n=-1, but -1 has its own meaning and typographical errors are much more likely than with ++ and --. Another possibility is to allow syntax like n++2 which would add 2 to n, essentially making ++ equivalent to += but returning the pre-assignment value; whether to allow the unary n++ to imply n++1 is a judgment call in that case.