4 ms·
Rust explicitly doesn't allow overriding `=`. Rust doesn't have templates, but rather parametric polymorphism and traits are part of that, they are used exactl
by dbaupp 13y ago
Rust explicitly doesn't allow overriding `=`.
Rust doesn't have templates, but rather parametric polymorphism and traits are part of that, they are used exactly like typeclasses in Haskell.
Static typing means a dropped semicolon never introduces a bug (a meaningfully forgotten semicolon almost always results in a compile error, I've never met a problem).
- fooyc 13y ago> Rust explicitly doesn't allow overriding `=`. In part 25 Copy and clone: "copied by default instead of moved, because these types implement the Copy trait" So implementing the Copy trait allow to modify the semantics of assigning an object to a variable, or of passing the object around; am I right ? Feels a lot like C++'s copy constructor. > Rust doesn't have templates, but rather parametric polymorphism and traits are part of that This is just an other name for templates. > Static typing means a dropped semicolon never introduces a bug Using the last expression as return value feels weird in a language that makes everything so uber-explicit, like ownership, stack vs heap allocation, etc
- dbaupp 13y agoCopy is a trait that is automatically implemented by the compiler (and cannot be manually overridden) that basically just says "semantically this type can be copied with memcpy", it doesn't actually control the runtime behaviour of =. Passing around a Copy type by-value leaves the source still usable ("a copy"), any other type leaves the source unusable at compile-time ("a move"); assignment is just a normal by-value use of the right-hand side. (The phrasing in that section isn't as clear as it could be.) They are templates with checking at the declaration, not the instantiations, so you don't get the errors nested deep inside libraries with ridiculous chains of messages. Don't knock the semicolon thing until you try it, it's actually pretty nice. :)
- fooyc 13y ago> Copy is a trait that is automatically implemented by the compiler (and cannot be manually overridden) that basically just says "semantically this type can be copied with memcpy", it doesn't actually control the runtime behaviour of =. (The phrasing in that section isn't as clear as it could be.) That's a very good news. Indeed that section is not clear, it looks like user defined classes could implement the copy trait, which would be really bad. Thanks for your answers.
- dbaupp 13y ago(BTW, I edited some more details about copy vs. move into my previous comment just after you replied.)
- rat87 13y agoI haven't dealt much with c++ templates but they are fully turing complete. I believe rust generics are weaker and more like java. In c++ templates try to see if a type can be used in a function. In rust you can only use a function if the type fulfils a trait which seems a lot like generic constraints in java/c#. I don't think making things explicit is a goal of rust. Making things safe as possible by default while being fas is. A good example is that most local variables are type inferenced rather then declared.
- steveklabnik 13y agoMaking _costs_ explicit is a goal of Rust. Stack allocation is syntactically cheaper than heap allocation, for example.
- fooyc 13y agoSadly, operator overloading is not a really good way of making costs explicit.