3 ms·
You can't do Rust without generics at all, they're utterly fundamental. Take the for loop. Rust's for loop is literally sugar for the infinite loop over an Iter
by tialaramex 4y ago
You can't do Rust without generics at all, they're utterly fundamental. Take the for loop. Rust's for loop is literally sugar for the infinite loop over an Iterator (a generic type) which emits an Option generic, with a break for None.
Rust calls such types "langitems" and they're required for the core Rust language. An allocator (for heap memory) isn't mandatory, fancy file I/O and networking aren't mandatory... But the langitems, and thus generic types, are mandatory.
- layer8 4y agoRust could conceivably have worked similar to C, where you have to cast void* to the correct type. You could still have borrowing annotations on such generic pointers, similar to how C(++) has const. Having borrowing semantics doesn’t necessarily imply a general-purpose generics feature. Stuff like iterators and option types would obviously be different then.
- tialaramex 4y agoYou could build a completely different language with some of Rust's features, if you go back a few years before Rust 1.0 there are lots of possibilities, but what I was getting at is that Rust itself is much more tightly wedded to the generics than many of the languages people associate with generics, a "Rust without generics" isn't Rust at all. Even in C++ the generics are a later addition, Stroustrup's early 1980s language doesn't have generic types, templates were a proposal added to what would become the C++ 98 standard and the Standard Template Library presents generic types for containers in C++ years after the C++ language was first popularized.
- layer8 4y agoI don’t think we really disagree. I was originally reaponding to the claim by DANK_YACHT stating that a (statically typed) language can be successful without having generics as long as it has other sufficiently innovative features. As a “counterexample” I hypothesized a version of Rust without generics but with the borrow checker (arguably the innovative feature most decisive for Rust’s success) and argued that I would doubt that this version would have been very successful, exactly because its lack of generics (which I believe aligns with what you are arguing). I’m not sure if you’re arguing that this hypothetical scenario isn’t a valid counterargument to the original claim I was responding to.
- vlang1dot0 4y agoYes, technically you can do that but it makes no sense from a design point of view to have a language that cares so much about safety it adds a borrowing system but then says "fuck it" to type safety. You have to have type safety to have memory safety and that was the whole point of Rust.