如何从期货0.1组合器方法链中早日返回?

时间:2019-12-13 14:12:33

标签: rust future rust-tokio

是否有办法从未来的合并者链中尽早返回?例如,如果某些条件成立,我想返回成功,否则继续沿链前进。

我想做类似的事情:

fn early_exit() -> impl Future<Item = u32, Error = String> {
    ok::<u32, String>(42)
        .and_then(some_function)
        .and_then(another_function)
        .and_then(|x| {
            // return early if condition is satisfied
            if some_condition(x) {
                return ok::<u32, String>(42);
            }
            // Else pass it down the combinator chain for further transformations
            else {
                Ok(x)
            }
        })
        .and_then(one_more_function)
        .and_then(last_function)
}

我认为使用async / await可以做到这一点,但是用期货0.1可以做到这一点吗?

0 个答案:

没有答案