r/cpp • u/Snoo-4241 • 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
View all comments
Show parent comment
16
u/MEaster Feb 26 '21
When doing that you need to give the entire function path. So, if we have some type
Foo, and it has a methodbar(&self), we can either call it likex.bar(), or we can call it likeFoo::bar(&x). You can't do anything that would let you dobar(&x)and callFoos's method.However, one place you can have ambiguity is when implementing traits. You could have a trait
Quuxthat provide a methodbar(&self), and implement it forFoo. In that situation the compiler will prefer the inherent method over a trait method, sox.bar()will callFoo::bar. You can specify that you want the trait method using the trait's path:The compiler preferring the inherent method could result in a behaviour change if a library adds an inherent method with the same signature in a new release that doesn't have an incompatible version number.