Passing functions as parameters


I'm trying to create a struct and impl for that which

  1. Holds a function
  2. That function receives a function as a parameter
  3. Which in turn is called with a function

I have tried multiple ways to type it, mix different responses from SO but I get more en more confused. It would be awesome if someone could explain how to do it and why Rust seems so particularly picky in this area.

This is the relevant code:

pub struct StatefulComponent<'a, State: 'a, Updater> where Updater: 'a + Fn(State) -> State {
  pub state: State,
  pub render: &'a Fn(&State, Updater) -> RenderChild<'a>,

impl<'a, State, Updater> Component<'a> for StatefulComponent<'a, State, Updater> where Updater: 'a + Fn(State) -> State {
  fn render(&self) -> RenderChild<'a> {
    let set_state = |_| {}; // Currently this passed function does nothing, I just want to get the types to work first.
    (self.render)(&self.state, set_state)
struct RenderChild<'a>(std::marker::PhantomData<&'a ()>); // I made this up to make it compiler

struct StatefulComponent<'a, State: 'a>
    pub state: State,
    pub render: &'a Fn(&State, &Fn(State) -> State) -> RenderChild<'a>,

trait Component<'a> { // Made this up as well
    fn render(&self) -> RenderChild<'a>;

impl<'a, State> Component<'a> for StatefulComponent<'a, State>
    fn render(&self) -> RenderChild<'a> {
        let set_state = |_| {unimplemented!()};
        (self.render)(&self.state, &set_state)


Basically, you cannot pass a closure to your render trait object in StatefulComponent because it expects the specific type Updater, which is a generic type the caller picks when creating the StatefulComponent. However, I don't think you need this constraint anyway so I dropped Updater entirely.

Also, not sure if you specifically want a trait object for the Fn or whether you'd want to use generics instead - I kept the trait object version here.

Thanks, however the function passed to render does not take and return state, it takes a function that should take a function that takes and return state. Sorry if that was unclear from the question. I updated the playground based on your feedback: Rust Playground

As you can see I now get problems with unknown size at compile time, which was what I tried to fix with Updater but that might have been totally wrong.
I can get around the sizing problems by having every Fn a reference however that complicates the usage from the render function as it has to deal with lifetimes for that Fn when it returns yet another closure as a member of the RenderChild Rust Playground

No, that’s my fault - I didn’t read carefully enough.

I’m on mobile now but will have a more detailed look later. From a cursory glance, I don’t think you can return a RenderChild whose lifetime is tied to the StatefulComponent yet also box up a closure object into it which is received via an argument of arbitrary lifetime.

I played around a bit with the lifetime issues, but had trouble giving concrete advice since I can't entirely figure out how all of the pieces are intended to fit together. Perhaps we can temporarily make the problem simpler in order to help get a minimum complete example:

Making the problem simpler, a.k.a. RC all the things

For now, let's get rid of the lifetimes by using owned data everywhere. The tools of the trade are:

  • Cloning profusely
  • move closures to eliminate borrowing from the environment
  • Rc<T> for things that you can't clone, or can't clone cheaply
  • Rc<RefCell<T>> for things that you need to mutate.

By doing this we're giving up many of the reasons why one would want to use rust in the first place; but this isn't supposed to be for forever.

Here, I made the second example compile:

use ::std::rc::Rc;

struct RenderChild(String, Rc<Fn()>);

type StateFn<S> = Rc<Fn(S) -> S>; // state transition function
type IoFn<A> = Rc<Fn(A)>;         // does something for side-effects

// I guess that makes this the type of a function that takes a
// state transition function, and uses it to update internal state?
type SetterFn<S> = IoFn< StateFn<S> >;

struct StatefulComponent<S>
    pub state: S,
    // uhhhh still don't know what this is
    pub render: Rc<Fn(S, SetterFn<S>) -> RenderChild>,

trait Component {
    fn render(&self) -> RenderChild;

impl<S: Clone> Component for StatefulComponent<S>
    fn render(&self) -> RenderChild {
        let set_state: SetterFn<S> = Rc::new(|_| unimplemented!());
        (self.render)(self.state.clone(), set_state)

fn counter(_: i32, set_state: SetterFn<i32>) -> RenderChild {
        Rc::new(move || set_state(Rc::new(|val| val + 1))))

fn main() {
  let cnt = StatefulComponent {
    state: 0,
    render: Rc::new(counter),

If this can be completed to have all the working pieces, then it might be an easier starting point as we can then incrementally add borrowed forms and lifetimes.

Thanks! I still haven't got it to work completely but I feel that I now know what I should read up on. Rusts lifetime model is not easy in all cases...