我有Option<impl Fn(i64) -> i64>作为函数apply()的参数,我想传递None个值.apply(None).如何正确编写apply(None)函数调用才能进行编译?

fn main(){
    let a = apply(Some(|i| i + 1));
    
    let b = apply(None);
    let b2 = apply(None::<dyn Fn(i64) -> i64>);
    
    println!("a: {0}; b: {1}", a, b)
}

fn apply(f: Option<impl Fn(i64) -> i64>) -> i64 {
    let i = 0_i64;
    match f {
        Some(f) => f(i),
        None => i,
    }
}

编译错误:

Compiling playground v0.0.1 (/playground)
error[E0282]: type annotations needed
 --> src/main.rs:4:16
  |
4 |     let b = apply(None);
  |                   ^^^^ cannot infer type of the type parameter `T` declared on the enum `Option`
  |
help: consider specifying the generic argument
  |
4 |     let b = apply(None::<T>);
  |                       +++++

error[E0283]: type annotations needed
  --> src/main.rs:4:16
   |
4  |     let b = apply(None);
   |             ----- ^^^^ cannot infer type of the type parameter `T` declared on the enum `Option`
   |             |
   |             required by a bound introduced by this call
   |
   = note: multiple `impl`s satisfying `_: Fn<(i64,)>` found in the following crates: `alloc`, `core`:
           - impl<A, F> Fn<A> for &F
             where A: Tuple, F: Fn<A>, F: ?Sized;
           - impl<Args, F, A> Fn<Args> for Box<F, A>
             where Args: Tuple, F: Fn<Args>, A: Allocator, F: ?Sized;
note: required by a bound in `apply`
  --> src/main.rs:10:25
   |
10 | fn apply(f: Option<impl Fn(i64) -> i64>) -> i64 {
   |                         ^^^^^^^^^^^^^^ required by this bound in `apply`
help: consider specifying the generic argument
   |
4  |     let b = apply(None::<T>);
   |                       +++++

Some errors have detailed explanations: E0282, E0283.
For more information about an error, try `rustc --explain E0282`.

我试了let b2 = apply(None::<dyn Fn(i64) -> i64>);次,但还是不能编译,错误是

 Compiling playground v0.0.1 (/playground)
error[E0277]: the size for values of type `dyn Fn(i64) -> i64` cannot be known at compilation time
 --> src/main.rs:5:20
  |
5 |     let b2 = apply(None::<dyn Fn(i64) -> i64>);
  |                    ^^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
  |
  = help: the trait `Sized` is not implemented for `dyn Fn(i64) -> i64`
note: required by a bound in `None`
 --> /rustc/eb26296b556cef10fb713a38f3d16b9886080f26/library/core/src/option.rs:567:5

For more information about this error, try `rustc --explain E0277`.
error: could not compile `playground` (bin "playground") due to previous error

推荐答案

类型dyn Fn(i64) -> i64没有大小,这意味着您不能将其存储在像Option这样的枚举中.

但是特征对象引用是有大小的,所以您可以改用&dyn:

apply(None::<&dyn Fn(i64) -> i64>);

但是,使用函数指针类型可能更好:

apply(None::<fn(i64) -> i64>);

playground

Rust相关问答推荐

在‘await’点上使用‘std::同步::Mutex’是否总是会导致僵局?

无法从流中读取Redis请求

铁 rust 中双倍或更多换行符的更好练习?

程序在频道RX上挂起

了解Rust';s特征对象和不同函数签名中的生存期注释

在为第三方 struct 实现第三方特征时避免包装器的任何方法

Rust proc_macro 和 syn:解析空格

如何强制匹配的返回类型为()?

带引脚和不带引脚的比较功能

Nom 解析器无法消耗无效输入

我可以用 Rust 编写一个不可变变量

我可以在 Rust 中 serde struct camel_case 和 deserde PascalCase

Rust 中的 Option as_ref 和 as_deref 有什么不同

如何递归传递闭包作为参数?

使用自定义 struct 收集 Vec

哪些特征通过 `Deref` 而哪些不通过?

如何连接 Rust 中的相邻切片

如何在 Rust 中创建最后一个元素是可变长度数组的 struct ?

字符串切片的向量超出范围但原始字符串仍然存在,为什么判断器说有错误?

如何在 nom 中构建负前瞻解析器?