Skip to content

Conversation

@alpaylan
Copy link

This is the PR for the tracking issue #147731 for implementation of replace_with.

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Oct 15, 2025
@rustbot rustbot added the T-libs Relevant to the library team, which will review and decide on the PR/issue. label Oct 15, 2025
@rustbot
Copy link
Collaborator

rustbot commented Oct 15, 2025

r? @joboet

rustbot has assigned @joboet.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Comment on lines 346 to 356
if let Some(from_byte) = match from.as_utf8_pattern() {
Some(Utf8Pattern::StringPattern([from_byte])) => Some(*from_byte),
Some(Utf8Pattern::CharPattern(c)) => c.as_ascii().map(|ascii_char| ascii_char.to_u8()),
_ => None,
} {
if let Some(sref) = from_byte.as_ascii().map(|ascii_char| f(ascii_char.as_str())) {
if let [to_byte] = sref.as_ref().as_bytes() {
return unsafe { replace_ascii(self.as_bytes(), from_byte, *to_byte) };
}
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be possible to use let chains here?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated with an if-let chain, did you have something else in mind or is this fine? (f70b5e1)

@rust-log-analyzer

This comment has been minimized.

@Kivooeo
Copy link
Member

Kivooeo commented Oct 16, 2025

Hm, that's weird because alloc is 2024 edition

edition = "2024"

@Kivooeo
Copy link
Member

Kivooeo commented Oct 16, 2025

It does compile with edition 2021 anyway tho

Command { cmd: ... "/checkout/obj/build/x86_64-unknown-linux-gnu/stage2/bin/rustc" "-L" "/checkout/obj/build/x86_64-unknown-linux-gnu/test/run-make/alloc-no-oom-handling/rmake_out" 


"--edition" "2021" 


"-Dwarnings" ... }, already_executed: true }

Well, I guess that's fine to revert this then :)

If this ever will get 2024 edition I will cleanup this accordingly

from: P,
mut f: impl FnMut(&str) -> S,
) -> String {
// Fast path for replacing a single ASCII character with another.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

f might not be side-effect-free, so this optimisation is incorrect.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, that is correct. I'm assuming I should just remove it, or are there any other potential optimization?

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Oct 21, 2025
#[must_use = "this returns the replaced string as a new allocation, \
without modifying the original"]
#[unstable(feature = "replace_with", issue = "147731")]
pub fn replacen_with<P: Pattern, S: AsRef<str>>(
Copy link
Contributor

@lygstate lygstate Dec 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

weired name, easy to confuse with replace_with

we already have sothing alike repeat_n for iterator.

better with name replace_n_with to distinguish it.
saved a single character _ does not save much typing be hard to reading

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The name comes from an existing function, str::replacen, so the addition is the _with. That makes me think changing of replacen to replace_n should be a separate issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-libs Relevant to the library team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants