How can I pull data out of an Option for independent use?

if let Some(origin) = resp.get("origin") {
    // use origin
}

If you can guarantee that it’s impossible for the value to be None, then you can use:

let origin = resp.get("origin").unwrap();

Or:

let origin = resp.get("origin").expect("This shouldn't be possible!");

And, since your function returns a Result:

let origin = resp.get("origin").ok_or("This shouldn't be possible!")?;

Or with a custom error type:

let origin = resp.get("origin").ok_or(MyError::DoesntExist)?;

Leave a Comment