Skip to content

Trim posing converse problem to replace #6

@adamreichold

Description

@adamreichold

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions