Simplify this code

pub struct Foo<T> {
  x: Option<T>

data: HashMap<K, Foo<T>>;

if let Some(entry) = data.get_mut(k) {
  if let Some(b) = & mut entry.x {

Is there a way to get a Option<&mut T> in a less clunky way ?

You can match the field within the first pattern to get &mut Option<T>:

    if let Some(Foo { x }) = data.get_mut(k) {

And you can also combine both of your patterns altogether:

    if let Some(Foo { x: Some(b) }) = data.get_mut(k) {


I did not state this well: I need to pass this as an argument to a function expecting an Option<&mut T>. Is there a way to do this without multi exprs ?

EDIT: possibly with .and_then

You can't get that directly in the pattern, but &mut Option<T> to Option<&mut T> is just .as_mut().