Common pattern for passing to closure?

I've recently done something like the following a couple times... wondering if it's a common pattern or there's another approach?

//create 2 copies of foo because one gets owned by the closure
//and the other is owned outside the closure
//also needs interior mutability
let foo1 = Rc::new(RefCell::new(Foo{...}));
let foo2 = Rc::clone(&foo1);

//move is required for reasons (e.g. interfacing with JS where it'll be `static)
do_something(move || { 
    //do something with foo1

//this could happen at an artbitrary time
*foo2.borrow_mut() = Foo{...}

There's a clever pattern to avoid numbering variables:

let foo = Arc::new(Foo{});

let foo = foo.clone();
move || { 
    //do something with foo
1 Like

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