5 ms·
I've been writing Rust professionally for a few years now and if there's one thing I've learned it's that if you ever write a function that takes a parameter of
by weavie 4y ago
I've been writing Rust professionally for a few years now and if there's one thing I've learned it's that if you ever write a function that takes a parameter of `impl Fn(&Vec<&'a str>) -> &'a str` you are going to be in for some pain. Just make it `impl Fn(&Vec<&str>) -> String`. It is highly unlikely that the extra allocation is ever going to be noticed in the performance.
Just because Rust pretty much forces you to be explicit about your allocations doesn't mean you have to avoid them at all costs.
- BaculumMeumEst 4y ago[flagged]
- bruce343434 4y agothanks for running this through chatgpt for me
- BaculumMeumEst 4y ago[flagged]
- jossclimb 4y agoA lot of programmers will look at '`impl Fn(&Vec<&'a str>) -> &'a str`' and think what the hell is that, alien hieroglyphics?
- nequo 4y agoLoosely speaking, it’s a type signature for a function that 1. takes an immutable reference to a vector of strings and 2. returns an immutable reference to a string, 3. with the constraint that the strings in the input vector must not be freed before the result is freed. It’s a reasonably concise way of saying all that.
- kazinator 4y agoI'm pretty sure I never want to have anything to do with any of these concepts, no matter the syntax.
- deleted 4y ago[deleted]
- satvikpendem 4y agoSame for APL, Perl, Lisp, Haskell and many others, or even C with multiple (double or triple) pointers and C++ with many pointer types. At the end of the day, you can't understand a language without, well, learning it.
- ModernMech 4y ago> It is highly unlikely that the extra allocation is ever going to be noticed in the performance. I had almost this exact scenario, and yes there is pain in writing it with explicit lifetimes. But I can't agree the performance improvement is negligible; maybe in isolation, but I saw about a 100x speed increase for my application when I switched away from Strings. For me it was because I was doing many of those extra String allocations in a loop, so it killed my performance.
- estebank 4y agoThis is not an uncommon pitfall when working with strings in all languages. In Java for example it is drilled into people to use StringBuilder instead of concatenating with + on String if you do it in a loop, precisely because of this exact issue.
- weavie 4y agoFor sure, optimise when necessary. Rust lets you do this.
- tjdetwiler 4y agoYou can still return the str reference; callers can easily do the copy if they need while allowing for zero-copy for simpler usages.
- estebank 4y agoOne day I need to get around to figuring out how to detect when people are going in circles with lifetime errors and have rustc open https://keepcalmandcallclone.website/ https://keepcalmandcallclone.website/ for them.