Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between "generic parameters of trait function" and "generic parameters of trait"?

Tags:

generics

rust

A simple example for generic parameters of trait function:

trait Ext: Sized {
    fn then<R>(self, f: fn(Self) -> R) -> R {
        f(self)
    }
}

impl<T> Ext for T {}

A simple example for generic parameters of trait:

trait Ext<R>: Sized {
    fn then(self, f: fn(Self) -> R) -> R {
        f(self)
    }
}

impl<T, R> Ext<R> for T {}

What's the difference between the two?

When should I use "generic parameters of trait function" and when should I use "generic parameters of trait"?

like image 802
hzqelf Avatar asked Mar 23 '21 18:03

hzqelf


People also ask

How do you define a generic function?

Generic functions are functions declared with one or more generic type parameters. They may be methods in a class or struct , or standalone functions. A single generic declaration implicitly declares a family of functions that differ only in the substitution of a different actual type for the generic type parameter.

What are generic arguments?

Generic Argument Clause The generic argument list is a comma-separated list of type arguments. A type argument is the name of an actual concrete type that replaces a corresponding type parameter in the generic parameter clause of a generic type. The result is a specialized version of that generic type.

What is the difference between trait and character in genetics?

What is the Difference Between Trait and Character in Genetics. The main difference between trait and character is that a trait is a state of a character, which is a distinct variation of a phenotypic characteristic of an organism whereas a character is a recognizable feature, which helps in the identification of the organism.

What does a trait mean?

A trait is a state of a character, which is a distinguishing feature of a particular group of organisms. A trait is inherited from parents and is expressed under the influence of the environment. The main difference between trait and character is their correspondence. 1. “Trait (Biology).” ScienceDaily, ScienceDaily, Available Here 2. “Character.”

What is the difference between actual parameter and formal parameter?

Summary – Actual vs Formal Parameters. The difference between Actual Parameters and Formal Parameters is that Actual Parameters are the values that are passed to the function when it is invoked while Formal Parameters are the variables defined by the function that receives values when the function is called.

What is the difference between model and parameter?

They are often saved as part of the learned model. Parameters are key to machine learning algorithms. They are the part of the model that is learned from historical training data. In classical machine learning literature, we may think of the model as the hypothesis and the parameters as the tailoring of the hypothesis to a specific set of data.


1 Answers

If you use a generic parameter on a trait, then the same value will have to apply to all functions in the trait implementation, rather only apply to each function, but you'll also have to specify the parameter whenever referencing the trait.

One advantage of this is that you can implement a trait for a specific value of that trait parameter, where each implementation is independent:

impl Ext<String> for Something { }
impl Ext<u32> for Something { }

It's also possible to restrict types based on a trait parameter. For example, for the Mul trait:

pub trait Mul<Rhs = Self> {
    type Output;
    pub fn mul(self, rhs: Rhs) -> Self::Output;
}

The implementation for a specific type will determine the type of Self, and it must specify Self::Output (which doesn't have to be the same as type). The trait parameter here is ensuring that the type of rhs is the same as Self

like image 80
transistor Avatar answered Oct 23 '22 01:10

transistor