# Include \`collect\_vec\` in \`std\`?

**URL:** <https://internals.rust-lang.org/t/include-collect-vec-in-std/7823>\
**Category:** libs\
**Created:** [June 28, 2018, 2:26pm UTC](https://internals.rust-lang.org/t/include-collect-vec-in-std/7823 "2018-06-28T14:26:53Z")\
**Posts on this page:** 4\
**Page:** 2

<div class="post-metadata">

**Author:** ![sfackler](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/sfackler/32/9_2.png) [@sfackler](https://internals.rust-lang.org/u/sfackler)\
**Post date:** [July 3, 2018, 4:23pm UTC](https://internals.rust-lang.org/t/include-collect-vec-in-std/7823/21 "2018-07-03T16:23:05Z")

</div>

A common example:

```rust
fn foo(s: &[u8]) { ... }

fn main() {
    let v = (0..128).collect();
    foo(&v);
}

```

```rust
error[E0277]: the trait bound `[u8]: std::marker::Sized` is not satisfied
 --> src/main.rs:4:9
  |
4 | let v = (0..128).collect();
  | ^ `[u8]` does not have a constant size known at compile-time
  |
  = help: the trait `std::marker::Sized` is not implemented for `[u8]`
  = note: all local variables must have a statically known size

error[E0277]: the trait bound `[u8]: std::iter::FromIterator<{integer}>` is not satisfied
 --> src/main.rs:4:22
  |
4 | let v = (0..128).collect();
  | ^^^^^^^ a collection of type `[u8]` cannot be built from an iterator over elements of type `{integer}`
  |
  = help: the trait `std::iter::FromIterator<{integer}>` is not implemented for `[u8]`

```

---

<div class="post-metadata">

**Author:** ![dhardy](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/dhardy/32/2399_2.png) [@dhardy](https://internals.rust-lang.org/u/dhardy)\
**Post date:** [July 5, 2018, 7:03am UTC](https://internals.rust-lang.org/t/include-collect-vec-in-std/7823/22 "2018-07-05T07:03:39Z")

</div>

I’ve been using the Turbofish quite a few times recently. One example is testing functions which return an iterator, where for testing the complete output it’s easiest to compare against a `Vec` (seems like type inference should work here, but it wasn’t); also in case the iterator output must be used multiple times and the iterator cannot be cloned then the output must be collected.

* * *

It seems like there’s quite a bit of support for adding `Iterator::into_vec`, but [as pointed out this isn’t possible](https://internals.rust-lang.org/t/include-collect-vec-in-std/7823/16?u=dhardy).

An alternative is to add a trait like `trait IntoVec { type T; fn into_vec(self) -> Vec<T>; }`, but adding a trait just for this seems unreasonable.

Another possibility: `Vec::collect_from(iter)`.

Or maybe we just have to accept that adding this isn’t reasonable? ☹

---

<div class="post-metadata">

**Author:** ![Nemo157](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/nemo157/32/11585_2.png) [@Nemo157](https://internals.rust-lang.org/u/Nemo157)\
**Post date:** [July 5, 2018, 8:04am UTC](https://internals.rust-lang.org/t/include-collect-vec-in-std/7823/23 "2018-07-05T08:04:07Z")

</div>

> [@dhardy](#):
>
> Another possibility: `Vec::collect_from(iter)` .

Do you mean

```rust
use std::iter::FromIterator;
Vec::from_iter(iter);

```

I'm still not sure if Editions can add new traits to the prelude, maybe `FromIterator` could be included in Edition 2018? Checking github it was briefly mentioned in [Add an extended prelude for the 2018 editions by SimonSapin · Pull Request #51434 · rust-lang/rust · GitHub](https://github.com/rust-lang/rust/pull/51434)

---

<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/include-collect-vec-in-std/7823/24 "2019-03-25T08:30:26Z")

</div>

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

[Previous page](https://internals.rust-lang.org/t/include-collect-vec-in-std/7823.md?page=1)
