che*_*eak 4 rust async-await option-type rust-futures
有没有更惯用或更漂亮的方法来在 Rust 中进行这样的操作?
let maybe_output = match maybe_input {
Some(input) => Some(async_result(input).await?),
None => None,
};
Run Code Online (Sandbox Code Playgroud)
我尝试map这样使用,
let maybe_output = maybe_input.map(|input| async_result(input).await?);
Run Code Online (Sandbox Code Playgroud)
但我不能在不返回 future 或结果的 lambda 中使用.awaitand运算符。?我怀疑我可以绘制地图并得到一个Option<Future<Result<_>>>,然后将其Future<Result<_>>排序Future<Result<Option<_>>>;
let maybe_output = maybe_input.map(|input|
async_result(input)
).transpose().await?;
Run Code Online (Sandbox Code Playgroud)
但我不知道是否可以调用transposeFuture,因为它是一个特征,而不是像 Result 或 Option 这样的类型。
OptionFuture箱子里有futures:
let maybe_output = OptionFuture::from(maybe_input.map(async_result)).await;
Run Code Online (Sandbox Code Playgroud)
然而,我个人认为原始形式更惯用。我可以立即理解它的match作用,但我必须再看一眼才能知道它的作用OptionFuture符合我的预期。
| 归档时间: |
|
| 查看次数: |
1629 次 |
| 最近记录: |