I wanted to make a generic struct that has a field which depends on the type parameters.
In C++ templates I would use type traits and compile time type transformations for this.
Is there anything similar in Rust?
I couldn't find anything on the subject, not even a proposal.
Maybe there is a different terminology in the Rust community
What do you mean by this? Do you mean that it has a type specified by a generic type parameter, like this?
struct Generic<T> {
inner: T,
}
As for type 'transformations', you could use associated types in traits:
trait Transformer {
type Output;
}
impl Transformer for i32 {
type Output = String;
}
impl Transformer for char {
type Output = ();
}
type T1 = <i32 as Transformer>::Output; // T1 is String
type T2 = <char as Transformer>::Output; // T2 is unit, ()
More generally: In C++ "type traits" has always meant templates that are useful only for their associated types and/or associated consts. For lots of reasons the "associated type/const" terminology never caught on over there but is nearly universal in Rust, and exactly what these are used for is also different. For example, Rust will probably never provide something like std::is_integral as an associated boolean const because in this language it makes way more sense to provide that functionality through traits. In fact, that's probably a big reason why C++ calls these "type traits".