# Const constructor of Vec

**URL:** <https://internals.rust-lang.org/t/const-constructor-of-vec/7390>\
**Category:** libs\
**Created:** [April 25, 2018, 2:55am UTC](https://internals.rust-lang.org/t/const-constructor-of-vec/7390 "2018-04-25T02:55:23Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![mark-i-m](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/mark-i-m/32/3167_2.png) [@mark-i-m](https://internals.rust-lang.org/u/mark-i-m)\
**Post date:** [April 25, 2018, 2:55am UTC](https://internals.rust-lang.org/t/const-constructor-of-vec/7390/1 "2018-04-25T02:55:23Z")

</div>

I was wondering why there isn’t a `const fn` to create a new empty `Vec`. It seems like this ought to be possible: it just doesn’t allocate any memory.

Having something like that would allow you to have `static` initializers with `Vec` in them without using `lazy_static`.

---

<div class="post-metadata">

**Author:** ![glandium](https://avatars.discourse-cdn.com/v4/letter/g/3d9bf3/32.png) [@glandium](https://internals.rust-lang.org/u/glandium)\
**Post date:** [April 25, 2018, 4:44am UTC](https://internals.rust-lang.org/t/const-constructor-of-vec/7390/2 "2018-04-25T04:44:52Z")

</div>

You should be able to use `vec![]`.

---

<div class="post-metadata">

**Author:** ![H2CO3](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/h2co3/32/2849_2.png) [@H2CO3](https://internals.rust-lang.org/u/H2CO3)\
**Post date:** [April 25, 2018, 6:40am UTC](https://internals.rust-lang.org/t/const-constructor-of-vec/7390/3 "2018-04-25T06:40:26Z")

</div>

> [@mark-i-m](#):
>
> have static initializers with `Vec` in them without using `lazy_static`.

But **why** do you want a static `Vec` without `lazy_static` anyway? If your `static` is immutable, why don't you use a primitive, sized array instead? If your `static` is mutable, and you want to grow the vector, how do you want to ensure thread/concurrency/memory safety? Do you always want to make manual `unsafe` locking code around your static?

---

<div class="post-metadata">

**Author:** ![Ixrec](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/ixrec/32/6754_2.png) [@Ixrec](https://internals.rust-lang.org/u/Ixrec)\
**Post date:** [April 25, 2018, 10:40am UTC](https://internals.rust-lang.org/t/const-constructor-of-vec/7390/4 "2018-04-25T10:40:58Z")

</div>

I’ve definitely seen `Vec::new()` get thrown around a few times as an example of a function that could be `const fn`, and [now that we allow Drop types in consts](https://github.com/rust-lang/rfcs/pull/1817) I’m not aware of any reason not to make it a `const fn`. I think we just haven’t gotten around to it yet.

---

<div class="post-metadata">

**Author:** ![skade](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/skade/32/3333_2.png) [@skade](https://internals.rust-lang.org/u/skade)\
**Post date:** [April 25, 2018, 12:31pm UTC](https://internals.rust-lang.org/t/const-constructor-of-vec/7390/5 "2018-04-25T12:31:17Z")

</div>

> [@H2CO3](#):
>
> If your static is mutable, and you want to grow the vector, how do you want to ensure thread/concurrency/memory safety?

`Mutex::new()` is a `const fn` in newer nightlies. [https://github.com/rust-lang/rust/blob/master/src/libstd/sys\_common/mutex.rs#L27](https://github.com/rust-lang/rust/blob/master/src/libstd/sys_common/mutex.rs#L27)

---

<div class="post-metadata">

**Author:** ![mark-i-m](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/mark-i-m/32/3167_2.png) [@mark-i-m](https://internals.rust-lang.org/u/mark-i-m)\
**Post date:** [April 25, 2018, 5:10pm UTC](https://internals.rust-lang.org/t/const-constructor-of-vec/7390/6 "2018-04-25T17:10:33Z")

</div>

> [@H2CO3](#):
>
> Do you always want to make manual unsafe locking code around your static?

> [@skade](#):
>
> Mutex::new() is a const fn in newer nightlies. [https://github.com/rust-lang/rust/blob/master/src/libstd/sys\_common/mutex.rs#L27](https://github.com/rust-lang/rust/blob/master/src/libstd/sys_common/mutex.rs#L27)

You can also use something like [`spin`](https://crates.io/crates/spin)::Mutex, which has a `const` constructor and works on `no_std`.

I think it's always possible to use `lazy_static`. However, TBH, it really annoys me. I've tried many times to get used to it, but I really shouldn't need the extra layers around my static just to initialize it at the very beginning of the program anyway...

---

<div class="post-metadata">

**Author:** ![Centril](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/centril/32/3334_2.png) [@Centril](https://internals.rust-lang.org/u/Centril)\
**Post date:** [April 25, 2018, 5:32pm UTC](https://internals.rust-lang.org/t/const-constructor-of-vec/7390/7 "2018-04-25T17:32:36Z")

</div>

As a general rule: Everything that can be `const fn` should be `const fn`, unless we have reason to believe that future developments might change that.

---

<div class="post-metadata">

**Author:** ![H2CO3](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/h2co3/32/2849_2.png) [@H2CO3](https://internals.rust-lang.org/u/H2CO3)\
**Post date:** [April 25, 2018, 5:39pm UTC](https://internals.rust-lang.org/t/const-constructor-of-vec/7390/8 "2018-04-25T17:39:00Z")

</div>

Oh yeah, right. That’s a fair point, so we do have `const`-constructible locking types.

---

<div class="post-metadata">

**Author:** ![mark-i-m](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/mark-i-m/32/3167_2.png) [@mark-i-m](https://internals.rust-lang.org/u/mark-i-m)\
**Post date:** [April 25, 2018, 9:18pm UTC](https://internals.rust-lang.org/t/const-constructor-of-vec/7390/9 "2018-04-25T21:18:54Z")

</div>

So it looks like at the moment we are blocked by [https://github.com/rust-lang/rust/issues/49146](https://github.com/rust-lang/rust/issues/49146)

---

<div class="post-metadata">

**Author:** ![mark-i-m](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/mark-i-m/32/3167_2.png) [@mark-i-m](https://internals.rust-lang.org/u/mark-i-m)\
**Post date:** [April 26, 2018, 5:45pm UTC](https://internals.rust-lang.org/t/const-constructor-of-vec/7390/10 "2018-04-26T17:45:37Z")

</div>

However, it looks like we can work around it for now: [https://github.com/rust-lang/rust/pull/50233](https://github.com/rust-lang/rust/pull/50233)

---

<div class="post-metadata">

**Author:** ![mark-i-m](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/mark-i-m/32/3167_2.png) [@mark-i-m](https://internals.rust-lang.org/u/mark-i-m)\
**Post date:** [April 30, 2018, 10:27pm UTC](https://internals.rust-lang.org/t/const-constructor-of-vec/7390/11 "2018-04-30T22:27:12Z")

</div>

Yay! My PR merged, so this is actually done now…

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/system/32/14092_2.png) [@system](https://internals.rust-lang.org/u/system)\
**Post date:** [March 25, 2019, 8:30am UTC](https://internals.rust-lang.org/t/const-constructor-of-vec/7390/12 "2019-03-25T08:30:07Z")

</div>

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