4 ms·
Functions with generics are not quite first class citizens in Rust, so there's a difference also in the type of a non-generic function and a generic function. O
by GolDDranks 4y ago
Functions with generics are not quite first class citizens in Rust, so there's a difference also in the type of a non-generic function and a generic function. Or rather, the generic function doesn't have a "type" as Rust understands it. (It has a higher kinded type, but Rust doesn't support those.)
Here's a demonstration of the difference:
https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=b80c4466bf61902e7a2ac3ad5a5c87d1 https://play.rust-lang.org/?version=stable&mode=debug&editio...
- scotty79 4y agoThanks for this example. I wasn't aware if that. But still no idea why I need to write: fn poly(a: impl Display) { println!("{a}"); } instead of just fn poly(a: Display) { println!("{a}"); } Can I use same identifier for type and trait at the same time?
- GolDDranks 4y agoYeah, it could be that the compiler just referred back to the definition of the name to see if it is a type or a trait. Actually, there was a plan to implement that behaviour, but it was given up in favor of the current "explicit" style. There was some fears of confusing traits and types being more confusing in the long run.