Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

rust

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?

like image 737
BigEpsilon Avatar asked Nov 17 '14 22:11

BigEpsilon


People also ask

Can a trait extend multiple traits?

Comparison with Abstract Classes Both traits and abstract classes provide mechanisms for reusability. However, there are a couple of fundamental differences between the two: We can extend from multiple traits, but only one abstract class. Abstract classes can have constructor parameters, whereas traits cannot.

Can we extend multiple traits in Scala?

A Scala class can extend multiple traits at once, but JVM classes can extend only one parent class. The Scala compiler solves this by creating "copies of each trait to form a tall, single-column hierarchy of the class and traits", a process known as linearization.

What are rust traits?

A trait in Rust is a group of methods that are defined for a particular type. Traits are an abstract definition of shared behavior amongst different types. So, in a way, traits are to Rust what interfaces are to Java or abstract classes are to C++. A trait method is able to access other methods within that trait.


1 Answers

Yep!

trait NewTrait: Clone + Default + OtherTraits {} impl<T> NewTrait for T where T: Clone + Default + OtherTraits {} 
like image 185
sfackler Avatar answered Sep 24 '22 20:09

sfackler