# Optional .collect() when combinator is last operation

**URL:** <https://internals.rust-lang.org/t/optional-collect-when-combinator-is-last-operation/21417>\
**Category:** language design\
**Created:** [August 22, 2024, 7:28am UTC](https://internals.rust-lang.org/t/optional-collect-when-combinator-is-last-operation/21417 "2024-08-22T07:28:58Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![User12345](https://avatars.discourse-cdn.com/v4/letter/u/bcef8e/32.png) [@User12345](https://internals.rust-lang.org/u/User12345)\
**Post date:** [August 22, 2024, 7:28am UTC](https://internals.rust-lang.org/t/optional-collect-when-combinator-is-last-operation/21417/1 "2024-08-22T07:28:58Z")

</div>

```rust
Let's take this example: 

let numbers = vec![1, 2, 3, 4, 5];
let squares_of_evens: Vec<i32> = numbers.iter()
    .filter(|&n| n % 2 == 0)
    .map(|&n| n * n)
    .collect();

```

- Since `.map()` is the last operation, we can skip calling `.collect()`
- Hence `Vec<i32>` part wouldn't be needed anymore, since we already know that current vector comprises of i32 elements, so it would be auto inferred by compiler.
- "Iterators are lazy", but since there is an assignment to `squares_of_evens`, hence we could consider it "consumed"
- `.collect()` can still be used when we explicitly want to convert it into a different type (e.g. `let squares_of_evens: Vec<u32>`)

Thus we could have such updated syntax:

```rust
let numbers = vec![1, 2, 3, 4, 5];
let squares_of_evens = numbers.iter()
    .filter(|&n| n % 2 == 0)
    .map(|&n| n * n);

```

The same approach could also be applicable to other similar combinators, e.g., filter

```rust
let numbers = vec![1, 2, 3, 4, 5];
let even_numbers = numbers.iter()
    .filter(|&n| n % 2 == 0);
    // .collect() not needed here
    // since `.filter` is the last operation, it can auto return a vector of i32 elements
   // as nothing has changed during .filter operation

```

---

<div class="post-metadata">

**Author:** ![jdahlstrom](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/jdahlstrom/32/3351_2.png) [@jdahlstrom](https://internals.rust-lang.org/u/jdahlstrom)\
**Post date:** [August 22, 2024, 7:46am UTC](https://internals.rust-lang.org/t/optional-collect-when-combinator-is-last-operation/21417/2 "2024-08-22T07:46:59Z")

</div>

No, this is both unfeasible and undesirable.

First off, how would you even implement it? Iterators are normal library code, they have no idea what’s the "last operation" in a chain of calls and can’t return different types based on such things.

Second, there are many different types you can `collect` into; in fact an unbounded number because any type can implement `FromIter` and become `collect`-able. `Vec` is just the most common use case.

And third, we don’t _want_ any sort of eager auto-collecting. Iterators are lazy for a reason, and often it’s possible to avoid the cost of materializing the iterator at all.

---

<div class="post-metadata">

**Author:** ![SkiFire13](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/skifire13/32/7579_2.png) [@SkiFire13](https://internals.rust-lang.org/u/SkiFire13)\
**Post date:** [August 22, 2024, 8:10am UTC](https://internals.rust-lang.org/t/optional-collect-when-combinator-is-last-operation/21417/3 "2024-08-22T08:10:06Z")

</div>

> [@User12345](#):
>
> Hence `Vec<u32>` part wouldn't be needed anymore, since we already know that current vector comprises of u32 elements, so it would be auto inferred by compiler.

The compiler doesn't know that you wanted it to contain `u32` elements. In fact if you don't specify anything the compiler will fallback to assuming `i32` elements.

> [@User12345](#):
>
> Thus we could have such updated syntax:
> 
> ```rust
> let numbers = vec![1, 2, 3, 4, 5];
> let squares_of_evens = numbers.iter()
> .filter(|&n| n % 2 == 0)
> .map(|&n| n * n);
> 
> ```

This is already valid code, and the type of `squares_of_evens` would be something like `Map<Filter<std::slice::Iter<'_, {some integer type}>, {some anonymous closure}>, {some other anonymous closure}>`, hence it cannot be `Vec<u32>` or `Vec<i32>`.

---

<div class="post-metadata">

**Author:** ![sahnehaeubchen](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/sahnehaeubchen/32/9709_2.png) [@sahnehaeubchen](https://internals.rust-lang.org/u/sahnehaeubchen)\
**Post date:** [August 22, 2024, 12:19pm UTC](https://internals.rust-lang.org/t/optional-collect-when-combinator-is-last-operation/21417/4 "2024-08-22T12:19:17Z")

</div>

This ultimatly means to introduce some kind of "collect coercion" similar to [deref coercion](https://doc.rust-lang.org/std/ops/trait.Deref.html#deref-coercion) which executes a lot of hidden code with idiomatic trait implementations.

---

<div class="post-metadata">

**Author:** ![farnz](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/farnz/32/6587_2.png) [@farnz](https://internals.rust-lang.org/u/farnz)\
**Post date:** [August 22, 2024, 12:45pm UTC](https://internals.rust-lang.org/t/optional-collect-when-combinator-is-last-operation/21417/5 "2024-08-22T12:45:56Z")

</div>

This would make [this silly code](https://play.rust-lang.org/?version=stable&mode=release&edition=2021&gist=7a703cea618f5cc83278fa1a1bda0030) stop working - I `map` an infinite iterator, and then do an operation later that gets me a finite result.

If you make `map` implicitly call `collect()` when it's the last operation in a series of transforms, then the `map` operation will enter an infinite loop. Further, as written, it'll stop compiling, because there is no `Vec::take_while` operation.

---

<div class="post-metadata">

**Author:** ![User12345](https://avatars.discourse-cdn.com/v4/letter/u/bcef8e/32.png) [@User12345](https://internals.rust-lang.org/u/User12345)\
**Post date:** [August 22, 2024, 7:41pm UTC](https://internals.rust-lang.org/t/optional-collect-when-combinator-is-last-operation/21417/6 "2024-08-22T19:41:56Z")

</div>

> [@SkiFire13](#):
>
> In fact if you don't specify anything the compiler will fallback to assuming `i32` elements.

That's correct. Description updated.

---

<div class="post-metadata">

**Author:** ![steffahn](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/steffahn/32/13288_2.png) [@steffahn](https://internals.rust-lang.org/u/steffahn)\
**Post date:** [February 13, 2026, 7:42pm UTC](https://internals.rust-lang.org/t/optional-collect-when-combinator-is-last-operation/21417/9 "2026-02-13T19:42:45Z")

</div>

Closed on February 13, 2026, 7:42pm UTC

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