将闭包传递给需要 std::ops::Fn 的函数



我正在尝试使用闭包来调用一堆特征向量的函数。 这里最重要的代码行是第一行,我们在其中说query必须是什么类型,最后一行,我们试图给出一个闭包。

fn stat_query(dataset: &Vec<Vec<i32>>, query: Fn(&Vec<i32>) -> f64) -> f64 {
    // takes in a dataset of feature vectors and a query,
    // returns the average of the query on the dataset
    if dataset.len() == 0 {
        0.
    } else {
        dataset.iter().map(|ref fv| query(&fv)).sum() / (dataset.len() as f64)
    }
}
fn main() {
    // build dataset
    let fv1: Vec<i32> = vec![1, 1, 1, 1, 1];
    let fv2: Vec<i32> = vec![1, 0, 1, 0, 1];
    let my_dataset = vec![fv1, fv2];
    // query checks whether sum of features is greater than a threshold
    fn my_query(ref fv: &Vec<i32>, threshold: i32) -> f64 {
        if fv.iter().sum() > threshold {
            1.
        } else {
            0.
        }
    }
    // run query on dataset with threshold 3
    println!("{}", stat_query(&my_dataset, |ref fv| my_query(fv, 3)));
}

当我运行这个时,我收到一个错误:

error[E0277]: the trait bound `for<'r> std::ops::Fn(&'r std::vec::Vec<i32>) -> f64 + 'static: std::marker::Sized` is not satisfied
 --> src/main.rs:1:40
  |
1 | fn stat_query(dataset: &Vec<Vec<i32>>, query: Fn(&Vec<i32>) -> f64) -> f64 {
  |                                        ^^^^^ `for<'r> std::ops::Fn(&'r std::vec::Vec<i32>) -> f64 + 'static` does not have a constant size known at compile-time
  |
  = help: the trait `std::marker::Sized` is not implemented for `for<'r> std::ops::Fn(&'r std::vec::Vec<i32>) -> f64 + 'static`
  = note: all local variables must have a statically known size
error[E0308]: mismatched types
  --> src/main.rs:27:44
   |
27 |     println!("{}", stat_query(&my_dataset, |ref fv| my_query(fv, 3)));
   |                                            ^^^^^^^^^^^^^^^^^^^^^^^^ expected trait std::ops::Fn, found closure
   |
   = note: expected type `for<'r> std::ops::Fn(&'r std::vec::Vec<i32>) -> f64 + 'static`
              found type `[closure@src/main.rs:27:44: 27:68]`

我认为关闭实施Fn(或FnOnceFnMut(? 当您期望Fn时提供关闭是否无效?我想我需要在第一行中指定有关Fn的其他内容? 但是什么?

假设我错误地指定了第一个函数的 query: Fn(&Vec<i32>) -> f64 参数的类型,或者写错了闭包|ref fv| my_query(fv, 3)

我阅读了将闭包传递给 trait 方法:预期的类型参数,找到闭包,但这似乎更多的是关于传递闭包,这不是函数接受的唯一一种东西。

第二个错误是更重要的错误:

error[E0277]: the trait bound `for<'r> std::ops::Fn(&'r std::vec::Vec<i32>) -> f64 + 'static: std::marker::Sized` is not satisfied

简而言之:Fn(&Vec<i32>) -> f64 不是大小类型,不能按值传递。 只能传递特征对象(引用或框(或实现特征的(大小(类型的值。 在这种情况下,您可能需要后者:

操场

fn stat_query<F>(dataset: &Vec<Vec<i32>>, query: F) -> f64
where
    F: Fn(&Vec<i32>) -> f64,
{
    // takes in a dataset of feature vectors and a query,
    // returns the average of the query on the dataset
    if dataset.len() == 0 {
        0.
    } else {
        dataset.iter().map(|ref fv| query(&fv)).sum::<f64>() / (dataset.len() as f64)
    }
}
fn main() {
    // build dataset
    let fv1: Vec<i32> = vec![1, 1, 1, 1, 1];
    let fv2: Vec<i32> = vec![1, 0, 1, 0, 1];
    let my_dataset = vec![fv1, fv2];
    // query checks whether sum of features is greater than a threshold
    fn my_query(ref fv: &Vec<i32>, threshold: i32) -> f64 {
        if fv.iter().sum::<i32>() > threshold {
            1.
        } else {
            0.
        }
    }
    // run query on dataset with threshold 3
    println!("{}", stat_query(&my_dataset, |ref fv| my_query(fv, 3)));
}

最新更新