gpt4 book ai didi

rust - 如何将 'None' 值传递给 Rust 中的异步函数

转载 作者:行者123 更新时间:2023-12-03 11:27:01 25 4
gpt4 key购买 nike

<分区>

考虑以下代码

async fn main_call(&self) -> Result<()> 
{
let _list = self.sub_function(/*what to pass here?*/).await;
// do something with the result

Ok(())
}

async fn sub_function<P, F>(&self, f : Option<F>) -> Result<Vec<P>>
where
F : FnOnce(P) -> bool,
P : Clone
{

// do something, and return a value
// the `Vec::new()` is just here for demonstration purposes
Ok(Vec::new())
}

我应该将什么作为值传递给 sub_function ,如果我不想通过闭包?如果我通过 None ,编译器提示:

type inside `async fn` body must be known in this context E0698 cannot infer type for type parameter `T`
declared on the enum `Option` Note: the type is part of the `async fn` body because of this `await`

投诉很明显,但我不确定我应该通过什么。 None::<FnOnce(12)->bool)不起作用。

expected a `std::ops::FnOnce<(&_,)>` closure, found `dyn std::ops::FnOnce(u32) -> bool` 
E0277 expected an `FnOnce<(&_,)>` closure, found `dyn std::ops::FnOnce(u32) -> bool`
Help: the trait `std::ops::FnOnce<(&_,)>` is not implemented for `dyn std::ops::FnOnce(u32) -> bool`

同样,错误信息很明显。

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com