Rust and C++ function definitions
A small study in syntax and legibility.
I just put my finger on one of the (many) reasons Rust reads better than C++: the visual consistency of its function definitions. Compareâ
Rust has:
fn foo() -> i32 { /* implementation */ }
fn bar() -> f32 { /* implementation */ }
C++ has:
int foo() { /* implementation */ }
double bar() { /* implementation */ }
That consistency adds up over many lines of code. There are many other such choices; the net effect is that Rust is much more pleasant to read than C++.
Note: Iâm aware that C++11 added the auto foo() -> <type>
syntax. But this actually worsens the problem. A totally new codebase which uses that form exclusively (which may not always be possible, because the semantics arenât the same) would have roughly the same visual consistency as Rust in that particular category. (Plenty of others would still be a mess.) But the vast majority of C++ codebases are not totally new. Adding the form means your codebase is more likely to look this this:
int foo() { /* implementation */ }
auto quux() -> uint32_t { /* implementation */ }
double bar() { /* implementation */ }
That is, for the record, more visual inconsistencyânot less!