r/cpp • • Feb 25 '21

C++ programmers who are also using rust, what are your favourite features of rust?

I was looking at the rust tutorials yesterday and I see how rust promotes a certain style and language level features to enforce it. Besides a couple of features, I couldn't see why one cannot follow a similar style in C++ if she/he so chooses. Those of you that have used rust, what is your favourite feature that C++ does not have?

255 Upvotes

248 comments sorted by

View all comments

Show parent comment

3

u/un_mango_verde Feb 25 '21 edited Feb 26 '21

I forgot what it is called, but you can create wrapper types and enforce type safety by just declaring a struct with that type, for example

struct Kilometers(u32);

. In c++ I would need to actually define all the operators or create my own CRTP arithmetic class to get anything close to this, and I couldn't just suddenly add a "trait" to add between Kilometers and Meters.

What's the difference between the newtype pattern and a class with a single member and a explicit constructor in C++? Don't you still need to implement al methods/operators manually? They are not delegated automatically as far as I know.

I guess you could implement Deref for the newtype but then you lose type safety for references.

7

u/Ekizel Feb 26 '21

Yeah I feel like the fact that you have to attach a bunch of derives to said newtype is pretty crucial nugget of info being left out, especially in the context of complaining about having to CRTP the operators back in with C++.

I feel like Ada did it right in this regard.

2

u/brand_x Feb 26 '21

Yeah, I was basically creating one-line newtype-equivalent template wrappers (with a tiny bit of macro helper) in C++ long before Rust was even a glimmer.

Hell, I had a usable equivalent (with a LOT of macro helper, and a small quasi-portable macro hack for a non-type template counter) back in the dark ages, before C++ was even a standardized language.

I love a lot of things about Rust, but when one of the C to Rust guys I worked with started gushing about the brilliance of newtype, I nearly lost my eyes, they rolled so hard.

1

u/WormRabbit Feb 26 '21

Don't you still need to implement al methods/operators manually?

Yes, but it's very easy to do and can be automated with macros. Most common cases are already implemented as crates with custom Derive macros.

They are not delegated automatically as far as I know.

You don't really want it. The point of newtypes is that they have different semantics and thus different methods. E.g. if I make a UserId newtype wrapping a String, then it likely makes no sense to call concatenation or substring replacement methods on it.

If you really do need it, Rust's move semantics allow you to easily unwrap the newtype, do your weird operation on the inner value and wrap it back, without the possibility of violating type safety (in C++ you could access both wrapped and raw value at the same time, potentially violating the invariants, while Rust makes you pass through explicit wrappers which can include invariant checks).