# Storing a function taking a function in a struct

**URL:** <https://users.rust-lang.org/t/storing-a-function-taking-a-function-in-a-struct/14434>\
**Category:** help\
**Created:** [December 15, 2017, 7:26am UTC](https://users.rust-lang.org/t/storing-a-function-taking-a-function-in-a-struct/14434 "2017-12-15T07:26:05Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alex\_PK](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/alex_pk/32/5438_2.png) [@Alex\_PK](https://users.rust-lang.org/u/Alex_PK)\
**Post date:** [December 15, 2017, 7:26am UTC](https://users.rust-lang.org/t/storing-a-function-taking-a-function-in-a-struct/14434/1 "2017-12-15T07:26:05Z")

</div>

Hi all,

I would like to store a function (or a closure) in a struct, and this function should take a method implemented on the struct as parameter and use it to store (or update) a generic type defined in the struct. Here is what I want to do:

```rust
struct Handler {
  v: Option<T>,
  f: Fn(Fn(T) -> ()),
}

impl Handler {
  fn new(f: Fn(T) -> ())-> Handler { 
    Handler { v: None; f } 
  }

  fn run(&mut self) -> () {
    (self.f)(self.my_cb);
  }

  fn my_cb(&mut self, v: T) {
    self.v = Some(v);
  }
}

fn main() {
  let mut h = Handler::new(|cb| { cb(5); });
  h.run();
}

```

I removed all type constraints to make the code clear, but I tried a lot of combinations.

I know it's pretty convoluted 🙂 but I keep running into lifetime issues, or "Not Sized" errors, or "Cannot be applied to Trait objects", etc. I lost count of the experiments I did... 😕

Thank you for any help, also if it is "it can't be done". 🙂

---

<div class="post-metadata">

**Author:** ![matklad](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/matklad/32/41370_2.png) [@matklad](https://users.rust-lang.org/u/matklad)\
**Post date:** [December 15, 2017, 8:15am UTC](https://users.rust-lang.org/t/storing-a-function-taking-a-function-in-a-struct/14434/2 "2017-12-15T08:15:33Z")

</div>

> [@Alex\_PK](#):
>
> f: Fn(Fn(T) -\> ()),

One way would be to use `Box<Fn(&Fn(T) -> ())>`. You can't store a bare `Fn` (because that's a trait, not a type), so you either have to use generics like `F: Fn() -> ()` or hide Fn behind a pointer (Box or &) and use trait objects.

---

<div class="post-metadata">

**Author:** ![Alex\_PK](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/alex_pk/32/5438_2.png) [@Alex\_PK](https://users.rust-lang.org/u/Alex_PK)\
**Post date:** [December 15, 2017, 2:09pm UTC](https://users.rust-lang.org/t/storing-a-function-taking-a-function-in-a-struct/14434/3 "2017-12-15T14:09:03Z")

</div>

Thanks! That (especially the & in the inner function) helped make some progress while on my commute this morning. 😃

I still had a few re-borrowing problems but I solved them with Arc/RwLock. I'll keep working on it tomorrow and possibly come back for more suggestions. 😃

---

<div class="post-metadata">

**Author:** ![steffahn](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/steffahn/32/47569_2.png) [@steffahn](https://users.rust-lang.org/u/steffahn)\
**Post date:** [January 12, 2023, 8:46am UTC](https://users.rust-lang.org/t/storing-a-function-taking-a-function-in-a-struct/14434/4 "2023-01-12T08:46:43Z")

</div>


