Calling an Option<Rc<dyn Fn(...)>>

We have:

o_func : Option<Rc<dyn Fn(..)>>

To use it, I end up doing:

if let Some(func) = &o_func {

This ends up turning something that should be one line into three lines, and two of the lines are noise. Is there a way to simplify this? (I'm dealing with GUI objects that can have 5-10 of these optional handlers, so this turns out to be quite repetitive.)

EDIT: changed o_func to &o_func

How about:

o_func.as_ref().map(|f| (f)(...));

Of course that this may introduce the issues of a closure if you're playing with complicated lifetime expressions.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.