将闭包传递给期望std :: ops :: Fn的函数

时间:2017-11-14 21:55:46

标签: rust closures traits

我正在尝试使用闭包来调用一堆特征向量上的函数。这里最重要的代码行是第一行,我们说的是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]`

我认为闭包implemented Fn(或FnOnceFnMut)?如果期望Fn,提供闭包是否无效?我想我需要在第一行中指定有关Fn的其他内容吗?但是什么?

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

我读过Passing closure to trait method: expected type parameter, found closure,但这似乎更多的是关于传递一个闭包,这不是函数接受的唯一一种东西。

1 个答案:

答案 0 :(得分:3)

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

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))); } 不是大小类型,不能按值传递。您只能传递trait objects(引用或框)或实现特征的(大小)类型的值。在这种情况下,你可能想要后者:

Playground

$(this).length
相关问题