Turn FnOnce into an FnMut


#1

Can I turn an FnOnce into an FnMut if, on the first call, I run the FnOnce, and on subsequent calls, I take another path?

Sorry if this is an obvious question, I’m sleepy.


#2

You can embed an FnOnce in a FnMut

    struct S(i32);
    let s = S(1);
    let o = move || {
        let r = s.0;
        drop(s);
        r
    };
    let mut o = Some(o);
    let mut m = move || match o.take() {
        Some(o) => o(),
        None => 0,
    };

#3

Thanks, wrapping the FnOnce in an Option is a really elegant (and in hindsight, obvious) idea.