Skip to content
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

Trim posing converse problem to replace #6

Open
adamreichold opened this issue Apr 11, 2024 · 0 comments
Open

Trim posing converse problem to replace #6

adamreichold opened this issue Apr 11, 2024 · 0 comments

Comments

@adamreichold
Copy link

We often have to trim strings because some values contain superfluous whitespace while most values do not. To avoid reallocating a new string for the majority of the values, we currently use a little extension trait called TrimExt with works like

impl TrimExt for String {
    fn trim(self) -> Self {
        let val = self.as_str().trim();

        if val.len() != self.len() {
            val.to_owned()
        } else {
            self
        }
    }
}

i.e. it keeps to original value if the trimming is actually a no-op.

So this does not really fit into the CowUtils trait as far I can see, but I think one could say that it does fit with this crate's aim of avoiding allocations when no modifications are necessary.

Would you be interested in adding a new trait to capture this problem which I think is sort of a converse to the replace problem, i.e. we start with an owned value and want to keep it if possible.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant