-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Add trim_prefix
and trim_suffix
methods for both slice
and str
types.
#142331
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
deven
wants to merge
1
commit into
rust-lang:master
Choose a base branch
from
deven:trim_prefix_suffix
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+160
−0
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This comment has been minimized.
This comment has been minimized.
Implements `trim_prefix` and `trim_suffix` methods for both `slice` and `str` types which remove at most one occurrence of a prefix/suffix while always returning a string/slice (rather than Option), enabling easy method chaining.
ecbcaa2
to
32cb8f1
Compare
r? @Amanieu |
3 tasks
r? @joboet |
@bors r+ |
Kobzol
added a commit
to Kobzol/rust
that referenced
this pull request
Jun 19, 2025
Add `trim_prefix` and `trim_suffix` methods for both `slice` and `str` types. Implements `trim_prefix` and `trim_suffix` methods for both `slice` and `str` types, which remove at most one occurrence of a prefix/suffix while always returning a string/slice (rather than Option), enabling easy method chaining. ## Tracking issue rust-lang#142312 ## API ```rust impl str { pub fn trim_prefix<P: Pattern>(&self, prefix: P) -> &str; pub fn trim_suffix<P: Pattern>(&self, suffix: P) -> &str where for<'a> P::Searcher<'a>: ReverseSearcher<'a>; } impl<T> [T] { pub fn trim_prefix<P: SlicePattern<Item = T> + ?Sized>(&self, prefix: &P) -> &[T] where T: PartialEq; pub fn trim_suffix<P: SlicePattern<Item = T> + ?Sized>(&self, suffix: &P) -> &[T] where T: PartialEq; } ``` ## Examples ```rust // Method chaining assert_eq!(" <https://example.com/> ".trim().trim_prefix('<').trim_suffix('>').trim(), "https://example.com/"); // Slices let v = &[10, 40, 30]; assert_eq!(v.trim_prefix(&[10]), &[40, 30][..]); ``` ## ACP Originally proposed in rust-lang/libs-team#597
bors
added a commit
that referenced
this pull request
Jun 19, 2025
Rollup of 6 pull requests Successful merges: - #141990 (Implement send_signal for unix child processes) - #142331 (Add `trim_prefix` and `trim_suffix` methods for both `slice` and `str` types.) - #142502 (rustdoc_json: improve handling of generic args) - #142629 (Add config builder for bootstrap tests) - #142687 (Reduce uses of `hir_crate`.) - #142714 (add comment to `src/bootstrap/build.rs`) r? `@ghost` `@rustbot` modify labels: rollup
jhpratt
added a commit
to jhpratt/rust
that referenced
this pull request
Jun 19, 2025
Add `trim_prefix` and `trim_suffix` methods for both `slice` and `str` types. Implements `trim_prefix` and `trim_suffix` methods for both `slice` and `str` types, which remove at most one occurrence of a prefix/suffix while always returning a string/slice (rather than Option), enabling easy method chaining. ## Tracking issue rust-lang#142312 ## API ```rust impl str { pub fn trim_prefix<P: Pattern>(&self, prefix: P) -> &str; pub fn trim_suffix<P: Pattern>(&self, suffix: P) -> &str where for<'a> P::Searcher<'a>: ReverseSearcher<'a>; } impl<T> [T] { pub fn trim_prefix<P: SlicePattern<Item = T> + ?Sized>(&self, prefix: &P) -> &[T] where T: PartialEq; pub fn trim_suffix<P: SlicePattern<Item = T> + ?Sized>(&self, suffix: &P) -> &[T] where T: PartialEq; } ``` ## Examples ```rust // Method chaining assert_eq!(" <https://example.com/> ".trim().trim_prefix('<').trim_suffix('>').trim(), "https://example.com/"); // Slices let v = &[10, 40, 30]; assert_eq!(v.trim_prefix(&[10]), &[40, 30][..]); ``` ## ACP Originally proposed in rust-lang/libs-team#597
bors
added a commit
that referenced
this pull request
Jun 19, 2025
Rollup of 10 pull requests Successful merges: - #138291 (rewrite `optimize` attribute to use new attribute parsing infrastructure) - #141990 (Implement send_signal for unix child processes) - #142331 (Add `trim_prefix` and `trim_suffix` methods for both `slice` and `str` types.) - #142478 (install docs for each target in different directory) - #142571 (Reason about borrowed classes in CopyProp.) - #142668 (vec_deque/fmt/vec tests: remove static mut) - #142687 (Reduce uses of `hir_crate`.) - #142690 (expand: Remove some unnecessary generic parameters) - #142699 (Update books) - #142714 (add comment to `src/bootstrap/build.rs`) Failed merges: - #142502 (rustdoc_json: improve handling of generic args) r? `@ghost` `@rustbot` modify labels: rollup
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
S-waiting-on-bors
Status: Waiting on bors to run and complete tests. Bors will change the label on completion.
T-libs
Relevant to the library team, which will review and decide on the PR/issue.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Implements
trim_prefix
andtrim_suffix
methods for bothslice
andstr
types, which remove at most one occurrence of a prefix/suffix while always returning a string/slice (rather than Option), enabling easy method chaining.Tracking issue
#142312
API
Examples
ACP
Originally proposed in rust-lang/libs-team#597