# How to convert closure type to other type without calling?

**URL:** <https://users.rust-lang.org/t/how-to-convert-closure-type-to-other-type-without-calling/58963>\
**Category:** uncategorized\
**Created:** [April 27, 2021, 1:27am UTC](https://users.rust-lang.org/t/how-to-convert-closure-type-to-other-type-without-calling/58963 "2021-04-27T01:27:44Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![monkeyTux](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/monkeytux/32/9273_2.png) [@monkeyTux](https://users.rust-lang.org/u/monkeyTux)\
**Post date:** [April 27, 2021, 1:27am UTC](https://users.rust-lang.org/t/how-to-convert-closure-type-to-other-type-without-calling/58963/1 "2021-04-27T01:27:45Z")

</div>

Hi, could anyone help me?  
I need to map a Vec of closures with one type to other Vec with another type of closures but I don't know how to do it, example: [Rust Playground](https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=304a2dc3d1a514bc7c2a017d0b9e2f87)

```
    mod inner {

    #[derive(Debug)]
    pub enum InnerMsg {
        Value(i32)
    }
    
    pub fn update() -> Vec<impl Fn() -> InnerMsg> {
        vec![|| InnerMsg::Value(2)]
    }
}

#[derive(Debug)]
enum Msg {
    Inside(inner::InnerMsg),
    Other
}

impl From<inner::InnerMsg> for Msg {
    fn from(m: inner::InnerMsg) -> Self {
       Self::Inside(m) 
    }
}

pub fn update() -> Vec<impl Fn() -> Msg> {
    inner::update() // how to map msg type ?
}

fn main() {
  let cmds = update(); 
  
  for cmd in cmds {
     let msg = cmd();
     println!("{:?}", msg);
  }
}

```

---

<div class="post-metadata">

**Author:** ![derspiny](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/derspiny/32/3540_2.png) [@derspiny](https://users.rust-lang.org/u/derspiny)\
**Post date:** [April 27, 2021, 1:54am UTC](https://users.rust-lang.org/t/how-to-convert-closure-type-to-other-type-without-calling/58963/2 "2021-04-27T01:54:50Z")

</div>

```rust
pub fn update() -> Vec<impl Fn() -> Msg> {
    inner::update() // -> Vec<impl Fn() -> inner::InnerMsg)
        .into_iter() // -> impl Iterator<impl Fn() -> inner::InnerMsg>
        .map(|f| move || Msg::Inside(f()))
                         // -> impl Iterator<impl Fn() -> Msg>
        .collect() // -> Vec<impl Fn() -> Msg>
}

```

The `move || Msg::Inside(f())` lambda expression creates a new function that takes ownership of `f` (in each case, our original `Fn() -> inner::InnerMsg` function value) and yields a new function value that will, when evaluated, call `f` once, and wrap its return value in the `Msg` type.

Edit: Per your `impl From`, you can also write this as

```rust
pub fn update() -> Vec<impl Fn() -> Msg> {
    inner::update() // -> Vec<impl Fn() -> inner::InnerMsg)
        .into_iter() // -> impl Iterator<impl Fn() -> inner::InnerMsg>
        .map(|f| move || Msg::from(f()))
                         // -> impl Iterator<impl Fn() -> Msg>
        .collect() // -> Vec<impl Fn() -> Msg>
}

```

to the same effect.

---

<div class="post-metadata">

**Author:** ![monkeyTux](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/monkeytux/32/9273_2.png) [@monkeyTux](https://users.rust-lang.org/u/monkeyTux)\
**Post date:** [April 27, 2021, 2:12am UTC](https://users.rust-lang.org/t/how-to-convert-closure-type-to-other-type-without-calling/58963/3 "2021-04-27T02:12:03Z")

</div>

Thanks man, that was simpler than I expected, on my code I used a Box\<dyn Fn() -\> Msg\> instead of  
impl Fn() -\> Msg and this solution not worked, but I changed to impl Trait and just works. Thanks again.

---

<div class="post-metadata">

**Author:** ![system](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/system/32/51372_2.png) [@system](https://users.rust-lang.org/u/system)\
**Post date:** [July 26, 2021, 2:12am UTC](https://users.rust-lang.org/t/how-to-convert-closure-type-to-other-type-without-calling/58963/4 "2021-07-26T02:12:22Z")

</div>

Closed on July 26, 2021, 2:12am UTC

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.
