Prana
Prana

Reputation: 703

Is it possible to have a closure in a structure with generic arguments and return type?

Is it possible to replace the closure below with generic arguments and return type?

struct HMHolder<T>
where
    T: Fn(u32) -> u32,
{
    calculation: T,
    value: HashMap<String, i32>,
}

For example, can I make the Fn<U, V>(x: U) -> V in that struct and create a constructor?

Upvotes: 0

Views: 91

Answers (1)

phimuemue
phimuemue

Reputation: 35973

Maybe like this (specifying a phantom type for the otherwised unused type parameters):

struct HMHolder<I, R, Calculation>
where
    Calculation: Fn(I) -> R,
{
    calculation: Calculation,
    value: std::collections::HashMap<String, i32>,
    marker: std::marker::PhantomData<(I, R)>,
}

Upvotes: 3

Related Questions