dzbjet
dzbjet

Reputation: 11

how to create a vector that contains the same generic function with different generic type?

how to create a vector that contains the same generic function with different generic type?

I wish the vector v contains function "decode<T:IE>(ie:&dyn IE)" with MyIEa, MyIEb, both implementing the IE trait.


pub fn test_ex7() {

    trait IE {}
    
    struct MyIEa {}
    impl IE for MyIEa {}
    
    struct MyIEb {}
    impl IE for MyIEb {}
    
    
    fn decode<T:IE>(ie: &dyn IE) -> T{
    
    }
    
    let v = vec![
        decode::<MyIEa>,
        decode::<MyIEb>,
    ];

}

mismatched types expected struct Box<[for<'r> fn(&'r (dyn IE + 'r)) {decode::<MyIEa>}], _> found struct Box<[for<'r> fn(&'r (dyn IE + 'r)); 2], std::alloc::Global>

Upvotes: 1

Views: 44

Answers (1)

Aleksander Krauze
Aleksander Krauze

Reputation: 6195

Add explicit type annotation:

let v: Vec<fn(&dyn IE)> = vec![
        decode::<MyIEa>,
        decode::<MyIEb>,
    ];

Upvotes: 1

Related Questions