Repository navigation
Cannot use std::iter::try_collect after importing itertools #666
Copy link
Copy link
Open
Labels
fallible-iteratorIterator of results/optionsIterator of results/options
Description
The try_collect method in itertools is shadowing the method of the same name in std::iter on nightly :
https://doc.rust-lang.org/std/iter/trait.Iterator.html#method.try_collect
#![allow(unused)]
#![feature(iterator_try_collect)]
use itertools::Itertools; // 0.10.5
fn main() {
let u = vec![Some(1), Some(2), Some(3)];
let v = u.into_iter().try_collect::<Vec<i32>>();
assert_eq!(v, Some(vec![1, 2, 3]));
}
Compiling playground v0.0.1 (/playground)
error[[E0107]](https://doc.rust-lang.org/nightly/error-index.html#E0107): this associated function takes 3 generic arguments but 1 generic argument was supplied
--> src/main.rs:8:27
|
8 | let v = u.into_iter().try_collect::<Vec<i32>>();
| ^^^^^^^^^^^ -------- supplied 1 generic argument
| |
| expected 3 generic arguments
|
note: associated function defined here, with 3 generic parameters: `T`, `U`, `E`
--> /playground/.cargo/registry/src/github.com-1ecc6299db9ec823/itertools-0.10.5/src/lib.rs:2009:8
|
2009 | fn try_collect<T, U, E>(self) -> Result<U, E>
| ^^^^^^^^^^^ - - -
help: add missing generic arguments
|
8 | let v = u.into_iter().try_collect::<Vec<i32>, U, E>();
| ++++++
error[[E0271]](https://doc.rust-lang.org/nightly/error-index.html#E0271): expected `IntoIter<Option<{integer}>>` to be an iterator that yields `Result<Vec<i32>, _>`, but it yields `Option<{integer}>`
--> src/main.rs:8:27
|
8 | let v = u.into_iter().try_collect::<Vec<i32>>();
| ^^^^^^^^^^^ expected enum `Result`, found enum `Option`
|
= note: expected enum `Result<Vec<i32>, _>`
found enum `Option<{integer}>`
note: the method call chain might not have had the expected associated types
--> src/main.rs:8:15
|
7 | let u = vec![Some(1), Some(2), Some(3)];
| ------------------------------- this expression has type `Vec<Option<{integer}>>`
8 | let v = u.into_iter().try_collect::<Vec<i32>>();
| ^^^^^^^^^^^ `Iterator::Item` is `Option<{integer}>` here
note: required by a bound in `itertools::Itertools::try_collect`
--> /playground/.cargo/registry/src/github.com-1ecc6299db9ec823/itertools-0.10.5/src/lib.rs:2011:32
|
2011 | Self: Sized + Iterator<Item = Result<T, E>>,
| ^^^^^^^^^^^^^^^^^^^ required by this bound in `Itertools::try_collect`
error[[E0308]](https://doc.rust-lang.org/nightly/error-index.html#E0308): mismatched types
--> src/main.rs:9:5
|
9 | assert_eq!(v, Some(vec![1, 2, 3]));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected enum `Result`, found enum `Option`
|
= note: expected enum `Result<_, _>`
found enum `Option<Vec<{integer}>>`
= note: this error originates in the macro `assert_eq` (in Nightly builds, run with -Z macro-backtrace for more info)
Some errors have detailed explanations: E0107, E0271, E0308.
For more information about an error, try `rustc --explain E0107`.
error: could not compile `playground` due to 3 previous errors
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
fallible-iteratorIterator of results/optionsIterator of results/options
Activity
Philippe-Cholet commentedon Oct 7, 2023
I think this should be fixed once RFC2845 (#702 (comment)) is implemented, as one should then shadow the other.
git-staus commentedon Aug 21, 2025
If I am not mistaken RFC2845 has been merged now, but the impossibility to use (
nightly) standardtry_collectwhenItertoolsis in scope persists. This has been mentioned in #402, #710, #830, #844, #934, and #951 already and I understand there is an ungoing discussion about naming conflicts in general, but I'd like to add my voice to this issue anyway.I have been chasing my tail for quite some time yesterday with code along the following lines:
I am quite fond of the (slighly more flexible)
try_collectfromstd, but in this case I neededcountsas well. As of yet (itertools0.14.0) I haven't found a decent way to avoid this conflict...git-staus commentedon Aug 21, 2025
Another idea I have not seen mentioned before: how about introducing an on-by-default feature flag? (e.g.
breaks-nightlyormight-conflictor whatever) This wayitertoolsfunctionality that might conflict could simply be put behind such a flag without introducing breakage, while still providing those onnightlywith a way to use all the rest ofitertools.I am happy to draft a PR if this is deemed a viable approach.
git-staus commentedon Aug 21, 2025
Despite the parallel naming in
itertoolsand thenightlystandard library, it seems the name oftry_collectis currently still subject to change (tracking issue). I guess this weakens any arguments for solving this conflict from theitertoolsfront...