How to explicitly specify lifetime bound for closure created inside closure

I have a noob question :stuck_out_tongue: :sheep: A lifetime bound of 'static is being elided on a generic closure somewhere. How would I specify the lifetime can be shorter? I only need the closure arg(s) to outlive the local context variable in run_with_context

struct Context<'i> {
    functions: Vec<Box<dyn FnOnce(&Context) + 'i>>

impl<'i> Context<'i> {
    fn new() -> Self {
        Self{functions: vec![]}
    fn push_func<F: FnOnce(&Context) + 'i>(&mut self, f: F) {

fn run_with_context<F: FnOnce(&mut Context)>(f: F) {
    let mut context = Context::new();
    f(&mut context);
    //the context would normally do something here, but just drop it for simplicity

fn main() {
    let my_string = "hello".to_string();
    run_with_context(|context| context.push_func(|context| println!("{}", &my_string)));

Any pointers are appreciated.

-fn run_with_context<F: FnOnce(&mut Context)>(f: F) {
+fn run_with_context<'cap, F: FnOnce(&mut Context<'cap>)>(f: F) {

Rust Playground


Thank you! :person_facepalming: I'm not thinking clearly today.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.