#[derive(Debug)]
struct Foo {
name: String,
val: String,
}
use std::collections::HashMap;
struct Bar {
pub map: Option<HashMap<String, Vec<Foo>>>,
}
fn fun(bar: Bar, key: &str) {
let vec = Vec::new();
let vec = if let Some(map) = bar.map {
if let Some(vec) = map.get(key) {
vec
} else {
&vec
}
} else {
&vec
};
}
in fun()
I'm unwrapping bar
first but it is taking around 10 lines (and this number of lines will increase if argument bar
is option
). is there any easy way doing this which will occupy lesser line of code?