Is there a way to combine multiple traits in order to define a new trait? [duplicate]
Asked Answered
M

1

61

Is there a way to combine multiple traits (by inheritance?) in order to define a new trait? I'm looking for something like concepts in C++:

auto concept newConcept<typename T> : concept1<T>, concept2<T>, concept3<T> {};

Suppose I want to make a new trait that inherits from Clone, Default and some other traits, is that possible?

Mach answered 17/11, 2014 at 22:41 Comment(1)
I was searching for a question like this under the term "super traits" so I just comment it here and hopefully someone may find it with this term later.Weston
E
96

Yep!

trait NewTrait: Clone + Default + OtherTraits {}
impl<T> NewTrait for T where T: Clone + Default + OtherTraits {}
Ekaterinburg answered 17/11, 2014 at 22:45 Comment(4)
Thanks (tried this with commas... but it didn't work). I searched for this for a long time, too bad it is not in the guide.Mach
It is mentioned in the reference, though: doc.rust-lang.org/reference.html#traits Yeah, I tripped up on this as well :)Viridissa
You can write a macro trait_alias as described in #30292084Thelen
This is called a blanket implementation users.rust-lang.org/t/what-are-blanket-implementations/49904Solidary

© 2022 - 2024 — McMap. All rights reserved.