我已經寫了一個函數來映射向量。該方法定義預計3個泛型參數,並定義了一個輸入向量和函數:期望的類型參數,找到&預期A
pub fn map<F, A, B>(mapFn: F, vect: &[A]) -> &[B]
where
F: Fn(A) -> B,
{
let mut list = vec![];
for v in vect {
let mut value = mapFn(v);
list.push(value);
}
&list[..]
}
我得到這個錯誤:
error[E0308]: mismatched types
--> src/main.rs:8:31
|
8 | let mut value = mapFn(v); // <------ This is where I get the error
| ^expected type parameter, found &A
|
= note: expected type `A`
found type `&A`
我也查Generics Error: expected type parameter, found struct,但它似乎沒有大致相同問題。
我看不到你的功能的興趣。這種方法已經存在。 – Boiethios