Sorry for the bike-shed, but I think using a closure syntax when we don't use closure semantics (because we don't want to move/borrow the values the defer
is supposed to clean up) can be confusing. If you want the ability to conditionally pass the result to the defer
, there are two syntax styles for that that are already used in other Rust constructs (and are therefore more teachable):
- Always pass the result, and just ignore it with
_
when you don't actually need it:// Without result: defer _ { some_queue.push(item); } // With result: defer result { result.and_then(|| take_action()); }
dever let
// Without result: defer { some_queue.push(item); } // With result: defer let result { result.and_then(|| take_action()); }
Also, with both styles (though it may be more natural for defer let
) we can support refutable patterns:
// First style
defer Ok(result) { take_action(); }
// Second style
defer let Ok(result) { take_action(); }