Trait specialization for slice

Hi,

I try to use the specialization feature of the nightly and I have something I can explains why it's not working. Can somebody tell me why the last call of the following code do not take the specialization?
https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=4b2b2c27d277bf1533dd31475230945c
without the specialization I have the expected behavior:
https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=cc4b9cec03e2257607c0e420f27d2673

Thank you!

Hi, it's because [1, 2, 3] is of type [i32; 3] and not [i32].
You can see it using this for example:

let _: () = [1, 2, 3];

If you want to use the specialization you can do:

[1, 2, 3].as_ref().something();
1 Like

impl<T> Do for T { ... }

Generic type parameters are implicitly constrained by Sized by default. But slice types are not Sized and not matching to the impl. You have to specify T: ?Sized to out-out Sized constraint.

1 Like

Thank you both,
Is there a way to specialize for [i32;N]? I can't find a way to do to it.

You need const generics, they are nightly only but work is been done to change that. playground

1 Like

Awesome! A big thank you!