Is it possible to implement `From` for a struct that itself `impl IntoIterator`?
⚓ Rust 📅 2026-01-22 👤 surdeus 👁️ 2Rust playground illustrating the issue
I have a struct (call it Collection) which implements IntoIterator. I'd like to be able to instantiate this struct using from, passing in another IntoIterator:
let collection = Collection::from([1, 2, 3]);
So, I believe I need to implement From<impl IntoIterator> for Collection. However, this results in conflicting implementations of From with the default blanket implementation From<T> for T.
CollectionimplementsFrom<I> where I: IntoIteratorCollectionimplementsIntoIteratorCollectioncould beI- This gives
From<Collection> for Collection - Which has the same signature as the blanket
From<T> for T, resulting in the conflict
I haven't been able to find any way around this... I was thinking I could exclude Collection from I, but this requires negative_impls, which is unstable.
This feels like a fairly common pattern? Looking at the docs for Vec<> I can see it implements From<> with brute force for the common iterable types, but I was hoping to be able to write something more generic.
I have of course already implemented FromIterator for Collection, so I can construct it with from_iter() – I just want to make the shorter from() work too.
2 posts - 2 participants
🏷️ Rust_feed