How is min_specialization used?

When I compile my crate with #[feature(specialization)], I get a warning that specialization is unsound, and that I should consider using #[feature(min_specialization)] instead.

But I can't find any information whatsoever on how to use it. The tracking issue for specialization links to the PR where min_specialization was implemented, and that PR has an example.

But when I try to compile that example myself, I get the standard "conflicting implementation" message.

Example lib.rs, pulled more or less directly from the PR:

#[feature(min_specialization)]

trait TestTrait<T> {}
impl<T, I: Iterator<Item=T>> TestTrait<T> for I { /* default impl */ }
impl<T> TestTrait<T> for std::vec::IntoIter<T> { /* specialized impl */ }

Error:

error[E0119]: conflicting implementations of trait `TestTrait<_>` for type `std::vec::IntoIter<_>`:
 --> src\lib.rs:8:1
  |
7 | impl<T, I: Iterator<Item=T>> TestTrait<T> for I { /* default impl */ }
  | ----------------------------------------------- first implementation here
8 | impl<T> TestTrait<T> for std::vec::IntoIter<T> { /* specialized impl */ }
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ conflicting implementation for `std::vec::IntoIter<_>`

Is there something obvious I'm missing here? Is there some documentation I can't find? If not, why does the compiler recommend that I use an undocumented feature?

It works fine in the playground (example). Are you sure you're using the nightly compiler?

1 Like

If I wasn't using nightly, I'd expect that it would complain about the feature attribute.

PS D:\Documents\Rust> rustc --version
rustc 1.50.0-nightly (eb4fc71dc 2020-12-17)

I found the problem.

If you look at the original post, I was activating the feature with

#[feature(min_specialization)]

Which is missing the exclamation point. So yes, I was missing something obvious. I'm surprised this even compiled.

2 Likes