Rust and C++ function definitions

A small study in syntax and legibility.

June 03, 2016 (updated June 07, 2016)Filed under tech#cplusplus#programming languages#rustMarkdown source

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!