3 ms·
strings.Cut is otherwise known in Rust as str::split_once. It's something I've always missed when writing Go. Nice to see it added to the stdlib
by howdydoo 5y ago
strings.Cut is otherwise known in Rust as str::split_once. It's something I've always missed when writing Go. Nice to see it added to the stdlib
- tialaramex 5y agoYes, I use this much more often than the more powerful general split feature, it feels very "right-sized". split_once() and similar split features in Rust are interesting because Rust doesn't have overloading, yet you can split on a string, or a character. This relies on a Trait, Pattern, that is Nightly, so you can't Implement it yourself in stable Rust today, but eventually this factors out the commonality which is cleverer than overloading (because it applies everywhere automatically)
- IshKebab 5y agoYou can implement it yourself. You just have to use your own trait.
- tialaramex 5y agoI stared at this a few times and I don't get it. The point is you can't implement Pattern, because it's a nightly feature. If you just make your own, that isn't Pattern and provides none of the benefits. Yes, you can implement your own trait, and provide blanket implementations for it, but unlike Pattern yours is not part of the standard. The nice thing about Pattern is that if Rust added say split_exactly_six_times() to the standard library that would take separator: Pattern too and so things which implement Pattern qualify, I can't see a way to get that benefit for my own traits.
- awinter-py 5y agoagree -- ultra useful alternative to splitting would be neat to imagine a language where this was an optimizer feature rather than a decision the programmer made. many functions that return an array are ultimately using just one, or a few elements of it. the compiler can make a lot of decisions in the caller about how to reduce allocation or short-circuit array processing. I think, but am not sure, this is what the experimental parsing language 'wuffs' is about https://github.com/google/wuffs https://github.com/google/wuffs -- the language itself is aware of array lengths as a first-class citizen and can make decisions accordingly
- layer8 5y agoThe optimization could be done by lazy evaluation, like a generator.
- tialaramex 5y agoWUFFS is, as it's name suggests, primarily about safety, but it gets performance advantages that fall out of safety (e.g it needs no runtime bounds or overflow/underflow checks because it proved these things can't happen at compile time)
- layer8 5y agoSplit_first would have been a better name though (and split_last instead of rsplit_once).