Skip to content

Remove the return value of String::splice #44038

Closed
@dtolnay

Description

@dtolnay

As discussed in #32310, it is never necessary to look at the return value of String::splice. Applying the splice eagerly is always simpler and at least as efficient.

- fn splice<R>(&'a mut self, range: R, replace_with: &'b str) -> Splice<'a, 'b>;
+ fn splice<R>(&mut self, range: R, replace_with: &str);

With a return value:

  1. The String and the replacement str are in scope (start of 'a and 'b).
  2. Call splice, receive Splice struct.
  3. The String is exclusively borrowed and unusable.
  4. Process the replaced segment one char at a time.
  5. Drop the Splice struct.
  6. Drop the String and the replacement str (end of 'a and 'b, must happen after 5).

Without a return value;

  1. The String and the replacement str are in scope (start of 'a and 'b).
  2. Grab the soon replaced segment &s[range].
  3. The String is shared borrowed but otherwise usable.
  4. Process the soon replaced segment one char at a time or as an ordinary &str.
  5. Call splice which eagerly does the replace and returns ().
  6. Drop the String and the replacement str (end of 'a and 'b).

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-collectionsArea: `std::collections`C-enhancementCategory: An issue proposing an enhancement or a PR with one.T-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions