Passing Option to Spawn Function


I have an Option of a struct that I’d like to pass into a function inside a spawn closure. I know this Option will always be Some because immediately before calling spawn, I set the variable to Some. However, inside my function, I’m unable to call unwrap() because that consumes the Option and I get an error about “cannot move out of borrowed content”. Is there any way, besides the if let Some(_) ... syntax to get around this issue?

Here is a playground that works, with the unwrap() commented out that will generate the error:


You want as_ref():

let c = arg.as_ref().unwrap().num;

unwrap() consumes the Option, and therefore takes it by value (self). So you want to get an owned Option by calling as_ref on the borrowed one, and then unwrap from there.