3 ms·
In cases like this, it can be useful to have the generic function call a separate, private function which does the implementation and is _not_ generic. Generic
by alilleybrinker 6y ago
In cases like this, it can be useful to have the generic function call a separate, private function which does the implementation and is _not_ generic. Generic functions are monomorphized, meaning copies are generated for each concrete type they're called with. Separating them out in this way can reduce the size of the generated code after monomorphization.
- Arnavion 6y agoIn cases like this, the generic function cannot return an Iterator of &str that borrow from the input because the input is being consumed, so it's a bad suggestion. The input needs to be &str to be able to return borrows from it. If they had tried to complete their code and compile it, they would've noticed there's no way the function could return `code.as_ref()`.
- alilleybrinker 6y agoThat's true that in this case their plan of making the input generic wouldn't work, as it relies on the output being borrowed. The technique of reducing the size of monomorphized functions would be useful in other contexts, but not this one.