Current location - Quotes Website - Personality signature - Advanced characteristics of rust _ basic shell
Advanced characteristics of rust _ basic shell
Associating types is a way to associate type placeholders with features, so these placeholder types can be used in method signatures of features:

The implementer of trait will specify the corresponding concrete type in the position of this type for concrete implementation:

You can only select the item type once, because the counter can only have one impl iterator. You don't have to specify an iterator that we need u32 value every time you call the next counter.

When using generic type parameters, you can specify the default concrete type for generics. If the default type is sufficient, this eliminates the need to implement trait for specific types. When declaring a generic type, use the syntax that specifies the default type for the generic type. PlaceholderType = concrete type & gt; :

The syntax of & ltrhs = Self & gt is called default type parameter, and RHS is a generic type parameter (abbreviation of "right hand side"), which is used to define the RHS parameter in the add method. If the specific type of RHS is not specified when adding the feature, the type of RHS will be the default self type.

There are no following parameters:

Belt parameters are as follows:

The default parameter types are mainly used in the following two aspects:

How to make one trait have the same name as another;

This method can be distinguished as follows:

But the correlation function is a part of the trait, but there is no self parameter. When two types in the same scope realize the same characteristics, Rust cannot calculate the expected type:

You will get an error when calling Animal::baby_name (). Animal::baby_name is an associative function rather than a method, so it is impossible to calculate which Animal::baby_name needs to be implemented without the self parameter. At this point, fully qualified syntax is required:

Generally, fully qualified grammar is defined as:

This verbose syntax is only needed when there are multiple implementations with the same name and Rust needs help to know which implementation to call.

If you want to work in VEC,

One way to get around this limitation is to use the newtype pattern: you can create a new pattern that contains VEC.

The disadvantage of this method is that because the wrapper is a new type, it has no method defined above its value and VEC.