amelius 6 days ago

I noticed that for most of the examples the Rust version is more verbose.

2
pornel 6 days ago

C++ syntax and semantics are optimized for C++ idioms, and Rust's aren't.

Rust is more related to ML-family languages than C-family and OOP, so it needs to "emulate" some C++ idioms.

This goes both ways, e.g. equivalent of Rust's pattern matching on enums with data translates to verbose and clunky C++.

amelius 6 days ago

So you are saying that this approach teaches C++ users to use the wrong idioms in Rust?

pornel 6 days ago

Wrong is too strong. The code is okay given the constraint — this is a guide for C++ programmers thinking in C++ terms, not for teaching purely idiomatic Rust from the ground up.

mrlongroots 6 days ago

Which, as a cpp programmer trying to pick up Rust, is honestly fine to begin with. Once you've written varying amounts of code in 5-10 programming languages, it is incredibly tedious to flip through pages trying to teach you how if conditions work and how for loops work: my brain doesn't pay attention even if I try.

This is more like: how to survive rustc as a cpp programmer which is honestly your mindframe when you start out, and it sets you up for "okay now that you speak the syntax, this is how to really think in rust terms".

tialaramex 5 days ago

One habit worth trying to adopt early in this mode is running clippy, Rust's linter. Typically invoked as `cargo clippy`

Clippy likes idiomatic Rust and will suggest you change code that's not idiomatic into code which is, even when the machine code would be completely identical - the rationale being that the maintainer (later you with more Rust knowledge, a colleague, or even some stranger) is more likely to follow the idiomatic Rust and the whole point of source code is that it's for humans not machines.

Clippy is no substitute for a capable human reviewer, it has no sense of taste or style, no higher level understanding of the problem, but it's free and it's right there and unlike a human reviewer you won't feel judged which can be sensitive when you're learning a new language and are used to having mastery.

amelius 6 days ago

Ok, do you have any examples to convince me?

modulus1 6 days ago

And they even made the C++ version more verbose than it should have been. Most people would write:

class Person { int age = 0; };

I wish rust would make default struct field values this easy to write.

steveklabnik 5 days ago

It’s coming soonish! There’s an accepted RFC that’s getting close to being ready to stabilize https://github.com/rust-lang/rust/issues/132162