Is there any way to return from a function from inside a closure?

后端 未结 1 1253
一整个雨季
一整个雨季 2020-12-20 12:25

I have the following simplified code:

fn f() -> i32 {
    let a = some_result.unwrap_or_else(|_| {
        return 1; // want to return this value from f &         


        
相关标签:
1条回答
  • 2020-12-20 13:12

    No, there is not.

    A closure is a method (a kind of function) under the hood. You are asking for the ability to exit a parent function from an arbitrarily deeply nested function call. Such non-local flow control has generally proven to be extremely bad for programmer sanity and program maintenance.


    To solve your problem:

    • How do you unwrap a Result on Ok or return from the function on Err?
    0 讨论(0)
提交回复
热议问题