Skip to content

implement replace_with and replacen_with for unstable feature replace_with - #147741

Open
alpaylan wants to merge 3 commits into
rust-lang:mainfrom
alpaylan:master
Open

implement replace_with and replacen_with for unstable feature replace_with#147741
alpaylan wants to merge 3 commits into
rust-lang:mainfrom
alpaylan:master

Conversation

@alpaylan

Copy link
Copy Markdown

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

rustbot commented Oct 15, 2025

Copy link
Copy Markdown
Collaborator

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 thread library/alloc/src/str.rs Outdated
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
Copy Markdown
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
Copy Markdown
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

Kivooeo commented Oct 16, 2025

Copy link
Copy Markdown
Member

Hm, that's weird because alloc is 2024 edition

edition = "2024"

@Kivooeo

Kivooeo commented Oct 16, 2025

Copy link
Copy Markdown
Member

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

Comment thread library/alloc/src/str.rs Outdated
from: P,
mut f: impl FnMut(&str) -> S,
) -> String {
// Fast path for replacing a single ASCII character with another.

Copy link
Copy Markdown
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
Copy Markdown
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
Comment thread library/alloc/src/str.rs
@SpriteOvO

Copy link
Copy Markdown
Member

Hi, ping from triage team. This PR has been inactive for a while. Are there any updates on this? I assume this PR should now be waiting for review?

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels May 3, 2026

@joboet joboet left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Just a few nits I wanted to pick...

View changes since this review

Comment thread library/alloc/src/str.rs Outdated
result
}

/// Replaces first N matches of a pattern with another string.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
/// Replaces first N matches of a pattern with another string.
/// Replaces the first N matches of a pattern with another string.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

This is also an issue that's similarly present in replacen (https://doc.rust-lang.org/src/alloc/str.rs.html#296)
I assume I should fix both.

Comment thread library/alloc/src/str.rs

/// Replaces first N matches of a pattern with another string.
///
/// `replacen` creates a new [`String`], and copies the data from this string slice into it.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This paragraph still needs updating.

Comment thread library/alloc/src/str.rs
Comment on lines +431 to +432
// Hope to reduce the times of re-allocation
let mut result = String::with_capacity(32);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This is a bit inconsistent, replace_with doesn't do this preallocation. I'd do it in replace_with, too.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

This is again replicating a pattern in replacen. I can do it of course but I had assumed there was a concrete reasoning behind it based on the usage pattern of replace vs replacen

@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 May 7, 2026
@rustbot

rustbot commented May 7, 2026

Copy link
Copy Markdown
Collaborator

Reminder, once the PR becomes ready for a review, use @rustbot ready.

@rustbot

This comment has been minimized.

@rustbot

rustbot commented Jun 1, 2026

Copy link
Copy Markdown
Collaborator

This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

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.

7 participants