You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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 allocated String, which may be inefficient if our original haystack was already in a String, and we are intending to overwrite it with the newly created String. In such a case, one can manually find the matched range of the needle in the haystack, and then use String::replace_range to replace the needle with the replacement in-place.
fnreplace_first(haystack:&mutString,needle:&str,replacement:&str){let range = match haystack.match_indices(needle).next(){Some((start, match_str)) => start..start + match_str.len(),None => return,};
haystack.replace_range(range, replacement);}
Additionally, though this cannot be done with replacen, you can similarly replace the last instance of a pattern by using rmatch_indices instead of match_indices.
Motivating examples or use cases
// adapted from https://github.com/rust-lang/rust/blob/master/src/tools/clippy/clippy_lints/src/unit_types/unit_arg.rs#L169let call_snippet_with_replacements = args_snippets
.iter().fold(call_snippet.to_owned(), |acc, arg| acc.replacen(arg.as_ref(),"()",1));// each iteration makes a new allocation// could beletmut call_snippet_with_replacements = call_snippet.to_owned();for arg in args_snippets {
call_snippet_with_replacements.replace_first(arg.as_ref(),"()");}
// adapted from https://github.com/cross-rs/cross/blob/main/xtask/src/crosstool.rs#L339-341
contents = contents
.replacen("%CT_GCC_V%",&ct_gcc_v,1)// this makes a new allocation.replacen("%CT_GCC%",&ct_gcc,1);// this makes a new allocation// could instead be
contents.replace_first("%CT_GCC_V%",&ct_gcc_v);// these avoid making a new allocation if possible
contents.replace_first("%CT_GCC%",&ct_gcc);
// adapted from https://github.com/rust-lang/rust-clippy/blob/master/clippy_utils/src/sugg.rs#L869let sugg = format!("{}{end_snip}",self.suggestion_start);ifself.closure_arg_is_type_annotated_double_ref{
sugg.replacen('&',"",1)}else{
sugg
}// could beletmut sugg = format!("{}{end_snip}",self.suggestion_start);ifself.closure_arg_is_type_annotated_double_ref{
sugg.replace_first('&',"");}
sugg
Other places with something essentially equivalent to string = string.replacen(pattern, replacement, 1);
// in alloc::string::StringimplString{pubfnreplace_first<P:Pattern>(&mutself,pattern:P,replacement:&str);pubfnreplace_last<P:Pattern>(&mutself,pattern:P,replacement:&str)wherefor<'a>P::Searcher<'a>:ReverseSearcher<'a>;}
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 APIs str::(r)match_indices and String::replace_range).
These could be fn(self) -> Self instead of fn(&mut self). This would make it difficult to perform on a mutably borrowed String: *string = std::mem::take(string).replace_first(...); vs string.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 new String allocation.
The method names could include in_place or similar, to distinguish them from replace/replacen methods on str that are not in-place. There is already String::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 named replace_first_in_place, that would match nicely with a possible String::replace_in_place and/or String::replacen_in_place that do what str::replace/str::replacen do, but in-place.
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):
We think this problem seems worth solving, and the standard library might be the right place to solve it.
We think that this probably doesn't belong in the standard library.
Second, if there's a concrete solution:
We think this specific solution looks roughly right, approved, you or someone else should implement this. (Further review will still happen on the subsequent implementation PR.)
We're not sure this is the right solution, and the alternatives or other materials don't give us enough information to be sure about that. Here are some questions we have that aren't answered, or rough ideas about alternatives we'd want to see discussed.
The text was updated successfully, but these errors were encountered:
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: