diff options
| author | Trevor Gross <t.gross35@gmail.com> | 2025-06-20 13:35:58 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-06-20 13:35:58 -0400 |
| commit | 851fbcb0928e55296212bb060cd82cf3350087c2 (patch) | |
| tree | 608f300fac2b481b69d8326155fd849260a00331 /compiler/rustc_codegen_llvm/src | |
| parent | 9c4ff566babe632af5e30281a822d1ae9972873b (diff) | |
| parent | 32cb8f1537a3c0948e01cb90c850d9f60e3139ed (diff) | |
| download | rust-851fbcb0928e55296212bb060cd82cf3350087c2.tar.gz rust-851fbcb0928e55296212bb060cd82cf3350087c2.zip | |
Rollup merge of #142331 - deven:trim_prefix_suffix, r=Amanieu
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/rust#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
Diffstat (limited to 'compiler/rustc_codegen_llvm/src')
0 files changed, 0 insertions, 0 deletions
