3 ms·
I like the real Rust syntax more, save for double colons. (And I might prefer other brackets to <> with generics) But how is that syntax of yours separating typ
by GolDDranks 4y ago
I like the real Rust syntax more, save for double colons. (And I might prefer other brackets to <> with generics) But how is that syntax of yours separating types and generics with traits? Currently `AsRef<Path>` looks like a type although it's not supposed to be one.
- scotty79 4y agoDo they really need to be separated? Does it matter all that much when you are creating a function whether parameter is a specific type or just sny type that implements specific trait?
- GolDDranks 4y agoFunctions 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.
- Measter 4y agoIt matters for two reasons I can see: * Is this function monomorphised for each type passed in or is the argument passed in as a trait object? If the latter, does it implicitly fall back to monomorpisation if the trait is not Object-safe(e.g. it takes or return `Self` by ownership)? How do we specify which of the two to use when we need to for performance reasons? * Let's say we have a function with two inputs: `func foo(a: SomeTrait, b: SomeTrait)` How do I specify that both inputs must be the same type?