# Closures of the same type can be stored in an array, why can't they be stored in a Vec?

**URL:** <https://users.rust-lang.org/t/closures-of-the-same-type-can-be-stored-in-an-array-why-cant-they-be-stored-in-a-vec/66049>\
**Category:** help\
**Created:** [October 18, 2021, 3:19am UTC](https://users.rust-lang.org/t/closures-of-the-same-type-can-be-stored-in-an-array-why-cant-they-be-stored-in-a-vec/66049 "2021-10-18T03:19:03Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![0XHyh](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/0xhyh/32/19077_2.png) [@0XHyh](https://users.rust-lang.org/u/0XHyh)\
**Post date:** [October 18, 2021, 3:19am UTC](https://users.rust-lang.org/t/closures-of-the-same-type-can-be-stored-in-an-array-why-cant-they-be-stored-in-a-vec/66049/1 "2021-10-18T03:19:04Z")

</div>

[playground link](https://play.rust-lang.org/?version=stable&mode=debug&edition=2015&gist=026bf9ae584b00ea014cb51371bacc14)

Compile Error:

```nohighlight
error[E0308]: mismatched types
 --> src/main.rs:7:14
  |
4 | let closures_1 = || {};
  | ----- the expected closure
...
7 | let v2 = vec![closures_1, closures_2,]; // ❌
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected slice, found array of 2 elements
  |
  = note: expected struct `Box<[[closure@src/main.rs:4:22: 4:27]], _>`
             found struct `Box<[fn(); 2], std::alloc::Global>`
  = note: this error originates in the macro `vec` (in Nightly builds, run with -Z macro-backtrace for more info)

```

why is that?

---

<div class="post-metadata">

**Author:** ![cole-miller](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/cole-miller/32/16722_2.png) [@cole-miller](https://users.rust-lang.org/u/cole-miller)\
**Post date:** [October 18, 2021, 3:25am UTC](https://users.rust-lang.org/t/closures-of-the-same-type-can-be-stored-in-an-array-why-cant-they-be-stored-in-a-vec/66049/2 "2021-10-18T03:25:29Z")

</div>

Neither of the closures captures an environment, so they coerce to the function pointer type `fn()`. (If one or both of them had captures then neither example would compile.) It looks like something in the expansion of the `vec` macro is blocking that coercion. If you cast explicitly like `vec![closures_1 as fn(), closures_2 as fn()]` there's no error.

Edit: experimenting with manually expanding `vec!`, it looks like the coercion is blocked by `box` expressions: this compiles

```rust
fn main() {
    let f = || {};
    let g = || {};
    let v = <[_]>::into_vec(Box::new([f, g]));
}

```

but this doesn't (nightly-only)

```rust
#![feature(box_syntax)]
fn main() {
    let f = || {};
    let g = || {};
    let v = <[_]>::into_vec(box [f, g]);
}

```

I guess `x` in `box x` isn't a coercion site?

---

<div class="post-metadata">

**Author:** ![quinedot](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/quinedot/32/17693_2.png) [@quinedot](https://users.rust-lang.org/u/quinedot)\
**Post date:** [October 18, 2021, 5:57am UTC](https://users.rust-lang.org/t/closures-of-the-same-type-can-be-stored-in-an-array-why-cant-they-be-stored-in-a-vec/66049/3 "2021-10-18T05:57:57Z")

</div>

I _think_ this is [#22405](https://github.com/rust-lang/rust/issues/22405) / the downside listed in RFC 809, but didn't spend too much time digging.

Not sure if it's more desirable to keep the presumed optimization or to make the stdlib stop "cheating" in this case (by exploiting capabilities no longer expected to be made stable in their currrent form).

---

<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:** [January 16, 2022, 5:58am UTC](https://users.rust-lang.org/t/closures-of-the-same-type-can-be-stored-in-an-array-why-cant-they-be-stored-in-a-vec/66049/4 "2022-01-16T05:58:15Z")

</div>

Closed on January 16, 2022, 5:58am 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.
