3 ms·
Pretty much all misconceptions named there can be avoided by following the advice that every Rust programmer will give you: read the damn book, it will save you
by fluffything 6y ago
Pretty much all misconceptions named there can be avoided by following the advice that every Rust programmer will give you: read the damn book, it will save you a lot of time.
If you are in the category of users that never RTFM, then I guess this article might be for you, but then kind of by definition, you won't read this article either, so...
---
For example, in C++ a generic `T` can only mean an owned type:
template<class T> void foo(T t);
template<class T> void bar(T& t);
You can't pass `foo` a reference to `T`, you'd need to pass it an owned type like `foo(std::ref(t))` instead or similar.
In Rust, when you write
fn foo<T>(t: T);
you can call `foo` with an owned type or a reference, no problems, generic types are... well... generic.
So if you start writing Rust code with a C++ background without reading the book, then you might probably think that generics in Rust work like templates in C++. One couldn't be more wrong about this, they are in fact completely different language features. They might solve very similar problems, but they are as different as Python is from Haskell.
- gpderetta 6y ago> For example, in C++ a generic `T` can only mean an owned type: Wrong. template<class T> void foo(T t) { } int main() { int x = 0; foo<int&>(x); } Unadorned Ts as function parameters will by default be deducted as value types, but a generic T parameter can represent references just fine. This is especially important for template classes. > you might probably think that generics in Rust work like templates in C++. One couldn't be more wrong about this, they are in fact completely different language features. They might solve very similar problems, but they are as different as Python is from Haskell. it seems to me that they are more similar than different.
- fluffything 6y ago> Wrong. Indeed, thanks for the correction! > it seems to me that they are more similar than different. I think the Haskell vs Python comparison is accurate. Rust generics are strongly typed (definitions are checked), while C++ templates are "weakly" typed (type checked when they are instantiated). In C++ it is trivial to write templates that will never type check when instantiated, while in Rust this is impossible.
- gpderetta 6y ago>> Wrong. >Indeed, thanks for the correction! Sorry for the brusque response, I had a "someone is wrong on the internet moment". >Rust generics are strongly typed (definitions are checked), while C++ templates are "weakly" typed (type checked when they are instantiated). True. The usual solution is to force-instantiate them against archetype classes. While it is not perfect it is a good approximation.
- fluffything 6y ago> True. The usual solution is to force-instantiate them against archetype classes. This is correct, but you not only need to make sure that they accept an archetype class, you also need to make sure that they reject all others. This often requires writing and instantiating a substantial amount of code for testing purposes, which often ends up as large or larger than the actual logic itself. I suppose you already know the pain of doing this, but for those who do not, it is like testing a function in python for all possible inputs. Can be done, but is a lot of work. In practice, most generic C++ code is not tested in this way due to how painful it is to do so. So what in Rust takes no work, takes a huge amount of effort in C++.