我正試圖找到一個好方法將Option<String>
轉換爲Option<i8>
。如何將選項<結果<T, Error>>轉換爲選項<T>而不打開它?
例如,
use std::str::FromStr;
fn main() {
let some_option: Option<String> = Some("too".to_owned());
let new_option: Option<i8> = some_option.map(|x| i8::from_str(x.as_str()));
}
我想我可以使用渦輪增壓魚顯式轉換類型,這樣是這樣的:
use std::str::FromStr;
fn main() {
let some_option: Option<String> = Some("too".to_owned());
let new_option: Option<i8> = some_option.map::<Option<i8>>(|x| i8::from_str(x.as_str()));
}
然而,編譯器指出,這是不正確數量的參數,所以我認爲這可能工作,但它不:
use std::str::FromStr;
fn main() {
let some_option: Option<String> = Some("too".to_owned());
let new_option: Option<i8> = some_option.map::<Option<i8>,i8::from_str>();
}