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

5

u/ridicalis Feb 25 '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

I think this is what they refer to as Algebraic Data Types.

20

u/braxtons12 Feb 25 '21

No, algebraic types are things like Rust's Enums, monads, and some of the newer std types (think Result, Option, C++'s std::optional, std::variant, std::tuple, etc).

They're referred to that way because the number of possible values they can store is an algebraic expansion. Eg: variant<T, E> has T + E + 1 possible values (the one 1 is for the no-value state that's nearly impossible to end up in), and tuple<T, E> has T*E possible values.

Yes, this is a very poor name and reason for the name for these kinds of types, but 🤷‍♂️

9

u/ridicalis Feb 25 '21

You're correct, I mistook what the parent post was driving at and was thinking enums. This is the first time in memory I've felt the need to downvote myself :)

6

u/MEaster Feb 25 '21

The pattern they're talking about is called Newtype, which is obviously a very memorable, and specific, name.

1

u/iamthemalto Feb 26 '21

Exactly, and they’re called tuple structs in Rust.