In-place String::replace_first
and String::replace_last
#506
Labels
String::replace_first
and String::replace_last
#506
Proposal
Problem statement
When replacing a substring a single time in a string, currently we can use
str::replacen(haystack, needle, replacement, 1)
. However, this returns a newly allocatedString
, which may be inefficient if our originalhaystack
was already in aString
, and we are intending to overwrite it with the newly createdString
. In such a case, one can manually find the matched range of the needle in the haystack, and then useString::replace_range
to replace the needle with the replacement in-place.Additionally, though this cannot be done with
replacen
, you can similarly replace the last instance of a pattern by usingrmatch_indices
instead ofmatch_indices
.Motivating examples or use cases
Other places with something essentially equivalent to
string = string.replacen(pattern, replacement, 1);
Solution sketch
Alternatives
Users could use the more general
str::replacen
if allocation is not a bottleneck, or if the needle and replacement are not the same length and copying the haystack to a new allocation is faster than shuffling data around in one allocation.Users could implement these manually in terms of existing
String
/str
APIs (the implementations in rust-lang/rust#134316 use only the existing safe, stable APIsstr::(r)match_indices
andString::replace_range
).These could be
fn(self) -> Self
instead offn(&mut self)
. This would make it difficult to perform on a mutably borrowedString
:*string = std::mem::take(string).replace_first(...);
vsstring.replace_first(...);
These could be
fn(&mut self) -> &mut Self
to allow chaining multiple calls, but this might be less clear that it does not return a newString
allocation.The method names could include
in_place
or similar, to distinguish them fromreplace
/replacen
methods onstr
that are not in-place. There is alreadyString::replace_range
though, that is in-place but does not explicitly indicate this in its name.@tgross35 mentioned on the implementation PR that it would make sense for there to also be an in-place
str::replace
alternative. If these are namedreplace_first_in_place
, that would match nicely with a possibleString::replace_in_place
and/orString::replacen_in_place
that do whatstr::replace
/str::replacen
do, but in-place.Links and related work
Implementation PR: rust-lang/rust#134316
What happens now?
This issue contains an API change proposal (or ACP) and is part of the libs-api team feature lifecycle. Once this issue is filed, the libs-api team will review open proposals as capability becomes available. Current response times do not have a clear estimate, but may be up to several months.
Possible responses
The libs team may respond in various different ways. First, the team will consider the problem (this doesn't require any concrete solution or alternatives to have been proposed):
Second, if there's a concrete solution:
The text was updated successfully, but these errors were encountered: