有没有一种方法可以从 Option
中“提取”数据?我有一个 API 调用返回 Some(HashMap)
。我想使用 HashMap
,就好像它不在 Some
里面,并操作这些数据。
根据我所读的,看起来 Some(...)
只适用于匹配比较和一些内置函数。
从板条箱文档中提取的简单 API 调用:
use std::collections::HashMap;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let resp = reqwest::blocking::get("https://httpbin.org/ip")?
.json::<HashMap<String, String>>()?;
println!("{:#?}", resp.get("origin"));
Ok(())
}
结果:
Some("75.69.138.107")