[help] impl block for generic type overriden by specific type

https://lemmy.world/post/4825824

[help] impl block for generic type overriden by specific type - Lemmy.world

I want to do basically this: rust struct MyStruct { data: T } impl for MyStruct { fn foo() { println!("Generic") } } impl for MyStruct { fn foo() { println!("u32") } } I have tried doing rust impl for MyStruct{ ... } But it doesn’t seem to work. I’ve also tried various things with traits but none of them seem to work. Is this even possible?

Something like this?
I’m sorry, no wonder you are lost, so it made no sense when you wrote this comment, lemmy deleted everything between the “less than” character and “>”. I had to change them for “<” in order for them not to get deleted.

lemmy deleted everything between the “less than” character and “>”.

Lemmy also escaped the ampersands in their comment’s link 😉

Isn’t broken sanitization great!

What you are looking for is called specialization.

It is currently unstable and incomplete, which means you can activate it by adding the macros #![feature(specialization)] and #![allow(incomplete_features)] at the beginning of the file. But you have no guarantee that it will work the same way on the next version of rust.

Tracking issue for specialization (RFC 1210) · Issue #31844 · rust-lang/rust

This is a tracking issue for specialization (rust-lang/rfcs#1210). Major implementation steps: Land #30652 =) Restrictions around lifetime dispatch (currently a soundness hole) default impl (#37653...

GitHub
Yeah, that seems like it would work! Unfortunately I can’t use unstable features. I’ll keep it in mind for other projects though.