fn example2<'a, F>(f: F) -> Example<'a>
where
F: Fn() + 'a,
{
Example { parse: Some(Box::new(f)) }
}
What's happening here? It's that Some(Box::new(f)) makes Option<Box<F>> not Option<Box<dyn Fn()>>. You can coerceBox<F> into Box<dyn Fn()> but coercing Option<Box<F>> into Option<Box<dyn Fn()>> is not supported.
In both type-specified local variable and directly assigning struct field cases, the desired type is known ahead of the evaluation of expression Some(Box::new(f)). Since it should returns Option<Box<dyn Fn()>> the Some knows it should receive Box<dyn Fn()>, it coerces Box<F> received into that type.