-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
str: add cut and cutn methods #49027
Conversation
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @bluss (or someone else) soon. If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes. Please see the contribution instructions for more information. |
This isn’t what I’d expect a string op named cut to do. |
I don't think there's any general expectation for rust methods to have the same behaviour as similarly named unix commands, and I think the name cut does make sense for this since it effectively cuts content out of the string according to the provided pattern. But I'm down w/ considering other names. I originally was gonna use remove but decided against it when I remembered String's remove method |
Is there any previous discussion on this? Seems too trivial to be in |
There's no general expectation for rust methods to match unix commands, but they usually either have some precedent or near-match in other ecosystems, or, conversely, conform to Rust's naming conventions for the sorts of operations that are unique to the language. Afaik 'cut' doesn't show up in many (any?) other languages' standard libraries, while the unix command is used widely in a similar enough context that I think it's reasonable to hold up as the prevailing prior art for the name in this particular case. |
IMO Also, a |
☔ The latest upstream changes (presumably #47813) made this pull request unmergeable. Please resolve the merge conflicts. |
@jfager Point taken. Maybe a name like purge, delete, snip, blank? fn cut<'a, P: Pattern<'a>>(&'a self, pattern: P) -> Cow<'a, str> {
let result = self.replace(pattern, "");
match result == self {
false => result.into(),
true => self.into()
}
} ? cut_in_place could be useful I guess and I could take a crack at it if cut and cutn get merged |
@cool-cool-sweat yes, but please don't actually write that code; it's less efficient because it still allocates unconditionally. As far as naming goes, I like |
Thank you for this PR @cool-cool-sweat! The triage team will periodically check in to make sure the PR is properly reviewed. Ping from triage @bluss! This PR needs your review. |
Gonna close this since there ain't much interest |
These methods just wrap replace/replacen and provide a more concise way to remove patterns from strs