-
Notifications
You must be signed in to change notification settings - Fork 349
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Anti-Pattern: Intermediate collect()
ion
#8
Comments
|
Collecting early and then iterating over the vector allows one to avoid parameterizing the code that does the iterating over the iterator type, right? I would say that the most common anti-pattern I see in Rust is unnecessarily parameterized functions; e.g. |
The latter uses a fat pointer+vtable whereas the former does static dispatch, am I right? In that case, it depends on whether you agree to the runtime cost of dynamic dispatch (which is often negligible) or not (e.g. in a tight loop). |
I usually see people doing it the parameterized way when performance isn't an issue and/or when it isn't clear that dynamic dispatch is worse than the effects of code bloat that results from parameterization. |
There are other downsides for using trait objects rather than generics - in particular you have to care about object safety which is a fiddly topic. The preferred solution is |
For this, I can not wait :) |
My point is that Intermediate |
I sometimes see this in code from iterative-minded folks, they iterate over something, collecting the result in a
Vec
. Then they iterate over theVec
, producing yet anotherVec
.In most cases, the intermediate storage can be avoided. However, there are two exceptions:
start()
ing a number of threads one intends tojoin()
later, the intermediate storage is needed to allow the threads to run concurrently.The text was updated successfully, but these errors were encountered: