Skip to main content

core/str/
mod.rs

1//! String manipulation.
2//!
3//! For more details, see the [`std::str`] module.
4//!
5//! [`std::str`]: ../../std/str/index.html
6
7#![stable(feature = "rust1", since = "1.0.0")]
8
9mod converts;
10mod count;
11mod error;
12mod iter;
13mod traits;
14mod validations;
15
16use self::pattern::{DoubleEndedSearcher, Pattern, ReverseSearcher, Searcher};
17use crate::char::{self, EscapeDebugExtArgs};
18use crate::hint::assert_unchecked;
19use crate::range::Range;
20use crate::slice::{self, SliceIndex};
21use crate::ub_checks::assert_unsafe_precondition;
22use crate::{ascii, mem};
23
24pub mod pattern;
25
26mod lossy;
27#[unstable(feature = "str_from_raw_parts", issue = "119206")]
28pub use converts::{from_raw_parts, from_raw_parts_mut};
29#[stable(feature = "rust1", since = "1.0.0")]
30pub use converts::{from_utf8, from_utf8_unchecked};
31#[stable(feature = "str_mut_extras", since = "1.20.0")]
32pub use converts::{from_utf8_mut, from_utf8_unchecked_mut};
33#[stable(feature = "rust1", since = "1.0.0")]
34pub use error::{ParseBoolError, Utf8Error};
35#[stable(feature = "encode_utf16", since = "1.8.0")]
36pub use iter::EncodeUtf16;
37#[stable(feature = "rust1", since = "1.0.0")]
38#[allow(deprecated)]
39pub use iter::LinesAny;
40#[stable(feature = "split_ascii_whitespace", since = "1.34.0")]
41pub use iter::SplitAsciiWhitespace;
42#[stable(feature = "split_inclusive", since = "1.51.0")]
43pub use iter::SplitInclusive;
44#[stable(feature = "rust1", since = "1.0.0")]
45pub use iter::{Bytes, CharIndices, Chars, Lines, SplitWhitespace};
46#[stable(feature = "str_escape", since = "1.34.0")]
47pub use iter::{EscapeDebug, EscapeDefault, EscapeUnicode};
48#[stable(feature = "str_match_indices", since = "1.5.0")]
49pub use iter::{MatchIndices, RMatchIndices};
50use iter::{MatchIndicesInternal, MatchesInternal, SplitInternal, SplitNInternal};
51#[stable(feature = "str_matches", since = "1.2.0")]
52pub use iter::{Matches, RMatches};
53#[stable(feature = "rust1", since = "1.0.0")]
54pub use iter::{RSplit, RSplitTerminator, Split, SplitTerminator};
55#[stable(feature = "rust1", since = "1.0.0")]
56pub use iter::{RSplitN, SplitN};
57#[stable(feature = "utf8_chunks", since = "1.79.0")]
58pub use lossy::{Utf8Chunk, Utf8Chunks};
59#[stable(feature = "rust1", since = "1.0.0")]
60pub use traits::FromStr;
61#[unstable(feature = "str_internals", issue = "none")]
62pub use validations::{next_code_point, utf8_char_width};
63
64#[inline(never)]
65#[cold]
66#[track_caller]
67#[rustc_allow_const_fn_unstable(const_eval_select)]
68#[cfg(not(panic = "immediate-abort"))]
69const fn slice_error_fail(s: &str, begin: usize, end: usize) -> ! {
70    crate::intrinsics::const_eval_select((s, begin, end), slice_error_fail_ct, slice_error_fail_rt)
71}
72
73#[cfg(panic = "immediate-abort")]
74const fn slice_error_fail(s: &str, begin: usize, end: usize) -> ! {
75    slice_error_fail_ct(s, begin, end)
76}
77
78#[track_caller]
79const fn slice_error_fail_ct(_: &str, _: usize, _: usize) -> ! {
80    panic!("failed to slice string");
81}
82
83#[track_caller]
84fn slice_error_fail_rt(s: &str, begin: usize, end: usize) -> ! {
85    let len = s.len();
86
87    // 1. begin is OOB.
88    if begin > len {
89        panic!("start byte index {begin} is out of bounds for string of length {len}");
90    }
91
92    // 2. end is OOB.
93    if end > len {
94        panic!("end byte index {end} is out of bounds for string of length {len}");
95    }
96
97    // 3. range is backwards.
98    if begin > end {
99        panic!("byte range starts at {begin} but ends at {end}");
100    }
101
102    // 4. begin is inside a character.
103    if !s.is_char_boundary(begin) {
104        let floor = s.floor_char_boundary(begin);
105        let ceil = s.ceil_char_boundary(begin);
106        let range = floor..ceil;
107        let ch = s[floor..ceil].chars().next().unwrap();
108        panic!(
109            "start byte index {begin} is not a char boundary; it is inside {ch:?} (bytes {range:?} of string)"
110        )
111    }
112
113    // 5. end is inside a character.
114    if !s.is_char_boundary(end) {
115        let floor = s.floor_char_boundary(end);
116        let ceil = s.ceil_char_boundary(end);
117        let range = floor..ceil;
118        let ch = s[floor..ceil].chars().next().unwrap();
119        panic!(
120            "end byte index {end} is not a char boundary; it is inside {ch:?} (bytes {range:?} of string)"
121        )
122    }
123
124    // 6. end is OOB and range is inclusive (end == len).
125    // This test cannot be combined with 2. above because for cases like
126    // `"abcαβγ"[4..9]` the error is that 4 is inside 'α', not that 9 is OOB.
127    debug_assert_eq!(end, len);
128    panic!("end byte index {end} is out of bounds for string of length {len}");
129}
130
131impl str {
132    /// Returns the length of `self`.
133    ///
134    /// This length is in bytes, not [`char`]s or graphemes. In other words,
135    /// it might not be what a human considers the length of the string.
136    ///
137    /// [`char`]: prim@char
138    ///
139    /// # Examples
140    ///
141    /// ```
142    /// let len = "foo".len();
143    /// assert_eq!(3, len);
144    ///
145    /// assert_eq!("ƒoo".len(), 4); // fancy f!
146    /// assert_eq!("ƒoo".chars().count(), 3);
147    /// ```
148    #[stable(feature = "rust1", since = "1.0.0")]
149    #[rustc_const_stable(feature = "const_str_len", since = "1.39.0")]
150    #[rustc_diagnostic_item = "str_len"]
151    #[rustc_no_implicit_autorefs]
152    #[must_use]
153    #[inline]
154    pub const fn len(&self) -> usize {
155        self.as_bytes().len()
156    }
157
158    /// Returns `true` if `self` has a length of zero bytes.
159    ///
160    /// # Examples
161    ///
162    /// ```
163    /// let s = "";
164    /// assert!(s.is_empty());
165    ///
166    /// let s = "not empty";
167    /// assert!(!s.is_empty());
168    /// ```
169    #[stable(feature = "rust1", since = "1.0.0")]
170    #[rustc_const_stable(feature = "const_str_is_empty", since = "1.39.0")]
171    #[rustc_no_implicit_autorefs]
172    #[must_use]
173    #[inline]
174    pub const fn is_empty(&self) -> bool {
175        self.len() == 0
176    }
177
178    /// Converts a slice of bytes to a string slice.
179    ///
180    /// A string slice ([`&str`]) is made of bytes ([`u8`]), and a byte slice
181    /// ([`&[u8]`][byteslice]) is made of bytes, so this function converts between
182    /// the two. Not all byte slices are valid string slices, however: [`&str`] requires
183    /// that it is valid UTF-8. `from_utf8()` checks to ensure that the bytes are valid
184    /// UTF-8, and then does the conversion.
185    ///
186    /// [`&str`]: str
187    /// [byteslice]: prim@slice
188    ///
189    /// If you are sure that the byte slice is valid UTF-8, and you don't want to
190    /// incur the overhead of the validity check, there is an unsafe version of
191    /// this function, [`from_utf8_unchecked`], which has the same
192    /// behavior but skips the check.
193    ///
194    /// If you need a `String` instead of a `&str`, consider
195    /// [`String::from_utf8`][string].
196    ///
197    /// [string]: ../std/string/struct.String.html#method.from_utf8
198    ///
199    /// Because you can stack-allocate a `[u8; N]`, and you can take a
200    /// [`&[u8]`][byteslice] of it, this function is one way to have a
201    /// stack-allocated string. There is an example of this in the
202    /// examples section below.
203    ///
204    /// [byteslice]: slice
205    ///
206    /// # Errors
207    ///
208    /// Returns `Err` if the slice is not UTF-8 with a description as to why the
209    /// provided slice is not UTF-8.
210    ///
211    /// # Examples
212    ///
213    /// Basic usage:
214    ///
215    /// ```
216    /// // some bytes, in a vector
217    /// let sparkle_heart = vec![240, 159, 146, 150];
218    ///
219    /// // We can use the ? (try) operator to check if the bytes are valid
220    /// let sparkle_heart = str::from_utf8(&sparkle_heart)?;
221    ///
222    /// assert_eq!("💖", sparkle_heart);
223    /// # Ok::<_, std::str::Utf8Error>(())
224    /// ```
225    ///
226    /// Incorrect bytes:
227    ///
228    /// ```
229    /// // some invalid bytes, in a vector
230    /// let sparkle_heart = vec![0, 159, 146, 150];
231    ///
232    /// assert!(str::from_utf8(&sparkle_heart).is_err());
233    /// ```
234    ///
235    /// See the docs for [`Utf8Error`] for more details on the kinds of
236    /// errors that can be returned.
237    ///
238    /// A "stack allocated string":
239    ///
240    /// ```
241    /// // some bytes, in a stack-allocated array
242    /// let sparkle_heart = [240, 159, 146, 150];
243    ///
244    /// // We know these bytes are valid, so just use `unwrap()`.
245    /// let sparkle_heart: &str = str::from_utf8(&sparkle_heart).unwrap();
246    ///
247    /// assert_eq!("💖", sparkle_heart);
248    /// ```
249    #[stable(feature = "inherent_str_constructors", since = "1.87.0")]
250    #[rustc_const_stable(feature = "inherent_str_constructors", since = "1.87.0")]
251    #[rustc_diagnostic_item = "str_inherent_from_utf8"]
252    pub const fn from_utf8(v: &[u8]) -> Result<&str, Utf8Error> {
253        converts::from_utf8(v)
254    }
255
256    /// Converts a mutable slice of bytes to a mutable string slice.
257    ///
258    /// # Examples
259    ///
260    /// Basic usage:
261    ///
262    /// ```
263    /// // "Hello, Rust!" as a mutable vector
264    /// let mut hellorust = vec![72, 101, 108, 108, 111, 44, 32, 82, 117, 115, 116, 33];
265    ///
266    /// // As we know these bytes are valid, we can use `unwrap()`
267    /// let outstr = str::from_utf8_mut(&mut hellorust).unwrap();
268    ///
269    /// assert_eq!("Hello, Rust!", outstr);
270    /// ```
271    ///
272    /// Incorrect bytes:
273    ///
274    /// ```
275    /// // Some invalid bytes in a mutable vector
276    /// let mut invalid = vec![128, 223];
277    ///
278    /// assert!(str::from_utf8_mut(&mut invalid).is_err());
279    /// ```
280    /// See the docs for [`Utf8Error`] for more details on the kinds of
281    /// errors that can be returned.
282    #[stable(feature = "inherent_str_constructors", since = "1.87.0")]
283    #[rustc_const_stable(feature = "const_str_from_utf8", since = "1.87.0")]
284    #[rustc_diagnostic_item = "str_inherent_from_utf8_mut"]
285    pub const fn from_utf8_mut(v: &mut [u8]) -> Result<&mut str, Utf8Error> {
286        converts::from_utf8_mut(v)
287    }
288
289    /// Converts a slice of bytes to a string slice without checking
290    /// that the string contains valid UTF-8.
291    ///
292    /// See the safe version, [`from_utf8`], for more information.
293    ///
294    /// # Safety
295    ///
296    /// The bytes passed in must be valid UTF-8.
297    ///
298    /// # Examples
299    ///
300    /// Basic usage:
301    ///
302    /// ```
303    /// // some bytes, in a vector
304    /// let sparkle_heart = vec![240, 159, 146, 150];
305    ///
306    /// let sparkle_heart = unsafe {
307    ///     str::from_utf8_unchecked(&sparkle_heart)
308    /// };
309    ///
310    /// assert_eq!("💖", sparkle_heart);
311    /// ```
312    #[inline]
313    #[must_use]
314    #[stable(feature = "inherent_str_constructors", since = "1.87.0")]
315    #[rustc_const_stable(feature = "inherent_str_constructors", since = "1.87.0")]
316    #[rustc_diagnostic_item = "str_inherent_from_utf8_unchecked"]
317    pub const unsafe fn from_utf8_unchecked(v: &[u8]) -> &str {
318        // SAFETY: converts::from_utf8_unchecked has the same safety requirements as this function.
319        unsafe { converts::from_utf8_unchecked(v) }
320    }
321
322    /// Converts a slice of bytes to a string slice without checking
323    /// that the string contains valid UTF-8; mutable version.
324    ///
325    /// See the immutable version, [`from_utf8_unchecked()`] for documentation and safety requirements.
326    ///
327    /// # Examples
328    ///
329    /// Basic usage:
330    ///
331    /// ```
332    /// let mut heart = vec![240, 159, 146, 150];
333    /// let heart = unsafe { str::from_utf8_unchecked_mut(&mut heart) };
334    ///
335    /// assert_eq!("💖", heart);
336    /// ```
337    #[inline]
338    #[must_use]
339    #[stable(feature = "inherent_str_constructors", since = "1.87.0")]
340    #[rustc_const_stable(feature = "inherent_str_constructors", since = "1.87.0")]
341    #[rustc_diagnostic_item = "str_inherent_from_utf8_unchecked_mut"]
342    pub const unsafe fn from_utf8_unchecked_mut(v: &mut [u8]) -> &mut str {
343        // SAFETY: converts::from_utf8_unchecked_mut has the same safety requirements as this function.
344        unsafe { converts::from_utf8_unchecked_mut(v) }
345    }
346
347    /// Checks that `index`-th byte is the first byte in a UTF-8 code point
348    /// sequence or the end of the string.
349    ///
350    /// The start and end of the string (when `index == self.len()`) are
351    /// considered to be boundaries.
352    ///
353    /// Returns `false` if `index` is greater than `self.len()`.
354    ///
355    /// # Examples
356    ///
357    /// ```
358    /// let s = "Löwe 老虎 Léopard";
359    /// assert!(s.is_char_boundary(0));
360    /// // start of `老`
361    /// assert!(s.is_char_boundary(6));
362    /// assert!(s.is_char_boundary(s.len()));
363    ///
364    /// // second byte of `ö`
365    /// assert!(!s.is_char_boundary(2));
366    ///
367    /// // third byte of `老`
368    /// assert!(!s.is_char_boundary(8));
369    /// ```
370    #[must_use]
371    #[stable(feature = "is_char_boundary", since = "1.9.0")]
372    #[rustc_const_stable(feature = "const_is_char_boundary", since = "1.86.0")]
373    #[inline]
374    pub const fn is_char_boundary(&self, index: usize) -> bool {
375        // 0 is always ok.
376        // Test for 0 explicitly so that it can optimize out the check
377        // easily and skip reading string data for that case.
378        // Note that optimizing `self.get(..index)` relies on this.
379        if index == 0 {
380            return true;
381        }
382
383        if index >= self.len() {
384            // For `true` we have two options:
385            //
386            // - index == self.len()
387            //   Empty strings are valid, so return true
388            // - index > self.len()
389            //   In this case return false
390            //
391            // The check is placed exactly here, because it improves generated
392            // code on higher opt-levels. See PR #84751 for more details.
393            index == self.len()
394        } else {
395            self.as_bytes()[index].is_utf8_char_boundary()
396        }
397    }
398
399    /// Finds the closest `x` not exceeding `index` where [`is_char_boundary(x)`] is `true`.
400    ///
401    /// This method can help you truncate a string so that it's still valid UTF-8, but doesn't
402    /// exceed a given number of bytes. Note that this is done purely at the character level
403    /// and can still visually split graphemes, even though the underlying characters aren't
404    /// split. For example, the emoji 🧑‍🔬 (scientist) could be split so that the string only
405    /// includes 🧑 (person) instead.
406    ///
407    /// [`is_char_boundary(x)`]: Self::is_char_boundary
408    ///
409    /// # Examples
410    ///
411    /// ```
412    /// let s = "❤️🧡💛💚💙💜";
413    /// assert_eq!(s.len(), 26);
414    /// assert!(!s.is_char_boundary(13));
415    ///
416    /// let closest = s.floor_char_boundary(13);
417    /// assert_eq!(closest, 10);
418    /// assert_eq!(&s[..closest], "❤️🧡");
419    /// ```
420    #[stable(feature = "round_char_boundary", since = "1.91.0")]
421    #[rustc_const_stable(feature = "round_char_boundary", since = "1.91.0")]
422    #[inline]
423    pub const fn floor_char_boundary(&self, index: usize) -> usize {
424        if index >= self.len() {
425            return self.len();
426        }
427        if self.as_bytes()[index].is_utf8_char_boundary() {
428            return index;
429        }
430        // Unlike `ceil_char_boundary`, the loop is unrolled manually to prevent the compiler from
431        // generating excessive unrolled loop bodies when `index` is statically known.
432
433        // The first byte of `&str` must always be a char boundary, so we can assume `i > 0` below
434        // for any `i` where `self.as_bytes()[i]` is not a char boundary.
435        debug_assert!(self.as_bytes()[0].is_utf8_char_boundary());
436
437        // SAFETY: `self.as_bytes()[0]` is always a char boundary with valid `&str`
438        unsafe { assert_unchecked(index >= 1) };
439        if self.as_bytes()[index - 1].is_utf8_char_boundary() {
440            return index - 1;
441        }
442
443        // SAFETY: `self.as_bytes()[0]` is always a char boundary with valid `&str`
444        unsafe { assert_unchecked(index >= 2) };
445        if self.as_bytes()[index - 2].is_utf8_char_boundary() {
446            return index - 2;
447        }
448
449        // `self.as_bytes()[0]` is always a char boundary with valid `&str`
450        debug_assert!(index >= 3);
451        // The character boundary will be within four bytes of the index
452        debug_assert!(self.as_bytes()[index - 3].is_utf8_char_boundary());
453        index - 3
454    }
455
456    /// Finds the closest `x` not below `index` where [`is_char_boundary(x)`] is `true`.
457    ///
458    /// If `index` is greater than the length of the string, this returns the length of the string.
459    ///
460    /// This method is the natural complement to [`floor_char_boundary`]. See that method
461    /// for more details.
462    ///
463    /// [`floor_char_boundary`]: str::floor_char_boundary
464    /// [`is_char_boundary(x)`]: Self::is_char_boundary
465    ///
466    /// # Examples
467    ///
468    /// ```
469    /// let s = "❤️🧡💛💚💙💜";
470    /// assert_eq!(s.len(), 26);
471    /// assert!(!s.is_char_boundary(13));
472    ///
473    /// let closest = s.ceil_char_boundary(13);
474    /// assert_eq!(closest, 14);
475    /// assert_eq!(&s[..closest], "❤️🧡💛");
476    /// ```
477    #[stable(feature = "round_char_boundary", since = "1.91.0")]
478    #[rustc_const_stable(feature = "round_char_boundary", since = "1.91.0")]
479    #[inline]
480    pub const fn ceil_char_boundary(&self, index: usize) -> usize {
481        if index >= self.len() {
482            self.len()
483        } else {
484            let mut i = index;
485            while !self.as_bytes()[i].is_utf8_char_boundary() {
486                i += 1;
487                if i >= self.len() {
488                    break;
489                }
490            }
491
492            // The character boundary will be within four bytes of the index
493            debug_assert!(i <= index + 3);
494
495            i
496        }
497    }
498
499    /// Converts a string slice to a byte slice. To convert the byte slice back
500    /// into a string slice, use the [`from_utf8`] function.
501    ///
502    /// # Examples
503    ///
504    /// ```
505    /// let bytes = "bors".as_bytes();
506    /// assert_eq!(b"bors", bytes);
507    /// ```
508    #[stable(feature = "rust1", since = "1.0.0")]
509    #[rustc_const_stable(feature = "str_as_bytes", since = "1.39.0")]
510    #[must_use]
511    #[inline(always)]
512    #[allow(unused_attributes)]
513    pub const fn as_bytes(&self) -> &[u8] {
514        // SAFETY: const sound because we transmute two types with the same layout
515        unsafe { mem::transmute(self) }
516    }
517
518    /// Converts a mutable string slice to a mutable byte slice.
519    ///
520    /// # Safety
521    ///
522    /// The caller must ensure that the content of the slice is valid UTF-8
523    /// before the borrow ends and the underlying `str` is used.
524    ///
525    /// Use of a `str` whose contents are not valid UTF-8 is undefined behavior.
526    ///
527    /// # Examples
528    ///
529    /// Basic usage:
530    ///
531    /// ```
532    /// let mut s = String::from("Hello");
533    /// let bytes = unsafe { s.as_bytes_mut() };
534    ///
535    /// assert_eq!(b"Hello", bytes);
536    /// ```
537    ///
538    /// Mutability:
539    ///
540    /// ```
541    /// let mut s = String::from("🗻∈🌏");
542    ///
543    /// unsafe {
544    ///     let bytes = s.as_bytes_mut();
545    ///
546    ///     bytes[0] = 0xF0;
547    ///     bytes[1] = 0x9F;
548    ///     bytes[2] = 0x8D;
549    ///     bytes[3] = 0x94;
550    /// }
551    ///
552    /// assert_eq!("🍔∈🌏", s);
553    /// ```
554    #[stable(feature = "str_mut_extras", since = "1.20.0")]
555    #[rustc_const_stable(feature = "const_str_as_mut", since = "1.83.0")]
556    #[must_use]
557    #[inline(always)]
558    pub const unsafe fn as_bytes_mut(&mut self) -> &mut [u8] {
559        // SAFETY: the cast from `&str` to `&[u8]` is safe since `str`
560        // has the same layout as `&[u8]` (only std can make this guarantee).
561        // The pointer dereference is safe since it comes from a mutable reference which
562        // is guaranteed to be valid for writes.
563        unsafe { &mut *(self as *mut str as *mut [u8]) }
564    }
565
566    /// Converts a string slice to a raw pointer.
567    ///
568    /// As string slices are a slice of bytes, the raw pointer points to a
569    /// [`u8`]. This pointer will be pointing to the first byte of the string
570    /// slice.
571    ///
572    /// The caller must ensure that the returned pointer is never written to.
573    /// If you need to mutate the contents of the string slice, use [`as_mut_ptr`].
574    ///
575    /// [`as_mut_ptr`]: str::as_mut_ptr
576    ///
577    /// # Examples
578    ///
579    /// ```
580    /// let s = "Hello";
581    /// let ptr = s.as_ptr();
582    /// ```
583    #[stable(feature = "rust1", since = "1.0.0")]
584    #[rustc_const_stable(feature = "rustc_str_as_ptr", since = "1.32.0")]
585    #[rustc_never_returns_null_ptr]
586    #[rustc_as_ptr]
587    #[must_use]
588    #[inline(always)]
589    pub const fn as_ptr(&self) -> *const u8 {
590        self as *const str as *const u8
591    }
592
593    /// Converts a mutable string slice to a raw pointer.
594    ///
595    /// As string slices are a slice of bytes, the raw pointer points to a
596    /// [`u8`]. This pointer will be pointing to the first byte of the string
597    /// slice.
598    ///
599    /// It is your responsibility to make sure that the string slice only gets
600    /// modified in a way that it remains valid UTF-8.
601    #[stable(feature = "str_as_mut_ptr", since = "1.36.0")]
602    #[rustc_const_stable(feature = "const_str_as_mut", since = "1.83.0")]
603    #[rustc_never_returns_null_ptr]
604    #[rustc_as_ptr]
605    #[must_use]
606    #[inline(always)]
607    #[rustc_no_writable]
608    pub const fn as_mut_ptr(&mut self) -> *mut u8 {
609        self as *mut str as *mut u8
610    }
611
612    /// Returns a subslice of `str`.
613    ///
614    /// This is the non-panicking alternative to indexing the `str`. Returns
615    /// [`None`] whenever equivalent indexing operation would panic.
616    ///
617    /// # Examples
618    ///
619    /// ```
620    /// let v = String::from("🗻∈🌏");
621    ///
622    /// assert_eq!(Some("🗻"), v.get(0..4));
623    ///
624    /// // indices not on UTF-8 sequence boundaries
625    /// assert!(v.get(1..).is_none());
626    /// assert!(v.get(..8).is_none());
627    ///
628    /// // out of bounds
629    /// assert!(v.get(..42).is_none());
630    /// ```
631    #[stable(feature = "str_checked_slicing", since = "1.20.0")]
632    #[rustc_const_unstable(feature = "const_index", issue = "143775")]
633    #[inline]
634    pub const fn get<I: [const] SliceIndex<str>>(&self, i: I) -> Option<&I::Output> {
635        i.get(self)
636    }
637
638    /// Returns a mutable subslice of `str`.
639    ///
640    /// This is the non-panicking alternative to indexing the `str`. Returns
641    /// [`None`] whenever equivalent indexing operation would panic.
642    ///
643    /// # Examples
644    ///
645    /// ```
646    /// let mut v = String::from("hello");
647    /// // correct length
648    /// assert!(v.get_mut(0..5).is_some());
649    /// // out of bounds
650    /// assert!(v.get_mut(..42).is_none());
651    /// assert_eq!(Some("he"), v.get_mut(0..2).map(|v| &*v));
652    ///
653    /// assert_eq!("hello", v);
654    /// {
655    ///     let s = v.get_mut(0..2);
656    ///     let s = s.map(|s| {
657    ///         s.make_ascii_uppercase();
658    ///         &*s
659    ///     });
660    ///     assert_eq!(Some("HE"), s);
661    /// }
662    /// assert_eq!("HEllo", v);
663    /// ```
664    #[stable(feature = "str_checked_slicing", since = "1.20.0")]
665    #[rustc_const_unstable(feature = "const_index", issue = "143775")]
666    #[inline]
667    pub const fn get_mut<I: [const] SliceIndex<str>>(&mut self, i: I) -> Option<&mut I::Output> {
668        i.get_mut(self)
669    }
670
671    /// Returns an unchecked subslice of `str`.
672    ///
673    /// This is the unchecked alternative to indexing the `str`.
674    ///
675    /// # Safety
676    ///
677    /// Callers of this function are responsible that these preconditions are
678    /// satisfied:
679    ///
680    /// * The starting index must not exceed the ending index;
681    /// * Indexes must be within bounds of the original slice;
682    /// * Indexes must lie on UTF-8 sequence boundaries.
683    ///
684    /// Failing that, the returned string slice may reference invalid memory or
685    /// violate the invariants communicated by the `str` type.
686    ///
687    /// # Examples
688    ///
689    /// ```
690    /// let v = "🗻∈🌏";
691    /// unsafe {
692    ///     assert_eq!("🗻", v.get_unchecked(0..4));
693    ///     assert_eq!("∈", v.get_unchecked(4..7));
694    ///     assert_eq!("🌏", v.get_unchecked(7..11));
695    /// }
696    /// ```
697    #[stable(feature = "str_checked_slicing", since = "1.20.0")]
698    #[inline]
699    pub unsafe fn get_unchecked<I: SliceIndex<str>>(&self, i: I) -> &I::Output {
700        // SAFETY: the caller must uphold the safety contract for `get_unchecked`;
701        // the slice is dereferenceable because `self` is a safe reference.
702        // The returned pointer is safe because impls of `SliceIndex` have to guarantee that it is.
703        unsafe { &*i.get_unchecked(self) }
704    }
705
706    /// Returns a mutable, unchecked subslice of `str`.
707    ///
708    /// This is the unchecked alternative to indexing the `str`.
709    ///
710    /// # Safety
711    ///
712    /// Callers of this function are responsible that these preconditions are
713    /// satisfied:
714    ///
715    /// * The starting index must not exceed the ending index;
716    /// * Indexes must be within bounds of the original slice;
717    /// * Indexes must lie on UTF-8 sequence boundaries.
718    ///
719    /// Failing that, the returned string slice may reference invalid memory or
720    /// violate the invariants communicated by the `str` type.
721    ///
722    /// # Examples
723    ///
724    /// ```
725    /// let mut v = String::from("🗻∈🌏");
726    /// unsafe {
727    ///     assert_eq!("🗻", v.get_unchecked_mut(0..4));
728    ///     assert_eq!("∈", v.get_unchecked_mut(4..7));
729    ///     assert_eq!("🌏", v.get_unchecked_mut(7..11));
730    /// }
731    /// ```
732    #[stable(feature = "str_checked_slicing", since = "1.20.0")]
733    #[inline]
734    pub unsafe fn get_unchecked_mut<I: SliceIndex<str>>(&mut self, i: I) -> &mut I::Output {
735        // SAFETY: the caller must uphold the safety contract for `get_unchecked_mut`;
736        // the slice is dereferenceable because `self` is a safe reference.
737        // The returned pointer is safe because impls of `SliceIndex` have to guarantee that it is.
738        unsafe { &mut *i.get_unchecked_mut(self) }
739    }
740
741    /// Creates a string slice from another string slice, bypassing safety
742    /// checks.
743    ///
744    /// This is generally not recommended, use with caution! For a safe
745    /// alternative see [`str`] and [`Index`].
746    ///
747    /// [`Index`]: crate::ops::Index
748    ///
749    /// This new slice goes from `begin` to `end`, including `begin` but
750    /// excluding `end`.
751    ///
752    /// To get a mutable string slice instead, see the
753    /// [`slice_mut_unchecked`] method.
754    ///
755    /// [`slice_mut_unchecked`]: str::slice_mut_unchecked
756    ///
757    /// # Safety
758    ///
759    /// Callers of this function are responsible that three preconditions are
760    /// satisfied:
761    ///
762    /// * `begin` must not exceed `end`.
763    /// * `begin` and `end` must be byte positions within the string slice.
764    /// * `begin` and `end` must lie on UTF-8 sequence boundaries.
765    ///
766    /// # Examples
767    ///
768    /// ```
769    /// let s = "Löwe 老虎 Léopard";
770    ///
771    /// unsafe {
772    ///     assert_eq!("Löwe 老虎 Léopard", s.slice_unchecked(0, 21));
773    /// }
774    ///
775    /// let s = "Hello, world!";
776    ///
777    /// unsafe {
778    ///     assert_eq!("world", s.slice_unchecked(7, 12));
779    /// }
780    /// ```
781    #[stable(feature = "rust1", since = "1.0.0")]
782    #[deprecated(since = "1.29.0", note = "use `get_unchecked(begin..end)` instead")]
783    #[must_use]
784    #[inline]
785    pub unsafe fn slice_unchecked(&self, begin: usize, end: usize) -> &str {
786        // SAFETY: the caller must uphold the safety contract for `get_unchecked`;
787        // the slice is dereferenceable because `self` is a safe reference.
788        // The returned pointer is safe because impls of `SliceIndex` have to guarantee that it is.
789        unsafe { &*(begin..end).get_unchecked(self) }
790    }
791
792    /// Creates a string slice from another string slice, bypassing safety
793    /// checks.
794    ///
795    /// This is generally not recommended, use with caution! For a safe
796    /// alternative see [`str`] and [`IndexMut`].
797    ///
798    /// [`IndexMut`]: crate::ops::IndexMut
799    ///
800    /// This new slice goes from `begin` to `end`, including `begin` but
801    /// excluding `end`.
802    ///
803    /// To get an immutable string slice instead, see the
804    /// [`slice_unchecked`] method.
805    ///
806    /// [`slice_unchecked`]: str::slice_unchecked
807    ///
808    /// # Safety
809    ///
810    /// Callers of this function are responsible that three preconditions are
811    /// satisfied:
812    ///
813    /// * `begin` must not exceed `end`.
814    /// * `begin` and `end` must be byte positions within the string slice.
815    /// * `begin` and `end` must lie on UTF-8 sequence boundaries.
816    #[stable(feature = "str_slice_mut", since = "1.5.0")]
817    #[deprecated(since = "1.29.0", note = "use `get_unchecked_mut(begin..end)` instead")]
818    #[inline]
819    pub unsafe fn slice_mut_unchecked(&mut self, begin: usize, end: usize) -> &mut str {
820        // SAFETY: the caller must uphold the safety contract for `get_unchecked_mut`;
821        // the slice is dereferenceable because `self` is a safe reference.
822        // The returned pointer is safe because impls of `SliceIndex` have to guarantee that it is.
823        unsafe { &mut *(begin..end).get_unchecked_mut(self) }
824    }
825
826    /// Divides one string slice into two at an index.
827    ///
828    /// The argument, `mid`, should be a byte offset from the start of the
829    /// string. It must also be on the boundary of a UTF-8 code point.
830    ///
831    /// The two slices returned go from the start of the string slice to `mid`,
832    /// and from `mid` to the end of the string slice.
833    ///
834    /// To get mutable string slices instead, see the [`split_at_mut`]
835    /// method.
836    ///
837    /// [`split_at_mut`]: str::split_at_mut
838    ///
839    /// # Panics
840    ///
841    /// Panics if `mid` is not on a UTF-8 code point boundary, or if it is past
842    /// the end of the last code point of the string slice.  For a non-panicking
843    /// alternative see [`split_at_checked`](str::split_at_checked).
844    ///
845    /// # Examples
846    ///
847    /// ```
848    /// let s = "Per Martin-Löf";
849    ///
850    /// let (first, last) = s.split_at(3);
851    ///
852    /// assert_eq!("Per", first);
853    /// assert_eq!(" Martin-Löf", last);
854    /// ```
855    #[inline]
856    #[must_use]
857    #[stable(feature = "str_split_at", since = "1.4.0")]
858    #[rustc_const_stable(feature = "const_str_split_at", since = "1.86.0")]
859    pub const fn split_at(&self, mid: usize) -> (&str, &str) {
860        match self.split_at_checked(mid) {
861            None => slice_error_fail(self, 0, mid),
862            Some(pair) => pair,
863        }
864    }
865
866    /// Divides one mutable string slice into two at an index.
867    ///
868    /// The argument, `mid`, should be a byte offset from the start of the
869    /// string. It must also be on the boundary of a UTF-8 code point.
870    ///
871    /// The two slices returned go from the start of the string slice to `mid`,
872    /// and from `mid` to the end of the string slice.
873    ///
874    /// To get immutable string slices instead, see the [`split_at`] method.
875    ///
876    /// [`split_at`]: str::split_at
877    ///
878    /// # Panics
879    ///
880    /// Panics if `mid` is not on a UTF-8 code point boundary, or if it is past
881    /// the end of the last code point of the string slice.  For a non-panicking
882    /// alternative see [`split_at_mut_checked`](str::split_at_mut_checked).
883    ///
884    /// # Examples
885    ///
886    /// ```
887    /// let mut s = "Per Martin-Löf".to_string();
888    /// {
889    ///     let (first, last) = s.split_at_mut(3);
890    ///     first.make_ascii_uppercase();
891    ///     assert_eq!("PER", first);
892    ///     assert_eq!(" Martin-Löf", last);
893    /// }
894    /// assert_eq!("PER Martin-Löf", s);
895    /// ```
896    #[inline]
897    #[must_use]
898    #[stable(feature = "str_split_at", since = "1.4.0")]
899    #[rustc_const_stable(feature = "const_str_split_at", since = "1.86.0")]
900    pub const fn split_at_mut(&mut self, mid: usize) -> (&mut str, &mut str) {
901        // is_char_boundary checks that the index is in [0, .len()]
902        if self.is_char_boundary(mid) {
903            // SAFETY: just checked that `mid` is on a char boundary.
904            unsafe { self.split_at_mut_unchecked(mid) }
905        } else {
906            slice_error_fail(self, 0, mid)
907        }
908    }
909
910    /// Divides one string slice into two at an index.
911    ///
912    /// The argument, `mid`, should be a valid byte offset from the start of the
913    /// string. It must also be on the boundary of a UTF-8 code point. The
914    /// method returns `None` if that’s not the case.
915    ///
916    /// The two slices returned go from the start of the string slice to `mid`,
917    /// and from `mid` to the end of the string slice.
918    ///
919    /// To get mutable string slices instead, see the [`split_at_mut_checked`]
920    /// method.
921    ///
922    /// [`split_at_mut_checked`]: str::split_at_mut_checked
923    ///
924    /// # Examples
925    ///
926    /// ```
927    /// let s = "Per Martin-Löf";
928    ///
929    /// let (first, last) = s.split_at_checked(3).unwrap();
930    /// assert_eq!("Per", first);
931    /// assert_eq!(" Martin-Löf", last);
932    ///
933    /// assert_eq!(None, s.split_at_checked(13));  // Inside “ö”
934    /// assert_eq!(None, s.split_at_checked(16));  // Beyond the string length
935    /// ```
936    #[inline]
937    #[must_use]
938    #[stable(feature = "split_at_checked", since = "1.80.0")]
939    #[rustc_const_stable(feature = "const_str_split_at", since = "1.86.0")]
940    pub const fn split_at_checked(&self, mid: usize) -> Option<(&str, &str)> {
941        // is_char_boundary checks that the index is in [0, .len()]
942        if self.is_char_boundary(mid) {
943            // SAFETY: just checked that `mid` is on a char boundary.
944            Some(unsafe { self.split_at_unchecked(mid) })
945        } else {
946            None
947        }
948    }
949
950    /// Divides one mutable string slice into two at an index.
951    ///
952    /// The argument, `mid`, should be a valid byte offset from the start of the
953    /// string. It must also be on the boundary of a UTF-8 code point. The
954    /// method returns `None` if that’s not the case.
955    ///
956    /// The two slices returned go from the start of the string slice to `mid`,
957    /// and from `mid` to the end of the string slice.
958    ///
959    /// To get immutable string slices instead, see the [`split_at_checked`] method.
960    ///
961    /// [`split_at_checked`]: str::split_at_checked
962    ///
963    /// # Examples
964    ///
965    /// ```
966    /// let mut s = "Per Martin-Löf".to_string();
967    /// if let Some((first, last)) = s.split_at_mut_checked(3) {
968    ///     first.make_ascii_uppercase();
969    ///     assert_eq!("PER", first);
970    ///     assert_eq!(" Martin-Löf", last);
971    /// }
972    /// assert_eq!("PER Martin-Löf", s);
973    ///
974    /// assert_eq!(None, s.split_at_mut_checked(13));  // Inside “ö”
975    /// assert_eq!(None, s.split_at_mut_checked(16));  // Beyond the string length
976    /// ```
977    #[inline]
978    #[must_use]
979    #[stable(feature = "split_at_checked", since = "1.80.0")]
980    #[rustc_const_stable(feature = "const_str_split_at", since = "1.86.0")]
981    pub const fn split_at_mut_checked(&mut self, mid: usize) -> Option<(&mut str, &mut str)> {
982        // is_char_boundary checks that the index is in [0, .len()]
983        if self.is_char_boundary(mid) {
984            // SAFETY: just checked that `mid` is on a char boundary.
985            Some(unsafe { self.split_at_mut_unchecked(mid) })
986        } else {
987            None
988        }
989    }
990
991    /// Divides one string slice into two at an index.
992    ///
993    /// # Safety
994    ///
995    /// The caller must ensure that `mid` is a valid byte offset from the start
996    /// of the string and falls on the boundary of a UTF-8 code point.
997    #[inline]
998    const unsafe fn split_at_unchecked(&self, mid: usize) -> (&str, &str) {
999        let len = self.len();
1000        let ptr = self.as_ptr();
1001        // SAFETY: caller guarantees `mid` is on a char boundary.
1002        unsafe {
1003            (
1004                from_utf8_unchecked(slice::from_raw_parts(ptr, mid)),
1005                from_utf8_unchecked(slice::from_raw_parts(ptr.add(mid), len - mid)),
1006            )
1007        }
1008    }
1009
1010    /// Divides one string slice into two at an index.
1011    ///
1012    /// # Safety
1013    ///
1014    /// The caller must ensure that `mid` is a valid byte offset from the start
1015    /// of the string and falls on the boundary of a UTF-8 code point.
1016    const unsafe fn split_at_mut_unchecked(&mut self, mid: usize) -> (&mut str, &mut str) {
1017        let len = self.len();
1018        let ptr = self.as_mut_ptr();
1019        // SAFETY: caller guarantees `mid` is on a char boundary.
1020        unsafe {
1021            (
1022                from_utf8_unchecked_mut(slice::from_raw_parts_mut(ptr, mid)),
1023                from_utf8_unchecked_mut(slice::from_raw_parts_mut(ptr.add(mid), len - mid)),
1024            )
1025        }
1026    }
1027
1028    /// Returns an iterator over the [`char`]s of a string slice.
1029    ///
1030    /// As a string slice consists of valid UTF-8, we can iterate through a
1031    /// string slice by [`char`]. This method returns such an iterator.
1032    ///
1033    /// It's important to remember that [`char`] represents a Unicode Scalar
1034    /// Value, and might not match your idea of what a 'character' is. Iteration
1035    /// over grapheme clusters may be what you actually want. This functionality
1036    /// is not provided by Rust's standard library, check crates.io instead.
1037    ///
1038    /// # Examples
1039    ///
1040    /// Basic usage:
1041    ///
1042    /// ```
1043    /// let word = "goodbye";
1044    ///
1045    /// let count = word.chars().count();
1046    /// assert_eq!(7, count);
1047    ///
1048    /// let mut chars = word.chars();
1049    ///
1050    /// assert_eq!(Some('g'), chars.next());
1051    /// assert_eq!(Some('o'), chars.next());
1052    /// assert_eq!(Some('o'), chars.next());
1053    /// assert_eq!(Some('d'), chars.next());
1054    /// assert_eq!(Some('b'), chars.next());
1055    /// assert_eq!(Some('y'), chars.next());
1056    /// assert_eq!(Some('e'), chars.next());
1057    ///
1058    /// assert_eq!(None, chars.next());
1059    /// ```
1060    ///
1061    /// Remember, [`char`]s might not match your intuition about characters:
1062    ///
1063    /// [`char`]: prim@char
1064    ///
1065    /// ```
1066    /// let y = "y̆";
1067    ///
1068    /// let mut chars = y.chars();
1069    ///
1070    /// assert_eq!(Some('y'), chars.next()); // not 'y̆'
1071    /// assert_eq!(Some('\u{0306}'), chars.next());
1072    ///
1073    /// assert_eq!(None, chars.next());
1074    /// ```
1075    #[stable(feature = "rust1", since = "1.0.0")]
1076    #[inline]
1077    #[rustc_diagnostic_item = "str_chars"]
1078    pub fn chars(&self) -> Chars<'_> {
1079        Chars { iter: self.as_bytes().iter() }
1080    }
1081
1082    /// Returns an iterator over the [`char`]s of a string slice, and their
1083    /// positions.
1084    ///
1085    /// As a string slice consists of valid UTF-8, we can iterate through a
1086    /// string slice by [`char`]. This method returns an iterator of both
1087    /// these [`char`]s, as well as their byte positions.
1088    ///
1089    /// The iterator yields tuples. The position is first, the [`char`] is
1090    /// second.
1091    ///
1092    /// # Examples
1093    ///
1094    /// Basic usage:
1095    ///
1096    /// ```
1097    /// let word = "goodbye";
1098    ///
1099    /// let count = word.char_indices().count();
1100    /// assert_eq!(7, count);
1101    ///
1102    /// let mut char_indices = word.char_indices();
1103    ///
1104    /// assert_eq!(Some((0, 'g')), char_indices.next());
1105    /// assert_eq!(Some((1, 'o')), char_indices.next());
1106    /// assert_eq!(Some((2, 'o')), char_indices.next());
1107    /// assert_eq!(Some((3, 'd')), char_indices.next());
1108    /// assert_eq!(Some((4, 'b')), char_indices.next());
1109    /// assert_eq!(Some((5, 'y')), char_indices.next());
1110    /// assert_eq!(Some((6, 'e')), char_indices.next());
1111    ///
1112    /// assert_eq!(None, char_indices.next());
1113    /// ```
1114    ///
1115    /// Remember, [`char`]s might not match your intuition about characters:
1116    ///
1117    /// [`char`]: prim@char
1118    ///
1119    /// ```
1120    /// let yes = "y̆es";
1121    ///
1122    /// let mut char_indices = yes.char_indices();
1123    ///
1124    /// assert_eq!(Some((0, 'y')), char_indices.next()); // not (0, 'y̆')
1125    /// assert_eq!(Some((1, '\u{0306}')), char_indices.next());
1126    ///
1127    /// // note the 3 here - the previous character took up two bytes
1128    /// assert_eq!(Some((3, 'e')), char_indices.next());
1129    /// assert_eq!(Some((4, 's')), char_indices.next());
1130    ///
1131    /// assert_eq!(None, char_indices.next());
1132    /// ```
1133    #[stable(feature = "rust1", since = "1.0.0")]
1134    #[inline]
1135    pub fn char_indices(&self) -> CharIndices<'_> {
1136        CharIndices { front_offset: 0, iter: self.chars() }
1137    }
1138
1139    /// Returns an iterator over the bytes of a string slice.
1140    ///
1141    /// As a string slice consists of a sequence of bytes, we can iterate
1142    /// through a string slice by byte. This method returns such an iterator.
1143    ///
1144    /// # Examples
1145    ///
1146    /// ```
1147    /// let mut bytes = "bors".bytes();
1148    ///
1149    /// assert_eq!(Some(b'b'), bytes.next());
1150    /// assert_eq!(Some(b'o'), bytes.next());
1151    /// assert_eq!(Some(b'r'), bytes.next());
1152    /// assert_eq!(Some(b's'), bytes.next());
1153    ///
1154    /// assert_eq!(None, bytes.next());
1155    /// ```
1156    #[stable(feature = "rust1", since = "1.0.0")]
1157    #[inline]
1158    pub fn bytes(&self) -> Bytes<'_> {
1159        Bytes(self.as_bytes().iter().copied())
1160    }
1161
1162    /// Splits a string slice by whitespace.
1163    ///
1164    /// The iterator returned will return string slices that are sub-slices of
1165    /// the original string slice, separated by any amount of whitespace.
1166    ///
1167    /// 'Whitespace' is defined according to the terms of the Unicode Derived
1168    /// Core Property `White_Space`. If you only want to split on ASCII whitespace
1169    /// instead, use [`split_ascii_whitespace`].
1170    ///
1171    /// [`split_ascii_whitespace`]: str::split_ascii_whitespace
1172    ///
1173    /// # Examples
1174    ///
1175    /// Basic usage:
1176    ///
1177    /// ```
1178    /// let mut iter = "A few words".split_whitespace();
1179    ///
1180    /// assert_eq!(Some("A"), iter.next());
1181    /// assert_eq!(Some("few"), iter.next());
1182    /// assert_eq!(Some("words"), iter.next());
1183    ///
1184    /// assert_eq!(None, iter.next());
1185    /// ```
1186    ///
1187    /// All kinds of whitespace are considered:
1188    ///
1189    /// ```
1190    /// let mut iter = " Mary   had\ta\u{2009}little  \n\t lamb".split_whitespace();
1191    /// assert_eq!(Some("Mary"), iter.next());
1192    /// assert_eq!(Some("had"), iter.next());
1193    /// assert_eq!(Some("a"), iter.next());
1194    /// assert_eq!(Some("little"), iter.next());
1195    /// assert_eq!(Some("lamb"), iter.next());
1196    ///
1197    /// assert_eq!(None, iter.next());
1198    /// ```
1199    ///
1200    /// If the string is empty or all whitespace, the iterator yields no string slices:
1201    /// ```
1202    /// assert_eq!("".split_whitespace().next(), None);
1203    /// assert_eq!("   ".split_whitespace().next(), None);
1204    /// ```
1205    #[must_use = "this returns the split string as an iterator, \
1206                  without modifying the original"]
1207    #[stable(feature = "split_whitespace", since = "1.1.0")]
1208    #[rustc_diagnostic_item = "str_split_whitespace"]
1209    #[inline]
1210    pub fn split_whitespace(&self) -> SplitWhitespace<'_> {
1211        SplitWhitespace { inner: self.split(IsWhitespace).filter(IsNotEmpty) }
1212    }
1213
1214    /// Splits a string slice by ASCII whitespace.
1215    ///
1216    /// The iterator returned will return string slices that are sub-slices of
1217    /// the original string slice, separated by any amount of ASCII whitespace.
1218    ///
1219    /// This uses the same definition as [`char::is_ascii_whitespace`].
1220    /// To split by Unicode `Whitespace` instead, use [`split_whitespace`].
1221    /// Note that because of this difference in definition, even if `s.is_ascii()`
1222    /// is `true`, `s.split_ascii_whitespace()` behavior will differ from `s.split_whitespace()`
1223    /// if `s` contains U+000B VERTICAL TAB.
1224    ///
1225    /// [`split_whitespace`]: str::split_whitespace
1226    ///
1227    /// # Examples
1228    ///
1229    /// Basic usage:
1230    ///
1231    /// ```
1232    /// let mut iter = "A few words".split_ascii_whitespace();
1233    ///
1234    /// assert_eq!(Some("A"), iter.next());
1235    /// assert_eq!(Some("few"), iter.next());
1236    /// assert_eq!(Some("words"), iter.next());
1237    ///
1238    /// assert_eq!(None, iter.next());
1239    /// ```
1240    ///
1241    /// Various kinds of ASCII whitespace are considered
1242    /// (see [`char::is_ascii_whitespace`]):
1243    ///
1244    /// ```
1245    /// let mut iter = " Mary   had\ta little  \n\t lamb".split_ascii_whitespace();
1246    /// assert_eq!(Some("Mary"), iter.next());
1247    /// assert_eq!(Some("had"), iter.next());
1248    /// assert_eq!(Some("a"), iter.next());
1249    /// assert_eq!(Some("little"), iter.next());
1250    /// assert_eq!(Some("lamb"), iter.next());
1251    ///
1252    /// assert_eq!(None, iter.next());
1253    /// ```
1254    ///
1255    /// If the string is empty or all ASCII whitespace, the iterator yields no string slices:
1256    /// ```
1257    /// assert_eq!("".split_ascii_whitespace().next(), None);
1258    /// assert_eq!("   ".split_ascii_whitespace().next(), None);
1259    /// ```
1260    #[must_use = "this returns the split string as an iterator, \
1261                  without modifying the original"]
1262    #[stable(feature = "split_ascii_whitespace", since = "1.34.0")]
1263    #[inline]
1264    pub fn split_ascii_whitespace(&self) -> SplitAsciiWhitespace<'_> {
1265        let inner =
1266            self.as_bytes().split(IsAsciiWhitespace).filter(BytesIsNotEmpty).map(UnsafeBytesToStr);
1267        SplitAsciiWhitespace { inner }
1268    }
1269
1270    /// Returns an iterator over the lines of a string, as string slices.
1271    ///
1272    /// Lines are split at line endings that are either newlines (`\n`) or
1273    /// sequences of a carriage return followed by a line feed (`\r\n`).
1274    ///
1275    /// Line terminators are not included in the lines returned by the iterator.
1276    ///
1277    /// Note that any carriage return (`\r`) not immediately followed by a
1278    /// line feed (`\n`) does not split a line. These carriage returns are
1279    /// thereby included in the produced lines.
1280    ///
1281    /// The final line ending is optional. A string that ends with a final line
1282    /// ending will return the same lines as an otherwise identical string
1283    /// without a final line ending.
1284    ///
1285    /// An empty string returns an empty iterator.
1286    ///
1287    /// # Examples
1288    ///
1289    /// Basic usage:
1290    ///
1291    /// ```
1292    /// let text = "foo\r\nbar\n\nbaz\r";
1293    /// let mut lines = text.lines();
1294    ///
1295    /// assert_eq!(Some("foo"), lines.next());
1296    /// assert_eq!(Some("bar"), lines.next());
1297    /// assert_eq!(Some(""), lines.next());
1298    /// // Trailing carriage return is included in the last line
1299    /// assert_eq!(Some("baz\r"), lines.next());
1300    ///
1301    /// assert_eq!(None, lines.next());
1302    /// ```
1303    ///
1304    /// The final line does not require any ending:
1305    ///
1306    /// ```
1307    /// let text = "foo\nbar\n\r\nbaz";
1308    /// let mut lines = text.lines();
1309    ///
1310    /// assert_eq!(Some("foo"), lines.next());
1311    /// assert_eq!(Some("bar"), lines.next());
1312    /// assert_eq!(Some(""), lines.next());
1313    /// assert_eq!(Some("baz"), lines.next());
1314    ///
1315    /// assert_eq!(None, lines.next());
1316    /// ```
1317    ///
1318    /// An empty string returns an empty iterator:
1319    ///
1320    /// ```
1321    /// let text = "";
1322    /// let mut lines = text.lines();
1323    ///
1324    /// assert_eq!(lines.next(), None);
1325    /// ```
1326    #[stable(feature = "rust1", since = "1.0.0")]
1327    #[inline]
1328    pub fn lines(&self) -> Lines<'_> {
1329        Lines(self.split_inclusive('\n').map(LinesMap))
1330    }
1331
1332    /// Returns an iterator over the lines of a string.
1333    #[stable(feature = "rust1", since = "1.0.0")]
1334    #[deprecated(since = "1.4.0", note = "use lines() instead now", suggestion = "lines")]
1335    #[inline]
1336    #[allow(deprecated)]
1337    pub fn lines_any(&self) -> LinesAny<'_> {
1338        LinesAny(self.lines())
1339    }
1340
1341    /// Returns an iterator of `u16` over the string encoded
1342    /// as native endian UTF-16 (without byte-order mark).
1343    ///
1344    /// # Examples
1345    ///
1346    /// ```
1347    /// let text = "Zażółć gęślą jaźń";
1348    ///
1349    /// let utf8_len = text.len();
1350    /// let utf16_len = text.encode_utf16().count();
1351    ///
1352    /// assert!(utf16_len <= utf8_len);
1353    /// ```
1354    #[must_use = "this returns the encoded string as an iterator, \
1355                  without modifying the original"]
1356    #[stable(feature = "encode_utf16", since = "1.8.0")]
1357    pub fn encode_utf16(&self) -> EncodeUtf16<'_> {
1358        EncodeUtf16 { chars: self.chars(), extra: 0 }
1359    }
1360
1361    /// Returns `true` if the given pattern matches a sub-slice of
1362    /// this string slice.
1363    ///
1364    /// Returns `false` if it does not.
1365    ///
1366    /// The [pattern] can be a `&str`, [`char`], a slice of [`char`]s, or a
1367    /// function or closure that determines if a character matches.
1368    ///
1369    /// [`char`]: prim@char
1370    /// [pattern]: self::pattern
1371    ///
1372    /// # Examples
1373    ///
1374    /// ```
1375    /// let bananas = "bananas";
1376    ///
1377    /// assert!(bananas.contains("nana"));
1378    /// assert!(!bananas.contains("apples"));
1379    /// ```
1380    #[stable(feature = "rust1", since = "1.0.0")]
1381    #[inline]
1382    pub fn contains<P: Pattern>(&self, pat: P) -> bool {
1383        pat.is_contained_in(self)
1384    }
1385
1386    /// Returns `true` if the given pattern matches a prefix of this
1387    /// string slice.
1388    ///
1389    /// Returns `false` if it does not.
1390    ///
1391    /// The [pattern] can be a `&str`, in which case this function will return true if
1392    /// the `&str` is a prefix of this string slice.
1393    ///
1394    /// The [pattern] can also be a [`char`], a slice of [`char`]s, or a
1395    /// function or closure that determines if a character matches.
1396    /// These will only be checked against the first character of this string slice.
1397    /// Look at the second example below regarding behavior for slices of [`char`]s.
1398    ///
1399    /// [`char`]: prim@char
1400    /// [pattern]: self::pattern
1401    ///
1402    /// # Examples
1403    ///
1404    /// ```
1405    /// let bananas = "bananas";
1406    ///
1407    /// assert!(bananas.starts_with("bana"));
1408    /// assert!(!bananas.starts_with("nana"));
1409    /// ```
1410    ///
1411    /// ```
1412    /// let bananas = "bananas";
1413    ///
1414    /// // Note that both of these assert successfully.
1415    /// assert!(bananas.starts_with(&['b', 'a', 'n', 'a']));
1416    /// assert!(bananas.starts_with(&['a', 'b', 'c', 'd']));
1417    /// ```
1418    #[stable(feature = "rust1", since = "1.0.0")]
1419    #[rustc_diagnostic_item = "str_starts_with"]
1420    pub fn starts_with<P: Pattern>(&self, pat: P) -> bool {
1421        pat.is_prefix_of(self)
1422    }
1423
1424    /// Returns `true` if the given pattern matches a suffix of this
1425    /// string slice.
1426    ///
1427    /// Returns `false` if it does not.
1428    ///
1429    /// The [pattern] can be a `&str`, [`char`], a slice of [`char`]s, or a
1430    /// function or closure that determines if a character matches.
1431    ///
1432    /// [`char`]: prim@char
1433    /// [pattern]: self::pattern
1434    ///
1435    /// # Examples
1436    ///
1437    /// ```
1438    /// let bananas = "bananas";
1439    ///
1440    /// assert!(bananas.ends_with("anas"));
1441    /// assert!(!bananas.ends_with("nana"));
1442    /// ```
1443    #[stable(feature = "rust1", since = "1.0.0")]
1444    #[rustc_diagnostic_item = "str_ends_with"]
1445    pub fn ends_with<P: Pattern>(&self, pat: P) -> bool
1446    where
1447        for<'a> P::Searcher<'a>: ReverseSearcher<'a>,
1448    {
1449        pat.is_suffix_of(self)
1450    }
1451
1452    /// Returns the byte index of the first character of this string slice that
1453    /// matches the pattern.
1454    ///
1455    /// Returns [`None`] if the pattern doesn't match.
1456    ///
1457    /// The [pattern] can be a `&str`, [`char`], a slice of [`char`]s, or a
1458    /// function or closure that determines if a character matches.
1459    ///
1460    /// [`char`]: prim@char
1461    /// [pattern]: self::pattern
1462    ///
1463    /// # Examples
1464    ///
1465    /// Simple patterns:
1466    ///
1467    /// ```
1468    /// let s = "Löwe 老虎 Léopard Gepardi";
1469    ///
1470    /// assert_eq!(s.find('L'), Some(0));
1471    /// assert_eq!(s.find('é'), Some(14));
1472    /// assert_eq!(s.find("pard"), Some(17));
1473    /// ```
1474    ///
1475    /// More complex patterns using point-free style and closures:
1476    ///
1477    /// ```
1478    /// let s = "Löwe 老虎 Léopard";
1479    ///
1480    /// assert_eq!(s.find(char::is_whitespace), Some(5));
1481    /// assert_eq!(s.find(char::is_lowercase), Some(1));
1482    /// assert_eq!(s.find(|c: char| c.is_whitespace() || c.is_lowercase()), Some(1));
1483    /// assert_eq!(s.find(|c: char| (c < 'o') && (c > 'a')), Some(4));
1484    /// ```
1485    ///
1486    /// Not finding the pattern:
1487    ///
1488    /// ```
1489    /// let s = "Löwe 老虎 Léopard";
1490    /// let x: &[_] = &['1', '2'];
1491    ///
1492    /// assert_eq!(s.find(x), None);
1493    /// ```
1494    #[stable(feature = "rust1", since = "1.0.0")]
1495    #[inline]
1496    pub fn find<P: Pattern>(&self, pat: P) -> Option<usize> {
1497        pat.into_searcher(self).next_match().map(|(i, _)| i)
1498    }
1499
1500    /// Returns the byte index for the first character of the last match of the pattern in
1501    /// this string slice.
1502    ///
1503    /// Returns [`None`] if the pattern doesn't match.
1504    ///
1505    /// The [pattern] can be a `&str`, [`char`], a slice of [`char`]s, or a
1506    /// function or closure that determines if a character matches.
1507    ///
1508    /// [`char`]: prim@char
1509    /// [pattern]: self::pattern
1510    ///
1511    /// # Examples
1512    ///
1513    /// Simple patterns:
1514    ///
1515    /// ```
1516    /// let s = "Löwe 老虎 Léopard Gepardi";
1517    ///
1518    /// assert_eq!(s.rfind('L'), Some(13));
1519    /// assert_eq!(s.rfind('é'), Some(14));
1520    /// assert_eq!(s.rfind("pard"), Some(24));
1521    /// ```
1522    ///
1523    /// More complex patterns with closures:
1524    ///
1525    /// ```
1526    /// let s = "Löwe 老虎 Léopard";
1527    ///
1528    /// assert_eq!(s.rfind(char::is_whitespace), Some(12));
1529    /// assert_eq!(s.rfind(char::is_lowercase), Some(20));
1530    /// ```
1531    ///
1532    /// Not finding the pattern:
1533    ///
1534    /// ```
1535    /// let s = "Löwe 老虎 Léopard";
1536    /// let x: &[_] = &['1', '2'];
1537    ///
1538    /// assert_eq!(s.rfind(x), None);
1539    /// ```
1540    #[stable(feature = "rust1", since = "1.0.0")]
1541    #[inline]
1542    pub fn rfind<P: Pattern>(&self, pat: P) -> Option<usize>
1543    where
1544        for<'a> P::Searcher<'a>: ReverseSearcher<'a>,
1545    {
1546        pat.into_searcher(self).next_match_back().map(|(i, _)| i)
1547    }
1548
1549    /// Returns an iterator over substrings of this string slice, separated by
1550    /// characters matched by a pattern.
1551    ///
1552    /// The [pattern] can be a `&str`, [`char`], a slice of [`char`]s, or a
1553    /// function or closure that determines if a character matches.
1554    ///
1555    /// If there are no matches the full string slice is returned as the only
1556    /// item in the iterator.
1557    ///
1558    /// [`char`]: prim@char
1559    /// [pattern]: self::pattern
1560    ///
1561    /// # Iterator behavior
1562    ///
1563    /// The returned iterator will be a [`DoubleEndedIterator`] if the pattern
1564    /// allows a reverse search and forward/reverse search yields the same
1565    /// elements. This is true for, e.g., [`char`], but not for `&str`.
1566    ///
1567    /// If the pattern allows a reverse search but its results might differ
1568    /// from a forward search, the [`rsplit`] method can be used.
1569    ///
1570    /// [`rsplit`]: str::rsplit
1571    ///
1572    /// # Examples
1573    ///
1574    /// Simple patterns:
1575    ///
1576    /// ```
1577    /// let v: Vec<&str> = "Mary had a little lamb".split(' ').collect();
1578    /// assert_eq!(v, ["Mary", "had", "a", "little", "lamb"]);
1579    ///
1580    /// let v: Vec<&str> = "".split('X').collect();
1581    /// assert_eq!(v, [""]);
1582    ///
1583    /// let v: Vec<&str> = "lionXXtigerXleopard".split('X').collect();
1584    /// assert_eq!(v, ["lion", "", "tiger", "leopard"]);
1585    ///
1586    /// let v: Vec<&str> = "lion::tiger::leopard".split("::").collect();
1587    /// assert_eq!(v, ["lion", "tiger", "leopard"]);
1588    ///
1589    /// let v: Vec<&str> = "AABBCC".split("DD").collect();
1590    /// assert_eq!(v, ["AABBCC"]);
1591    ///
1592    /// let v: Vec<&str> = "abc1def2ghi".split(char::is_numeric).collect();
1593    /// assert_eq!(v, ["abc", "def", "ghi"]);
1594    ///
1595    /// let v: Vec<&str> = "lionXtigerXleopard".split(char::is_uppercase).collect();
1596    /// assert_eq!(v, ["lion", "tiger", "leopard"]);
1597    /// ```
1598    ///
1599    /// If the pattern is a slice of chars, split on each occurrence of any of the characters:
1600    ///
1601    /// ```
1602    /// let v: Vec<&str> = "2020-11-03 23:59".split(&['-', ' ', ':', '@'][..]).collect();
1603    /// assert_eq!(v, ["2020", "11", "03", "23", "59"]);
1604    /// ```
1605    ///
1606    /// A more complex pattern, using a closure:
1607    ///
1608    /// ```
1609    /// let v: Vec<&str> = "abc1defXghi".split(|c| c == '1' || c == 'X').collect();
1610    /// assert_eq!(v, ["abc", "def", "ghi"]);
1611    /// ```
1612    ///
1613    /// If a string contains multiple contiguous separators, you will end up
1614    /// with empty strings in the output:
1615    ///
1616    /// ```
1617    /// let x = "||||a||b|c".to_string();
1618    /// let d: Vec<_> = x.split('|').collect();
1619    ///
1620    /// assert_eq!(d, &["", "", "", "", "a", "", "b", "c"]);
1621    /// ```
1622    ///
1623    /// Contiguous separators are separated by the empty string.
1624    ///
1625    /// ```
1626    /// let x = "(///)".to_string();
1627    /// let d: Vec<_> = x.split('/').collect();
1628    ///
1629    /// assert_eq!(d, &["(", "", "", ")"]);
1630    /// ```
1631    ///
1632    /// Separators at the start or end of a string are neighbored
1633    /// by empty strings.
1634    ///
1635    /// ```
1636    /// let d: Vec<_> = "010".split("0").collect();
1637    /// assert_eq!(d, &["", "1", ""]);
1638    /// ```
1639    ///
1640    /// When the empty string is used as a separator, it separates
1641    /// every character in the string, along with the beginning
1642    /// and end of the string.
1643    ///
1644    /// ```
1645    /// let f: Vec<_> = "rust".split("").collect();
1646    /// assert_eq!(f, &["", "r", "u", "s", "t", ""]);
1647    /// ```
1648    ///
1649    /// Contiguous separators can lead to possibly surprising behavior
1650    /// when whitespace is used as the separator. This code is correct:
1651    ///
1652    /// ```
1653    /// let x = "    a  b c".to_string();
1654    /// let d: Vec<_> = x.split(' ').collect();
1655    ///
1656    /// assert_eq!(d, &["", "", "", "", "a", "", "b", "c"]);
1657    /// ```
1658    ///
1659    /// It does _not_ give you:
1660    ///
1661    /// ```,ignore
1662    /// assert_eq!(d, &["a", "b", "c"]);
1663    /// ```
1664    ///
1665    /// Use [`split_whitespace`] for this behavior.
1666    ///
1667    /// [`split_whitespace`]: str::split_whitespace
1668    #[stable(feature = "rust1", since = "1.0.0")]
1669    #[inline]
1670    pub fn split<P: Pattern>(&self, pat: P) -> Split<'_, P> {
1671        Split(SplitInternal {
1672            start: 0,
1673            end: self.len(),
1674            matcher: pat.into_searcher(self),
1675            allow_trailing_empty: true,
1676            finished: false,
1677        })
1678    }
1679
1680    /// Returns an iterator over substrings of this string slice, separated by
1681    /// characters matched by a pattern.
1682    ///
1683    /// Differs from the iterator produced by `split` in that `split_inclusive`
1684    /// leaves the matched part as the terminator of the substring.
1685    ///
1686    /// The [pattern] can be a `&str`, [`char`], a slice of [`char`]s, or a
1687    /// function or closure that determines if a character matches.
1688    ///
1689    /// [`char`]: prim@char
1690    /// [pattern]: self::pattern
1691    ///
1692    /// # Examples
1693    ///
1694    /// ```
1695    /// let v: Vec<&str> = "Mary had a little lamb\nlittle lamb\nlittle lamb."
1696    ///     .split_inclusive('\n').collect();
1697    /// assert_eq!(v, ["Mary had a little lamb\n", "little lamb\n", "little lamb."]);
1698    /// ```
1699    ///
1700    /// If the last element of the string is matched,
1701    /// that element will be considered the terminator of the preceding substring.
1702    /// That substring will be the last item returned by the iterator.
1703    ///
1704    /// ```
1705    /// let v: Vec<&str> = "Mary had a little lamb\nlittle lamb\nlittle lamb.\n"
1706    ///     .split_inclusive('\n').collect();
1707    /// assert_eq!(v, ["Mary had a little lamb\n", "little lamb\n", "little lamb.\n"]);
1708    /// ```
1709    #[stable(feature = "split_inclusive", since = "1.51.0")]
1710    #[inline]
1711    pub fn split_inclusive<P: Pattern>(&self, pat: P) -> SplitInclusive<'_, P> {
1712        SplitInclusive(SplitInternal {
1713            start: 0,
1714            end: self.len(),
1715            matcher: pat.into_searcher(self),
1716            allow_trailing_empty: false,
1717            finished: false,
1718        })
1719    }
1720
1721    /// Returns an iterator over substrings of the given string slice, separated
1722    /// by characters matched by a pattern and yielded in reverse order.
1723    ///
1724    /// The [pattern] can be a `&str`, [`char`], a slice of [`char`]s, or a
1725    /// function or closure that determines if a character matches.
1726    ///
1727    /// [`char`]: prim@char
1728    /// [pattern]: self::pattern
1729    ///
1730    /// # Iterator behavior
1731    ///
1732    /// The returned iterator requires that the pattern supports a reverse
1733    /// search, and it will be a [`DoubleEndedIterator`] if a forward/reverse
1734    /// search yields the same elements.
1735    ///
1736    /// For iterating from the front, the [`split`] method can be used.
1737    ///
1738    /// [`split`]: str::split
1739    ///
1740    /// # Examples
1741    ///
1742    /// Simple patterns:
1743    ///
1744    /// ```
1745    /// let v: Vec<&str> = "Mary had a little lamb".rsplit(' ').collect();
1746    /// assert_eq!(v, ["lamb", "little", "a", "had", "Mary"]);
1747    ///
1748    /// let v: Vec<&str> = "".rsplit('X').collect();
1749    /// assert_eq!(v, [""]);
1750    ///
1751    /// let v: Vec<&str> = "lionXXtigerXleopard".rsplit('X').collect();
1752    /// assert_eq!(v, ["leopard", "tiger", "", "lion"]);
1753    ///
1754    /// let v: Vec<&str> = "lion::tiger::leopard".rsplit("::").collect();
1755    /// assert_eq!(v, ["leopard", "tiger", "lion"]);
1756    /// ```
1757    ///
1758    /// A more complex pattern, using a closure:
1759    ///
1760    /// ```
1761    /// let v: Vec<&str> = "abc1defXghi".rsplit(|c| c == '1' || c == 'X').collect();
1762    /// assert_eq!(v, ["ghi", "def", "abc"]);
1763    /// ```
1764    #[stable(feature = "rust1", since = "1.0.0")]
1765    #[inline]
1766    pub fn rsplit<P: Pattern>(&self, pat: P) -> RSplit<'_, P>
1767    where
1768        for<'a> P::Searcher<'a>: ReverseSearcher<'a>,
1769    {
1770        RSplit(self.split(pat).0)
1771    }
1772
1773    /// Returns an iterator over substrings of the given string slice, separated
1774    /// by characters matched by a pattern.
1775    ///
1776    /// The [pattern] can be a `&str`, [`char`], a slice of [`char`]s, or a
1777    /// function or closure that determines if a character matches.
1778    ///
1779    /// [`char`]: prim@char
1780    /// [pattern]: self::pattern
1781    ///
1782    /// Equivalent to [`split`], except that the trailing substring
1783    /// is skipped if empty.
1784    ///
1785    /// [`split`]: str::split
1786    ///
1787    /// This method can be used for string data that is _terminated_,
1788    /// rather than _separated_ by a pattern.
1789    ///
1790    /// # Iterator behavior
1791    ///
1792    /// The returned iterator will be a [`DoubleEndedIterator`] if the pattern
1793    /// allows a reverse search and forward/reverse search yields the same
1794    /// elements. This is true for, e.g., [`char`], but not for `&str`.
1795    ///
1796    /// If the pattern allows a reverse search but its results might differ
1797    /// from a forward search, the [`rsplit_terminator`] method can be used.
1798    ///
1799    /// [`rsplit_terminator`]: str::rsplit_terminator
1800    ///
1801    /// # Examples
1802    ///
1803    /// ```
1804    /// let v: Vec<&str> = "A.B.".split_terminator('.').collect();
1805    /// assert_eq!(v, ["A", "B"]);
1806    ///
1807    /// let v: Vec<&str> = "A..B..".split_terminator(".").collect();
1808    /// assert_eq!(v, ["A", "", "B", ""]);
1809    ///
1810    /// let v: Vec<&str> = "A.B:C.D".split_terminator(&['.', ':'][..]).collect();
1811    /// assert_eq!(v, ["A", "B", "C", "D"]);
1812    /// ```
1813    #[stable(feature = "rust1", since = "1.0.0")]
1814    #[inline]
1815    pub fn split_terminator<P: Pattern>(&self, pat: P) -> SplitTerminator<'_, P> {
1816        SplitTerminator(SplitInternal { allow_trailing_empty: false, ..self.split(pat).0 })
1817    }
1818
1819    /// Returns an iterator over substrings of `self`, separated by characters
1820    /// matched by a pattern and yielded in reverse order.
1821    ///
1822    /// The [pattern] can be a `&str`, [`char`], a slice of [`char`]s, or a
1823    /// function or closure that determines if a character matches.
1824    ///
1825    /// [`char`]: prim@char
1826    /// [pattern]: self::pattern
1827    ///
1828    /// Equivalent to [`split`], except that the trailing substring is
1829    /// skipped if empty.
1830    ///
1831    /// [`split`]: str::split
1832    ///
1833    /// This method can be used for string data that is _terminated_,
1834    /// rather than _separated_ by a pattern.
1835    ///
1836    /// # Iterator behavior
1837    ///
1838    /// The returned iterator requires that the pattern supports a
1839    /// reverse search, and it will be double ended if a forward/reverse
1840    /// search yields the same elements.
1841    ///
1842    /// For iterating from the front, the [`split_terminator`] method can be
1843    /// used.
1844    ///
1845    /// [`split_terminator`]: str::split_terminator
1846    ///
1847    /// # Examples
1848    ///
1849    /// ```
1850    /// let v: Vec<&str> = "A.B.".rsplit_terminator('.').collect();
1851    /// assert_eq!(v, ["B", "A"]);
1852    ///
1853    /// let v: Vec<&str> = "A..B..".rsplit_terminator(".").collect();
1854    /// assert_eq!(v, ["", "B", "", "A"]);
1855    ///
1856    /// let v: Vec<&str> = "A.B:C.D".rsplit_terminator(&['.', ':'][..]).collect();
1857    /// assert_eq!(v, ["D", "C", "B", "A"]);
1858    /// ```
1859    #[stable(feature = "rust1", since = "1.0.0")]
1860    #[inline]
1861    pub fn rsplit_terminator<P: Pattern>(&self, pat: P) -> RSplitTerminator<'_, P>
1862    where
1863        for<'a> P::Searcher<'a>: ReverseSearcher<'a>,
1864    {
1865        RSplitTerminator(self.split_terminator(pat).0)
1866    }
1867
1868    /// Returns an iterator over substrings of the given string slice, separated
1869    /// by a pattern, restricted to returning at most `n` items.
1870    ///
1871    /// If `n` substrings are returned, the last substring (the `n`th substring)
1872    /// will contain the remainder of the string.
1873    ///
1874    /// The [pattern] can be a `&str`, [`char`], a slice of [`char`]s, or a
1875    /// function or closure that determines if a character matches.
1876    ///
1877    /// [`char`]: prim@char
1878    /// [pattern]: self::pattern
1879    ///
1880    /// # Iterator behavior
1881    ///
1882    /// The returned iterator will not be double ended, because it is
1883    /// not efficient to support.
1884    ///
1885    /// If the pattern allows a reverse search, the [`rsplitn`] method can be
1886    /// used.
1887    ///
1888    /// [`rsplitn`]: str::rsplitn
1889    ///
1890    /// # Examples
1891    ///
1892    /// Simple patterns:
1893    ///
1894    /// ```
1895    /// let v: Vec<&str> = "Mary had a little lambda".splitn(3, ' ').collect();
1896    /// assert_eq!(v, ["Mary", "had", "a little lambda"]);
1897    ///
1898    /// let v: Vec<&str> = "lionXXtigerXleopard".splitn(3, "X").collect();
1899    /// assert_eq!(v, ["lion", "", "tigerXleopard"]);
1900    ///
1901    /// let v: Vec<&str> = "abcXdef".splitn(1, 'X').collect();
1902    /// assert_eq!(v, ["abcXdef"]);
1903    ///
1904    /// let v: Vec<&str> = "".splitn(1, 'X').collect();
1905    /// assert_eq!(v, [""]);
1906    /// ```
1907    ///
1908    /// A more complex pattern, using a closure:
1909    ///
1910    /// ```
1911    /// let v: Vec<&str> = "abc1defXghi".splitn(2, |c| c == '1' || c == 'X').collect();
1912    /// assert_eq!(v, ["abc", "defXghi"]);
1913    /// ```
1914    #[stable(feature = "rust1", since = "1.0.0")]
1915    #[inline]
1916    pub fn splitn<P: Pattern>(&self, n: usize, pat: P) -> SplitN<'_, P> {
1917        SplitN(SplitNInternal { iter: self.split(pat).0, count: n })
1918    }
1919
1920    /// Returns an iterator over substrings of this string slice, separated by a
1921    /// pattern, starting from the end of the string, restricted to returning at
1922    /// most `n` items.
1923    ///
1924    /// If `n` substrings are returned, the last substring (the `n`th substring)
1925    /// will contain the remainder of the string.
1926    ///
1927    /// The [pattern] can be a `&str`, [`char`], a slice of [`char`]s, or a
1928    /// function or closure that determines if a character matches.
1929    ///
1930    /// [`char`]: prim@char
1931    /// [pattern]: self::pattern
1932    ///
1933    /// # Iterator behavior
1934    ///
1935    /// The returned iterator will not be double ended, because it is not
1936    /// efficient to support.
1937    ///
1938    /// For splitting from the front, the [`splitn`] method can be used.
1939    ///
1940    /// [`splitn`]: str::splitn
1941    ///
1942    /// # Examples
1943    ///
1944    /// Simple patterns:
1945    ///
1946    /// ```
1947    /// let v: Vec<&str> = "Mary had a little lamb".rsplitn(3, ' ').collect();
1948    /// assert_eq!(v, ["lamb", "little", "Mary had a"]);
1949    ///
1950    /// let v: Vec<&str> = "lionXXtigerXleopard".rsplitn(3, 'X').collect();
1951    /// assert_eq!(v, ["leopard", "tiger", "lionX"]);
1952    ///
1953    /// let v: Vec<&str> = "lion::tiger::leopard".rsplitn(2, "::").collect();
1954    /// assert_eq!(v, ["leopard", "lion::tiger"]);
1955    /// ```
1956    ///
1957    /// A more complex pattern, using a closure:
1958    ///
1959    /// ```
1960    /// let v: Vec<&str> = "abc1defXghi".rsplitn(2, |c| c == '1' || c == 'X').collect();
1961    /// assert_eq!(v, ["ghi", "abc1def"]);
1962    /// ```
1963    #[stable(feature = "rust1", since = "1.0.0")]
1964    #[inline]
1965    pub fn rsplitn<P: Pattern>(&self, n: usize, pat: P) -> RSplitN<'_, P>
1966    where
1967        for<'a> P::Searcher<'a>: ReverseSearcher<'a>,
1968    {
1969        RSplitN(self.splitn(n, pat).0)
1970    }
1971
1972    /// Splits the string on the first occurrence of the specified delimiter and
1973    /// returns prefix before delimiter and suffix after delimiter.
1974    ///
1975    /// # Examples
1976    ///
1977    /// ```
1978    /// assert_eq!("cfg".split_once('='), None);
1979    /// assert_eq!("cfg=".split_once('='), Some(("cfg", "")));
1980    /// assert_eq!("cfg=foo".split_once('='), Some(("cfg", "foo")));
1981    /// assert_eq!("cfg=foo=bar".split_once('='), Some(("cfg", "foo=bar")));
1982    /// ```
1983    #[stable(feature = "str_split_once", since = "1.52.0")]
1984    #[inline]
1985    pub fn split_once<P: Pattern>(&self, delimiter: P) -> Option<(&'_ str, &'_ str)> {
1986        let (start, end) = delimiter.into_searcher(self).next_match()?;
1987        // SAFETY: `Searcher` is known to return valid indices.
1988        unsafe { Some((self.get_unchecked(..start), self.get_unchecked(end..))) }
1989    }
1990
1991    /// Splits the string on the last occurrence of the specified delimiter and
1992    /// returns prefix before delimiter and suffix after delimiter.
1993    ///
1994    /// # Examples
1995    ///
1996    /// ```
1997    /// assert_eq!("cfg".rsplit_once('='), None);
1998    /// assert_eq!("cfg=".rsplit_once('='), Some(("cfg", "")));
1999    /// assert_eq!("cfg=foo".rsplit_once('='), Some(("cfg", "foo")));
2000    /// assert_eq!("cfg=foo=bar".rsplit_once('='), Some(("cfg=foo", "bar")));
2001    /// ```
2002    #[stable(feature = "str_split_once", since = "1.52.0")]
2003    #[inline]
2004    pub fn rsplit_once<P: Pattern>(&self, delimiter: P) -> Option<(&'_ str, &'_ str)>
2005    where
2006        for<'a> P::Searcher<'a>: ReverseSearcher<'a>,
2007    {
2008        let (start, end) = delimiter.into_searcher(self).next_match_back()?;
2009        // SAFETY: `Searcher` is known to return valid indices.
2010        unsafe { Some((self.get_unchecked(..start), self.get_unchecked(end..))) }
2011    }
2012
2013    /// Returns an iterator over the disjoint matches of a pattern within the
2014    /// given string slice.
2015    ///
2016    /// The [pattern] can be a `&str`, [`char`], a slice of [`char`]s, or a
2017    /// function or closure that determines if a character matches.
2018    ///
2019    /// [`char`]: prim@char
2020    /// [pattern]: self::pattern
2021    ///
2022    /// # Iterator behavior
2023    ///
2024    /// The returned iterator will be a [`DoubleEndedIterator`] if the pattern
2025    /// allows a reverse search and forward/reverse search yields the same
2026    /// elements. This is true for, e.g., [`char`], but not for `&str`.
2027    ///
2028    /// If the pattern allows a reverse search but its results might differ
2029    /// from a forward search, the [`rmatches`] method can be used.
2030    ///
2031    /// [`rmatches`]: str::rmatches
2032    ///
2033    /// # Examples
2034    ///
2035    /// ```
2036    /// let v: Vec<&str> = "abcXXXabcYYYabc".matches("abc").collect();
2037    /// assert_eq!(v, ["abc", "abc", "abc"]);
2038    ///
2039    /// let v: Vec<&str> = "1abc2abc3".matches(char::is_numeric).collect();
2040    /// assert_eq!(v, ["1", "2", "3"]);
2041    /// ```
2042    #[stable(feature = "str_matches", since = "1.2.0")]
2043    #[inline]
2044    pub fn matches<P: Pattern>(&self, pat: P) -> Matches<'_, P> {
2045        Matches(MatchesInternal(pat.into_searcher(self)))
2046    }
2047
2048    /// Returns an iterator over the disjoint matches of a pattern within this
2049    /// string slice, yielded in reverse order.
2050    ///
2051    /// The [pattern] can be a `&str`, [`char`], a slice of [`char`]s, or a
2052    /// function or closure that determines if a character matches.
2053    ///
2054    /// [`char`]: prim@char
2055    /// [pattern]: self::pattern
2056    ///
2057    /// # Iterator behavior
2058    ///
2059    /// The returned iterator requires that the pattern supports a reverse
2060    /// search, and it will be a [`DoubleEndedIterator`] if a forward/reverse
2061    /// search yields the same elements.
2062    ///
2063    /// For iterating from the front, the [`matches`] method can be used.
2064    ///
2065    /// [`matches`]: str::matches
2066    ///
2067    /// # Examples
2068    ///
2069    /// ```
2070    /// let v: Vec<&str> = "abcXXXabcYYYabc".rmatches("abc").collect();
2071    /// assert_eq!(v, ["abc", "abc", "abc"]);
2072    ///
2073    /// let v: Vec<&str> = "1abc2abc3".rmatches(char::is_numeric).collect();
2074    /// assert_eq!(v, ["3", "2", "1"]);
2075    /// ```
2076    #[stable(feature = "str_matches", since = "1.2.0")]
2077    #[inline]
2078    pub fn rmatches<P: Pattern>(&self, pat: P) -> RMatches<'_, P>
2079    where
2080        for<'a> P::Searcher<'a>: ReverseSearcher<'a>,
2081    {
2082        RMatches(self.matches(pat).0)
2083    }
2084
2085    /// Returns an iterator over the disjoint matches of a pattern within this string
2086    /// slice as well as the index that the match starts at.
2087    ///
2088    /// For matches of `pat` within `self` that overlap, only the indices
2089    /// corresponding to the first match are returned.
2090    ///
2091    /// The [pattern] can be a `&str`, [`char`], a slice of [`char`]s, or a
2092    /// function or closure that determines if a character matches.
2093    ///
2094    /// [`char`]: prim@char
2095    /// [pattern]: self::pattern
2096    ///
2097    /// # Iterator behavior
2098    ///
2099    /// The returned iterator will be a [`DoubleEndedIterator`] if the pattern
2100    /// allows a reverse search and forward/reverse search yields the same
2101    /// elements. This is true for, e.g., [`char`], but not for `&str`.
2102    ///
2103    /// If the pattern allows a reverse search but its results might differ
2104    /// from a forward search, the [`rmatch_indices`] method can be used.
2105    ///
2106    /// [`rmatch_indices`]: str::rmatch_indices
2107    ///
2108    /// # Examples
2109    ///
2110    /// ```
2111    /// let v: Vec<_> = "abcXXXabcYYYabc".match_indices("abc").collect();
2112    /// assert_eq!(v, [(0, "abc"), (6, "abc"), (12, "abc")]);
2113    ///
2114    /// let v: Vec<_> = "1abcabc2".match_indices("abc").collect();
2115    /// assert_eq!(v, [(1, "abc"), (4, "abc")]);
2116    ///
2117    /// let v: Vec<_> = "ababa".match_indices("aba").collect();
2118    /// assert_eq!(v, [(0, "aba")]); // only the first `aba`
2119    /// ```
2120    #[stable(feature = "str_match_indices", since = "1.5.0")]
2121    #[inline]
2122    pub fn match_indices<P: Pattern>(&self, pat: P) -> MatchIndices<'_, P> {
2123        MatchIndices(MatchIndicesInternal(pat.into_searcher(self)))
2124    }
2125
2126    /// Returns an iterator over the disjoint matches of a pattern within `self`,
2127    /// yielded in reverse order along with the index of the match.
2128    ///
2129    /// For matches of `pat` within `self` that overlap, only the indices
2130    /// corresponding to the last match are returned.
2131    ///
2132    /// The [pattern] can be a `&str`, [`char`], a slice of [`char`]s, or a
2133    /// function or closure that determines if a character matches.
2134    ///
2135    /// [`char`]: prim@char
2136    /// [pattern]: self::pattern
2137    ///
2138    /// # Iterator behavior
2139    ///
2140    /// The returned iterator requires that the pattern supports a reverse
2141    /// search, and it will be a [`DoubleEndedIterator`] if a forward/reverse
2142    /// search yields the same elements.
2143    ///
2144    /// For iterating from the front, the [`match_indices`] method can be used.
2145    ///
2146    /// [`match_indices`]: str::match_indices
2147    ///
2148    /// # Examples
2149    ///
2150    /// ```
2151    /// let v: Vec<_> = "abcXXXabcYYYabc".rmatch_indices("abc").collect();
2152    /// assert_eq!(v, [(12, "abc"), (6, "abc"), (0, "abc")]);
2153    ///
2154    /// let v: Vec<_> = "1abcabc2".rmatch_indices("abc").collect();
2155    /// assert_eq!(v, [(4, "abc"), (1, "abc")]);
2156    ///
2157    /// let v: Vec<_> = "ababa".rmatch_indices("aba").collect();
2158    /// assert_eq!(v, [(2, "aba")]); // only the last `aba`
2159    /// ```
2160    #[stable(feature = "str_match_indices", since = "1.5.0")]
2161    #[inline]
2162    pub fn rmatch_indices<P: Pattern>(&self, pat: P) -> RMatchIndices<'_, P>
2163    where
2164        for<'a> P::Searcher<'a>: ReverseSearcher<'a>,
2165    {
2166        RMatchIndices(self.match_indices(pat).0)
2167    }
2168
2169    /// Returns a string slice with leading and trailing whitespace removed.
2170    ///
2171    /// 'Whitespace' is defined according to the terms of the Unicode Derived
2172    /// Core Property `White_Space`, which includes newlines.
2173    ///
2174    /// # Examples
2175    ///
2176    /// ```
2177    /// let s = "\n Hello\tworld\t\n";
2178    ///
2179    /// assert_eq!("Hello\tworld", s.trim());
2180    /// ```
2181    #[inline]
2182    #[must_use = "this returns the trimmed string as a slice, \
2183                  without modifying the original"]
2184    #[stable(feature = "rust1", since = "1.0.0")]
2185    #[rustc_diagnostic_item = "str_trim"]
2186    pub fn trim(&self) -> &str {
2187        self.trim_matches(char::is_whitespace)
2188    }
2189
2190    /// Returns a string slice with leading whitespace removed.
2191    ///
2192    /// 'Whitespace' is defined according to the terms of the Unicode Derived
2193    /// Core Property `White_Space`, which includes newlines.
2194    ///
2195    /// # Text directionality
2196    ///
2197    /// A string is a sequence of bytes. `start` in this context means the first
2198    /// position of that byte string; for a left-to-right language like English or
2199    /// Russian, this will be left side, and for right-to-left languages like
2200    /// Arabic or Hebrew, this will be the right side.
2201    ///
2202    /// # Examples
2203    ///
2204    /// Basic usage:
2205    ///
2206    /// ```
2207    /// let s = "\n Hello\tworld\t\n";
2208    /// assert_eq!("Hello\tworld\t\n", s.trim_start());
2209    /// ```
2210    ///
2211    /// Directionality:
2212    ///
2213    /// ```
2214    /// let s = "  English  ";
2215    /// assert!(Some('E') == s.trim_start().chars().next());
2216    ///
2217    /// let s = "  עברית  ";
2218    /// assert!(Some('ע') == s.trim_start().chars().next());
2219    /// ```
2220    #[inline]
2221    #[must_use = "this returns the trimmed string as a new slice, \
2222                  without modifying the original"]
2223    #[stable(feature = "trim_direction", since = "1.30.0")]
2224    #[rustc_diagnostic_item = "str_trim_start"]
2225    pub fn trim_start(&self) -> &str {
2226        self.trim_start_matches(char::is_whitespace)
2227    }
2228
2229    /// Returns a string slice with trailing whitespace removed.
2230    ///
2231    /// 'Whitespace' is defined according to the terms of the Unicode Derived
2232    /// Core Property `White_Space`, which includes newlines.
2233    ///
2234    /// # Text directionality
2235    ///
2236    /// A string is a sequence of bytes. `end` in this context means the last
2237    /// position of that byte string; for a left-to-right language like English or
2238    /// Russian, this will be right side, and for right-to-left languages like
2239    /// Arabic or Hebrew, this will be the left side.
2240    ///
2241    /// # Examples
2242    ///
2243    /// Basic usage:
2244    ///
2245    /// ```
2246    /// let s = "\n Hello\tworld\t\n";
2247    /// assert_eq!("\n Hello\tworld", s.trim_end());
2248    /// ```
2249    ///
2250    /// Directionality:
2251    ///
2252    /// ```
2253    /// let s = "  English  ";
2254    /// assert!(Some('h') == s.trim_end().chars().rev().next());
2255    ///
2256    /// let s = "  עברית  ";
2257    /// assert!(Some('ת') == s.trim_end().chars().rev().next());
2258    /// ```
2259    #[inline]
2260    #[must_use = "this returns the trimmed string as a new slice, \
2261                  without modifying the original"]
2262    #[stable(feature = "trim_direction", since = "1.30.0")]
2263    #[rustc_diagnostic_item = "str_trim_end"]
2264    pub fn trim_end(&self) -> &str {
2265        self.trim_end_matches(char::is_whitespace)
2266    }
2267
2268    /// Returns a string slice with leading whitespace removed.
2269    ///
2270    /// 'Whitespace' is defined according to the terms of the Unicode Derived
2271    /// Core Property `White_Space`.
2272    ///
2273    /// # Text directionality
2274    ///
2275    /// A string is a sequence of bytes. 'Left' in this context means the first
2276    /// position of that byte string; for a language like Arabic or Hebrew
2277    /// which are 'right to left' rather than 'left to right', this will be
2278    /// the _right_ side, not the left.
2279    ///
2280    /// # Examples
2281    ///
2282    /// Basic usage:
2283    ///
2284    /// ```
2285    /// let s = " Hello\tworld\t";
2286    ///
2287    /// assert_eq!("Hello\tworld\t", s.trim_left());
2288    /// ```
2289    ///
2290    /// Directionality:
2291    ///
2292    /// ```
2293    /// let s = "  English";
2294    /// assert!(Some('E') == s.trim_left().chars().next());
2295    ///
2296    /// let s = "  עברית";
2297    /// assert!(Some('ע') == s.trim_left().chars().next());
2298    /// ```
2299    #[must_use = "this returns the trimmed string as a new slice, \
2300                  without modifying the original"]
2301    #[inline]
2302    #[stable(feature = "rust1", since = "1.0.0")]
2303    #[deprecated(since = "1.33.0", note = "superseded by `trim_start`", suggestion = "trim_start")]
2304    pub fn trim_left(&self) -> &str {
2305        self.trim_start()
2306    }
2307
2308    /// Returns a string slice with trailing whitespace removed.
2309    ///
2310    /// 'Whitespace' is defined according to the terms of the Unicode Derived
2311    /// Core Property `White_Space`.
2312    ///
2313    /// # Text directionality
2314    ///
2315    /// A string is a sequence of bytes. 'Right' in this context means the last
2316    /// position of that byte string; for a language like Arabic or Hebrew
2317    /// which are 'right to left' rather than 'left to right', this will be
2318    /// the _left_ side, not the right.
2319    ///
2320    /// # Examples
2321    ///
2322    /// Basic usage:
2323    ///
2324    /// ```
2325    /// let s = " Hello\tworld\t";
2326    ///
2327    /// assert_eq!(" Hello\tworld", s.trim_right());
2328    /// ```
2329    ///
2330    /// Directionality:
2331    ///
2332    /// ```
2333    /// let s = "English  ";
2334    /// assert!(Some('h') == s.trim_right().chars().rev().next());
2335    ///
2336    /// let s = "עברית  ";
2337    /// assert!(Some('ת') == s.trim_right().chars().rev().next());
2338    /// ```
2339    #[must_use = "this returns the trimmed string as a new slice, \
2340                  without modifying the original"]
2341    #[inline]
2342    #[stable(feature = "rust1", since = "1.0.0")]
2343    #[deprecated(since = "1.33.0", note = "superseded by `trim_end`", suggestion = "trim_end")]
2344    pub fn trim_right(&self) -> &str {
2345        self.trim_end()
2346    }
2347
2348    /// Returns a string slice with all prefixes and suffixes that match a
2349    /// pattern repeatedly removed.
2350    ///
2351    /// The [pattern] can be a [`char`], a slice of [`char`]s, or a function
2352    /// or closure that determines if a character matches.
2353    ///
2354    /// [`char`]: prim@char
2355    /// [pattern]: self::pattern
2356    ///
2357    /// # Examples
2358    ///
2359    /// Simple patterns:
2360    ///
2361    /// ```
2362    /// assert_eq!("11foo1bar11".trim_matches('1'), "foo1bar");
2363    /// assert_eq!("123foo1bar123".trim_matches(char::is_numeric), "foo1bar");
2364    ///
2365    /// let x: &[_] = &['1', '2'];
2366    /// assert_eq!("12foo1bar12".trim_matches(x), "foo1bar");
2367    /// ```
2368    ///
2369    /// A more complex pattern, using a closure:
2370    ///
2371    /// ```
2372    /// assert_eq!("1foo1barXX".trim_matches(|c| c == '1' || c == 'X'), "foo1bar");
2373    /// ```
2374    #[must_use = "this returns the trimmed string as a new slice, \
2375                  without modifying the original"]
2376    #[stable(feature = "rust1", since = "1.0.0")]
2377    pub fn trim_matches<P: Pattern>(&self, pat: P) -> &str
2378    where
2379        for<'a> P::Searcher<'a>: DoubleEndedSearcher<'a>,
2380    {
2381        let mut i = 0;
2382        let mut j = 0;
2383        let mut matcher = pat.into_searcher(self);
2384        if let Some((a, b)) = matcher.next_reject() {
2385            i = a;
2386            j = b; // Remember earliest known match, correct it below if
2387            // last match is different
2388        }
2389        if let Some((_, b)) = matcher.next_reject_back() {
2390            j = b;
2391        }
2392        // SAFETY: `Searcher` is known to return valid indices.
2393        unsafe { self.get_unchecked(i..j) }
2394    }
2395
2396    /// Returns a string slice with all prefixes that match a pattern
2397    /// repeatedly removed.
2398    ///
2399    /// The [pattern] can be a `&str`, [`char`], a slice of [`char`]s, or a
2400    /// function or closure that determines if a character matches.
2401    ///
2402    /// [`char`]: prim@char
2403    /// [pattern]: self::pattern
2404    ///
2405    /// # Text directionality
2406    ///
2407    /// A string is a sequence of bytes. `start` in this context means the first
2408    /// position of that byte string; for a left-to-right language like English or
2409    /// Russian, this will be left side, and for right-to-left languages like
2410    /// Arabic or Hebrew, this will be the right side.
2411    ///
2412    /// # Examples
2413    ///
2414    /// ```
2415    /// assert_eq!("11foo1bar11".trim_start_matches('1'), "foo1bar11");
2416    /// assert_eq!("123foo1bar123".trim_start_matches(char::is_numeric), "foo1bar123");
2417    ///
2418    /// let x: &[_] = &['1', '2'];
2419    /// assert_eq!("12foo1bar12".trim_start_matches(x), "foo1bar12");
2420    /// ```
2421    #[must_use = "this returns the trimmed string as a new slice, \
2422                  without modifying the original"]
2423    #[stable(feature = "trim_direction", since = "1.30.0")]
2424    pub fn trim_start_matches<P: Pattern>(&self, pat: P) -> &str {
2425        let mut i = self.len();
2426        let mut matcher = pat.into_searcher(self);
2427        if let Some((a, _)) = matcher.next_reject() {
2428            i = a;
2429        }
2430        // SAFETY: `Searcher` is known to return valid indices.
2431        unsafe { self.get_unchecked(i..self.len()) }
2432    }
2433
2434    /// Returns a string slice with the prefix removed.
2435    ///
2436    /// If the string starts with the pattern `prefix`, returns the substring after the prefix,
2437    /// wrapped in `Some`. Unlike [`trim_start_matches`], this method removes the prefix exactly once.
2438    ///
2439    /// If the string does not start with `prefix`, returns `None`.
2440    ///
2441    /// The [pattern] can be a `&str`, [`char`], a slice of [`char`]s, or a
2442    /// function or closure that determines if a character matches.
2443    ///
2444    /// [`char`]: prim@char
2445    /// [pattern]: self::pattern
2446    /// [`trim_start_matches`]: Self::trim_start_matches
2447    ///
2448    /// # Examples
2449    ///
2450    /// ```
2451    /// assert_eq!("foo:bar".strip_prefix("foo:"), Some("bar"));
2452    /// assert_eq!("foo:bar".strip_prefix("bar"), None);
2453    /// assert_eq!("foofoo".strip_prefix("foo"), Some("foo"));
2454    /// ```
2455    #[must_use = "this returns the remaining substring as a new slice, \
2456                  without modifying the original"]
2457    #[stable(feature = "str_strip", since = "1.45.0")]
2458    pub fn strip_prefix<P: Pattern>(&self, prefix: P) -> Option<&str> {
2459        prefix.strip_prefix_of(self)
2460    }
2461
2462    /// Returns a string slice with the suffix removed.
2463    ///
2464    /// If the string ends with the pattern `suffix`, returns the substring before the suffix,
2465    /// wrapped in `Some`.  Unlike [`trim_end_matches`], this method removes the suffix exactly once.
2466    ///
2467    /// If the string does not end with `suffix`, returns `None`.
2468    ///
2469    /// The [pattern] can be a `&str`, [`char`], a slice of [`char`]s, or a
2470    /// function or closure that determines if a character matches.
2471    ///
2472    /// [`char`]: prim@char
2473    /// [pattern]: self::pattern
2474    /// [`trim_end_matches`]: Self::trim_end_matches
2475    ///
2476    /// # Examples
2477    ///
2478    /// ```
2479    /// assert_eq!("bar:foo".strip_suffix(":foo"), Some("bar"));
2480    /// assert_eq!("bar:foo".strip_suffix("bar"), None);
2481    /// assert_eq!("foofoo".strip_suffix("foo"), Some("foo"));
2482    /// ```
2483    #[must_use = "this returns the remaining substring as a new slice, \
2484                  without modifying the original"]
2485    #[stable(feature = "str_strip", since = "1.45.0")]
2486    pub fn strip_suffix<P: Pattern>(&self, suffix: P) -> Option<&str>
2487    where
2488        for<'a> P::Searcher<'a>: ReverseSearcher<'a>,
2489    {
2490        suffix.strip_suffix_of(self)
2491    }
2492
2493    /// Returns a string slice with the prefix and suffix removed.
2494    ///
2495    /// If the string starts with the pattern `prefix` and ends with the pattern `suffix`, returns
2496    /// the substring after the prefix and before the suffix, wrapped in `Some`.
2497    /// Unlike [`trim_start_matches`] and [`trim_end_matches`], this method removes both the prefix
2498    /// and suffix exactly once.
2499    ///
2500    /// If the string does not start with `prefix` or does not end with `suffix`, returns `None`.
2501    ///
2502    /// Each [pattern] can be a `&str`, [`char`], a slice of [`char`]s, or a
2503    /// function or closure that determines if a character matches.
2504    ///
2505    /// [`char`]: prim@char
2506    /// [pattern]: self::pattern
2507    /// [`trim_start_matches`]: Self::trim_start_matches
2508    /// [`trim_end_matches`]: Self::trim_end_matches
2509    ///
2510    /// # Examples
2511    ///
2512    /// ```
2513    /// #![feature(strip_circumfix)]
2514    ///
2515    /// assert_eq!("bar:hello:foo".strip_circumfix("bar:", ":foo"), Some("hello"));
2516    /// assert_eq!("bar:foo".strip_circumfix("foo", "foo"), None);
2517    /// assert_eq!("foo:bar;".strip_circumfix("foo:", ';'), Some("bar"));
2518    /// ```
2519    #[must_use = "this returns the remaining substring as a new slice, \
2520                  without modifying the original"]
2521    #[unstable(feature = "strip_circumfix", issue = "147946")]
2522    pub fn strip_circumfix<P: Pattern, S: Pattern>(&self, prefix: P, suffix: S) -> Option<&str>
2523    where
2524        for<'a> S::Searcher<'a>: ReverseSearcher<'a>,
2525    {
2526        self.strip_prefix(prefix)?.strip_suffix(suffix)
2527    }
2528
2529    /// Returns a string slice with the optional prefix removed.
2530    ///
2531    /// If the string starts with the pattern `prefix`, returns the substring after the prefix.
2532    /// Unlike [`strip_prefix`], this method always returns `&str` for easy method chaining,
2533    /// instead of returning [`Option<&str>`].
2534    ///
2535    /// If the string does not start with `prefix`, returns the original string unchanged.
2536    ///
2537    /// The [pattern] can be a `&str`, [`char`], a slice of [`char`]s, or a
2538    /// function or closure that determines if a character matches.
2539    ///
2540    /// [`char`]: prim@char
2541    /// [pattern]: self::pattern
2542    /// [`strip_prefix`]: Self::strip_prefix
2543    ///
2544    /// # Examples
2545    ///
2546    /// ```
2547    /// #![feature(trim_prefix_suffix)]
2548    ///
2549    /// // Prefix present - removes it
2550    /// assert_eq!("foo:bar".trim_prefix("foo:"), "bar");
2551    /// assert_eq!("foofoo".trim_prefix("foo"), "foo");
2552    ///
2553    /// // Prefix absent - returns original string
2554    /// assert_eq!("foo:bar".trim_prefix("bar"), "foo:bar");
2555    ///
2556    /// // Method chaining example
2557    /// assert_eq!("<https://example.com/>".trim_prefix('<').trim_suffix('>'), "https://example.com/");
2558    /// ```
2559    #[must_use = "this returns the remaining substring as a new slice, \
2560                  without modifying the original"]
2561    #[unstable(feature = "trim_prefix_suffix", issue = "142312")]
2562    pub fn trim_prefix<P: Pattern>(&self, prefix: P) -> &str {
2563        prefix.strip_prefix_of(self).unwrap_or(self)
2564    }
2565
2566    /// Returns a string slice with the optional suffix removed.
2567    ///
2568    /// If the string ends with the pattern `suffix`, returns the substring before the suffix.
2569    /// Unlike [`strip_suffix`], this method always returns `&str` for easy method chaining,
2570    /// instead of returning [`Option<&str>`].
2571    ///
2572    /// If the string does not end with `suffix`, returns the original string unchanged.
2573    ///
2574    /// The [pattern] can be a `&str`, [`char`], a slice of [`char`]s, or a
2575    /// function or closure that determines if a character matches.
2576    ///
2577    /// [`char`]: prim@char
2578    /// [pattern]: self::pattern
2579    /// [`strip_suffix`]: Self::strip_suffix
2580    ///
2581    /// # Examples
2582    ///
2583    /// ```
2584    /// #![feature(trim_prefix_suffix)]
2585    ///
2586    /// // Suffix present - removes it
2587    /// assert_eq!("bar:foo".trim_suffix(":foo"), "bar");
2588    /// assert_eq!("foofoo".trim_suffix("foo"), "foo");
2589    ///
2590    /// // Suffix absent - returns original string
2591    /// assert_eq!("bar:foo".trim_suffix("bar"), "bar:foo");
2592    ///
2593    /// // Method chaining example
2594    /// assert_eq!("<https://example.com/>".trim_prefix('<').trim_suffix('>'), "https://example.com/");
2595    /// ```
2596    #[must_use = "this returns the remaining substring as a new slice, \
2597                  without modifying the original"]
2598    #[unstable(feature = "trim_prefix_suffix", issue = "142312")]
2599    pub fn trim_suffix<P: Pattern>(&self, suffix: P) -> &str
2600    where
2601        for<'a> P::Searcher<'a>: ReverseSearcher<'a>,
2602    {
2603        suffix.strip_suffix_of(self).unwrap_or(self)
2604    }
2605
2606    /// Returns a string slice with all suffixes that match a pattern
2607    /// repeatedly removed.
2608    ///
2609    /// The [pattern] can be a `&str`, [`char`], a slice of [`char`]s, or a
2610    /// function or closure that determines if a character matches.
2611    ///
2612    /// [`char`]: prim@char
2613    /// [pattern]: self::pattern
2614    ///
2615    /// # Text directionality
2616    ///
2617    /// A string is a sequence of bytes. `end` in this context means the last
2618    /// position of that byte string; for a left-to-right language like English or
2619    /// Russian, this will be right side, and for right-to-left languages like
2620    /// Arabic or Hebrew, this will be the left side.
2621    ///
2622    /// # Examples
2623    ///
2624    /// Simple patterns:
2625    ///
2626    /// ```
2627    /// assert_eq!("11foo1bar11".trim_end_matches('1'), "11foo1bar");
2628    /// assert_eq!("123foo1bar123".trim_end_matches(char::is_numeric), "123foo1bar");
2629    ///
2630    /// let x: &[_] = &['1', '2'];
2631    /// assert_eq!("12foo1bar12".trim_end_matches(x), "12foo1bar");
2632    /// ```
2633    ///
2634    /// A more complex pattern, using a closure:
2635    ///
2636    /// ```
2637    /// assert_eq!("1fooX".trim_end_matches(|c| c == '1' || c == 'X'), "1foo");
2638    /// ```
2639    #[must_use = "this returns the trimmed string as a new slice, \
2640                  without modifying the original"]
2641    #[stable(feature = "trim_direction", since = "1.30.0")]
2642    pub fn trim_end_matches<P: Pattern>(&self, pat: P) -> &str
2643    where
2644        for<'a> P::Searcher<'a>: ReverseSearcher<'a>,
2645    {
2646        let mut j = 0;
2647        let mut matcher = pat.into_searcher(self);
2648        if let Some((_, b)) = matcher.next_reject_back() {
2649            j = b;
2650        }
2651        // SAFETY: `Searcher` is known to return valid indices.
2652        unsafe { self.get_unchecked(0..j) }
2653    }
2654
2655    /// Returns a string slice with all prefixes that match a pattern
2656    /// repeatedly removed.
2657    ///
2658    /// The [pattern] can be a `&str`, [`char`], a slice of [`char`]s, or a
2659    /// function or closure that determines if a character matches.
2660    ///
2661    /// [`char`]: prim@char
2662    /// [pattern]: self::pattern
2663    ///
2664    /// # Text directionality
2665    ///
2666    /// A string is a sequence of bytes. 'Left' in this context means the first
2667    /// position of that byte string; for a language like Arabic or Hebrew
2668    /// which are 'right to left' rather than 'left to right', this will be
2669    /// the _right_ side, not the left.
2670    ///
2671    /// # Examples
2672    ///
2673    /// ```
2674    /// assert_eq!("11foo1bar11".trim_left_matches('1'), "foo1bar11");
2675    /// assert_eq!("123foo1bar123".trim_left_matches(char::is_numeric), "foo1bar123");
2676    ///
2677    /// let x: &[_] = &['1', '2'];
2678    /// assert_eq!("12foo1bar12".trim_left_matches(x), "foo1bar12");
2679    /// ```
2680    #[stable(feature = "rust1", since = "1.0.0")]
2681    #[deprecated(
2682        since = "1.33.0",
2683        note = "superseded by `trim_start_matches`",
2684        suggestion = "trim_start_matches"
2685    )]
2686    pub fn trim_left_matches<P: Pattern>(&self, pat: P) -> &str {
2687        self.trim_start_matches(pat)
2688    }
2689
2690    /// Returns a string slice with all suffixes that match a pattern
2691    /// repeatedly removed.
2692    ///
2693    /// The [pattern] can be a `&str`, [`char`], a slice of [`char`]s, or a
2694    /// function or closure that determines if a character matches.
2695    ///
2696    /// [`char`]: prim@char
2697    /// [pattern]: self::pattern
2698    ///
2699    /// # Text directionality
2700    ///
2701    /// A string is a sequence of bytes. 'Right' in this context means the last
2702    /// position of that byte string; for a language like Arabic or Hebrew
2703    /// which are 'right to left' rather than 'left to right', this will be
2704    /// the _left_ side, not the right.
2705    ///
2706    /// # Examples
2707    ///
2708    /// Simple patterns:
2709    ///
2710    /// ```
2711    /// assert_eq!("11foo1bar11".trim_right_matches('1'), "11foo1bar");
2712    /// assert_eq!("123foo1bar123".trim_right_matches(char::is_numeric), "123foo1bar");
2713    ///
2714    /// let x: &[_] = &['1', '2'];
2715    /// assert_eq!("12foo1bar12".trim_right_matches(x), "12foo1bar");
2716    /// ```
2717    ///
2718    /// A more complex pattern, using a closure:
2719    ///
2720    /// ```
2721    /// assert_eq!("1fooX".trim_right_matches(|c| c == '1' || c == 'X'), "1foo");
2722    /// ```
2723    #[stable(feature = "rust1", since = "1.0.0")]
2724    #[deprecated(
2725        since = "1.33.0",
2726        note = "superseded by `trim_end_matches`",
2727        suggestion = "trim_end_matches"
2728    )]
2729    pub fn trim_right_matches<P: Pattern>(&self, pat: P) -> &str
2730    where
2731        for<'a> P::Searcher<'a>: ReverseSearcher<'a>,
2732    {
2733        self.trim_end_matches(pat)
2734    }
2735
2736    /// Parses this string slice into another type.
2737    ///
2738    /// Because `parse` is so general, it can cause problems with type
2739    /// inference. As such, `parse` is one of the few times you'll see
2740    /// the syntax affectionately known as the 'turbofish': `::<>`. This
2741    /// helps the inference algorithm understand specifically which type
2742    /// you're trying to parse into.
2743    ///
2744    /// `parse` can parse into any type that implements the [`FromStr`] trait.
2745    ///
2746    /// # Errors
2747    ///
2748    /// Will return [`Err`] if it's not possible to parse this string slice into
2749    /// the desired type.
2750    ///
2751    /// [`Err`]: FromStr::Err
2752    ///
2753    /// # Examples
2754    ///
2755    /// Basic usage:
2756    ///
2757    /// ```
2758    /// let four: u32 = "4".parse().unwrap();
2759    ///
2760    /// assert_eq!(4, four);
2761    /// ```
2762    ///
2763    /// Using the 'turbofish' instead of annotating `four`:
2764    ///
2765    /// ```
2766    /// let four = "4".parse::<u32>();
2767    ///
2768    /// assert_eq!(Ok(4), four);
2769    /// ```
2770    ///
2771    /// Failing to parse:
2772    ///
2773    /// ```
2774    /// let nope = "j".parse::<u32>();
2775    ///
2776    /// assert!(nope.is_err());
2777    /// ```
2778    #[inline]
2779    #[stable(feature = "rust1", since = "1.0.0")]
2780    pub fn parse<F: FromStr>(&self) -> Result<F, F::Err> {
2781        FromStr::from_str(self)
2782    }
2783
2784    /// Checks if all characters in this string are within the ASCII range.
2785    ///
2786    /// An empty string returns `true`.
2787    ///
2788    /// # Examples
2789    ///
2790    /// ```
2791    /// let ascii = "hello!\n";
2792    /// let non_ascii = "Grüße, Jürgen ❤";
2793    ///
2794    /// assert!(ascii.is_ascii());
2795    /// assert!(!non_ascii.is_ascii());
2796    /// ```
2797    #[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
2798    #[rustc_const_stable(feature = "const_slice_is_ascii", since = "1.74.0")]
2799    #[must_use]
2800    #[inline]
2801    pub const fn is_ascii(&self) -> bool {
2802        // We can treat each byte as character here: all multibyte characters
2803        // start with a byte that is not in the ASCII range, so we will stop
2804        // there already.
2805        self.as_bytes().is_ascii()
2806    }
2807
2808    /// If this string slice [`is_ascii`](Self::is_ascii), returns it as a slice
2809    /// of [ASCII characters](`ascii::Char`), otherwise returns `None`.
2810    #[unstable(feature = "ascii_char", issue = "110998")]
2811    #[must_use]
2812    #[inline]
2813    pub const fn as_ascii(&self) -> Option<&[ascii::Char]> {
2814        // Like in `is_ascii`, we can work on the bytes directly.
2815        self.as_bytes().as_ascii()
2816    }
2817
2818    /// Converts this string slice into a slice of [ASCII characters](ascii::Char),
2819    /// without checking whether they are valid.
2820    ///
2821    /// # Safety
2822    ///
2823    /// Every character in this string must be ASCII, or else this is UB.
2824    #[unstable(feature = "ascii_char", issue = "110998")]
2825    #[must_use]
2826    #[inline]
2827    pub const unsafe fn as_ascii_unchecked(&self) -> &[ascii::Char] {
2828        assert_unsafe_precondition!(
2829            check_library_ub,
2830            "as_ascii_unchecked requires that the string is valid ASCII",
2831            (it: &str = self) => it.is_ascii()
2832        );
2833
2834        // SAFETY: the caller promised that every byte of this string slice
2835        // is ASCII.
2836        unsafe { self.as_bytes().as_ascii_unchecked() }
2837    }
2838
2839    /// Checks that two strings are an ASCII case-insensitive match.
2840    ///
2841    /// Same as `to_ascii_lowercase(a) == to_ascii_lowercase(b)`,
2842    /// but without allocating and copying temporaries.
2843    ///
2844    /// For Unicode-aware case-insensitive matching, consider
2845    /// [`str::eq_ignore_case_unnormalized`].
2846    ///
2847    /// # Examples
2848    ///
2849    /// ```
2850    /// assert!("Ferris".eq_ignore_ascii_case("FERRIS"));
2851    /// assert!("Ferrös".eq_ignore_ascii_case("FERRöS"));
2852    /// assert!(!"Ferrös".eq_ignore_ascii_case("FERRÖS"));
2853    /// ```
2854    #[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
2855    #[rustc_const_stable(feature = "const_eq_ignore_ascii_case", since = "1.89.0")]
2856    #[must_use]
2857    #[inline]
2858    pub const fn eq_ignore_ascii_case(&self, other: &str) -> bool {
2859        self.as_bytes().eq_ignore_ascii_case(other.as_bytes())
2860    }
2861
2862    /// Checks that two strings are a caseless match, according to
2863    /// [Definition 144] in Chapter 3 of the Unicode Standard.
2864    ///
2865    /// [Definition 144]: https://www.unicode.org/versions/latest/core-spec/chapter-3/#G53513
2866    ///
2867    /// Same as `a.to_casefold_unnormalized() == b.to_casefold_unnormalized()`,
2868    /// but without allocating. See that method's documentation,
2869    /// as well as [`char::to_casefold_unnormalized()`],
2870    /// for more information about case folding.
2871    ///
2872    /// No [normalization] (e.g. NFC) is performed, so visually and semantically identical strings
2873    /// might still compare unequal. For example, `"Å"` (U+00C5 LATIN CAPITAL LETTER A WITH RING ABOVE)
2874    /// is considered distinct from `"Å"` (A followed by U+030A COMBINING RING ABOVE),
2875    /// even though Unicode considers them canonically equivalent.
2876    ///
2877    /// In addition, this method is independent of language/locale,
2878    /// so the special behavior of I/ı/İ/i in Turkish and Azeri is not handled.
2879    ///
2880    /// # Examples
2881    ///
2882    /// ```
2883    /// #![feature(casefold)]
2884    /// assert!("Ferris".eq_ignore_case_unnormalized("FERRIS"));
2885    /// assert!("Ferrös".eq_ignore_case_unnormalized("FERRÖS"));
2886    /// assert!("ẞ".eq_ignore_case_unnormalized("ss"));
2887    /// ```
2888    ///
2889    /// No NFC [normalization] is performed:
2890    ///
2891    /// ```rust
2892    /// #![feature(casefold)]
2893    /// // These two strings are visually and semantically identical...
2894    /// let comp = "Å";
2895    /// let decomp = "Å";
2896    ///
2897    /// // ... but not codepoint-for-codepoint equal.
2898    /// assert_eq!(comp, "\u{C5}");
2899    /// assert_eq!(decomp, "A\u{030A}");
2900    ///
2901    /// // Their case-foldings are likewise unequal:
2902    /// assert!(!comp.eq_ignore_case_unnormalized(decomp));
2903    /// ```
2904    ///
2905    /// [normalization]: https://www.unicode.org/faq/normalization
2906    #[unstable(feature = "casefold", issue = "154742")]
2907    #[must_use]
2908    #[inline]
2909    pub fn eq_ignore_case_unnormalized(&self, other: &str) -> bool {
2910        self.chars()
2911            .flat_map(char::to_casefold_unnormalized)
2912            .eq(other.chars().flat_map(char::to_casefold_unnormalized))
2913    }
2914
2915    /// Converts this string to its ASCII upper case equivalent in-place.
2916    ///
2917    /// ASCII letters 'a' to 'z' are mapped to 'A' to 'Z',
2918    /// but non-ASCII letters are unchanged.
2919    ///
2920    /// To return a new uppercased value without modifying the existing one, use
2921    /// [`to_ascii_uppercase()`].
2922    ///
2923    /// [`to_ascii_uppercase()`]: #method.to_ascii_uppercase
2924    ///
2925    /// # Examples
2926    ///
2927    /// ```
2928    /// let mut s = String::from("Grüße, Jürgen ❤");
2929    ///
2930    /// s.make_ascii_uppercase();
2931    ///
2932    /// assert_eq!("GRüßE, JüRGEN ❤", s);
2933    /// ```
2934    #[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
2935    #[rustc_const_stable(feature = "const_make_ascii", since = "1.84.0")]
2936    #[inline]
2937    pub const fn make_ascii_uppercase(&mut self) {
2938        // SAFETY: changing ASCII letters only does not invalidate UTF-8.
2939        let me = unsafe { self.as_bytes_mut() };
2940        me.make_ascii_uppercase()
2941    }
2942
2943    /// Converts this string to its ASCII lower case equivalent in-place.
2944    ///
2945    /// ASCII letters 'A' to 'Z' are mapped to 'a' to 'z',
2946    /// but non-ASCII letters are unchanged.
2947    ///
2948    /// To return a new lowercased value without modifying the existing one, use
2949    /// [`to_ascii_lowercase()`].
2950    ///
2951    /// [`to_ascii_lowercase()`]: #method.to_ascii_lowercase
2952    ///
2953    /// # Examples
2954    ///
2955    /// ```
2956    /// let mut s = String::from("GRÜßE, JÜRGEN ❤");
2957    ///
2958    /// s.make_ascii_lowercase();
2959    ///
2960    /// assert_eq!("grÜße, jÜrgen ❤", s);
2961    /// ```
2962    #[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
2963    #[rustc_const_stable(feature = "const_make_ascii", since = "1.84.0")]
2964    #[inline]
2965    pub const fn make_ascii_lowercase(&mut self) {
2966        // SAFETY: changing ASCII letters only does not invalidate UTF-8.
2967        let me = unsafe { self.as_bytes_mut() };
2968        me.make_ascii_lowercase()
2969    }
2970
2971    /// Returns a string slice with leading ASCII whitespace removed.
2972    ///
2973    /// 'Whitespace' refers to the definition used by
2974    /// [`u8::is_ascii_whitespace`]. Importantly, this definition excludes
2975    /// the U+000B code point even though it has the Unicode [`White_Space`] property
2976    /// and is removed by [`str::trim_start`].
2977    ///
2978    /// [`u8::is_ascii_whitespace`]: u8::is_ascii_whitespace
2979    /// [`White_Space`]: https://www.unicode.org/reports/tr44/#White_Space
2980    ///
2981    /// # Examples
2982    ///
2983    /// ```
2984    /// assert_eq!(" \t \u{3000}hello world\n".trim_ascii_start(), "\u{3000}hello world\n");
2985    /// assert_eq!("  ".trim_ascii_start(), "");
2986    /// assert_eq!("".trim_ascii_start(), "");
2987    /// ```
2988    #[must_use = "this returns the trimmed string as a new slice, \
2989                  without modifying the original"]
2990    #[stable(feature = "byte_slice_trim_ascii", since = "1.80.0")]
2991    #[rustc_const_stable(feature = "byte_slice_trim_ascii", since = "1.80.0")]
2992    #[inline]
2993    pub const fn trim_ascii_start(&self) -> &str {
2994        // SAFETY: Removing ASCII characters from a `&str` does not invalidate
2995        // UTF-8.
2996        unsafe { core::str::from_utf8_unchecked(self.as_bytes().trim_ascii_start()) }
2997    }
2998
2999    /// Returns a string slice with trailing ASCII whitespace removed.
3000    ///
3001    /// 'Whitespace' refers to the definition used by
3002    /// [`u8::is_ascii_whitespace`]. Importantly, this definition excludes
3003    /// the U+000B code point even though it has the Unicode [`White_Space`] property
3004    /// and is removed by [`str::trim_end`].
3005    ///
3006    /// [`u8::is_ascii_whitespace`]: u8::is_ascii_whitespace
3007    /// [`White_Space`]: https://www.unicode.org/reports/tr44/#White_Space
3008    ///
3009    /// # Examples
3010    ///
3011    /// ```
3012    /// assert_eq!("\r hello world\u{3000}\n ".trim_ascii_end(), "\r hello world\u{3000}");
3013    /// assert_eq!("  ".trim_ascii_end(), "");
3014    /// assert_eq!("".trim_ascii_end(), "");
3015    /// ```
3016    #[must_use = "this returns the trimmed string as a new slice, \
3017                  without modifying the original"]
3018    #[stable(feature = "byte_slice_trim_ascii", since = "1.80.0")]
3019    #[rustc_const_stable(feature = "byte_slice_trim_ascii", since = "1.80.0")]
3020    #[inline]
3021    pub const fn trim_ascii_end(&self) -> &str {
3022        // SAFETY: Removing ASCII characters from a `&str` does not invalidate
3023        // UTF-8.
3024        unsafe { core::str::from_utf8_unchecked(self.as_bytes().trim_ascii_end()) }
3025    }
3026
3027    /// Returns a string slice with leading and trailing ASCII whitespace
3028    /// removed.
3029    ///
3030    /// 'Whitespace' refers to the definition used by
3031    /// [`u8::is_ascii_whitespace`]. Importantly, this definition excludes
3032    /// the U+000B code point even though it has the Unicode [`White_Space`] property
3033    /// and is removed by [`str::trim`].
3034    ///
3035    /// [`u8::is_ascii_whitespace`]: u8::is_ascii_whitespace
3036    /// [`White_Space`]: https://www.unicode.org/reports/tr44/#White_Space
3037    ///
3038    /// # Examples
3039    ///
3040    /// ```
3041    /// assert_eq!("\r hello world\n ".trim_ascii(), "hello world");
3042    /// assert_eq!("  ".trim_ascii(), "");
3043    /// assert_eq!("".trim_ascii(), "");
3044    /// ```
3045    #[must_use = "this returns the trimmed string as a new slice, \
3046                  without modifying the original"]
3047    #[stable(feature = "byte_slice_trim_ascii", since = "1.80.0")]
3048    #[rustc_const_stable(feature = "byte_slice_trim_ascii", since = "1.80.0")]
3049    #[inline]
3050    pub const fn trim_ascii(&self) -> &str {
3051        // SAFETY: Removing ASCII characters from a `&str` does not invalidate
3052        // UTF-8.
3053        unsafe { core::str::from_utf8_unchecked(self.as_bytes().trim_ascii()) }
3054    }
3055
3056    /// Returns an iterator that escapes each char in `self` with [`char::escape_debug`].
3057    ///
3058    /// Note: only extended grapheme codepoints that begin the string will be
3059    /// escaped.
3060    ///
3061    /// # Examples
3062    ///
3063    /// As an iterator:
3064    ///
3065    /// ```
3066    /// for c in "❤\n!".escape_debug() {
3067    ///     print!("{c}");
3068    /// }
3069    /// println!();
3070    /// ```
3071    ///
3072    /// Using `println!` directly:
3073    ///
3074    /// ```
3075    /// println!("{}", "❤\n!".escape_debug());
3076    /// ```
3077    ///
3078    ///
3079    /// Both are equivalent to:
3080    ///
3081    /// ```
3082    /// println!("❤\\n!");
3083    /// ```
3084    ///
3085    /// Using `to_string`:
3086    ///
3087    /// ```
3088    /// assert_eq!("❤\n!".escape_debug().to_string(), "❤\\n!");
3089    /// ```
3090    #[must_use = "this returns the escaped string as an iterator, \
3091                  without modifying the original"]
3092    #[stable(feature = "str_escape", since = "1.34.0")]
3093    pub fn escape_debug(&self) -> EscapeDebug<'_> {
3094        let mut chars = self.chars();
3095        EscapeDebug {
3096            inner: chars
3097                .next()
3098                .map(|first| first.escape_debug_ext(EscapeDebugExtArgs::ESCAPE_ALL))
3099                .into_iter()
3100                .flatten()
3101                .chain(chars.flat_map(CharEscapeDebugContinue)),
3102        }
3103    }
3104
3105    /// Returns an iterator that escapes each char in `self` with [`char::escape_default`].
3106    ///
3107    /// # Examples
3108    ///
3109    /// As an iterator:
3110    ///
3111    /// ```
3112    /// for c in "❤\n!".escape_default() {
3113    ///     print!("{c}");
3114    /// }
3115    /// println!();
3116    /// ```
3117    ///
3118    /// Using `println!` directly:
3119    ///
3120    /// ```
3121    /// println!("{}", "❤\n!".escape_default());
3122    /// ```
3123    ///
3124    ///
3125    /// Both are equivalent to:
3126    ///
3127    /// ```
3128    /// println!("\\u{{2764}}\\n!");
3129    /// ```
3130    ///
3131    /// Using `to_string`:
3132    ///
3133    /// ```
3134    /// assert_eq!("❤\n!".escape_default().to_string(), "\\u{2764}\\n!");
3135    /// ```
3136    #[must_use = "this returns the escaped string as an iterator, \
3137                  without modifying the original"]
3138    #[stable(feature = "str_escape", since = "1.34.0")]
3139    pub fn escape_default(&self) -> EscapeDefault<'_> {
3140        EscapeDefault { inner: self.chars().flat_map(CharEscapeDefault) }
3141    }
3142
3143    /// Returns an iterator that escapes each char in `self` with [`char::escape_unicode`].
3144    ///
3145    /// # Examples
3146    ///
3147    /// As an iterator:
3148    ///
3149    /// ```
3150    /// for c in "❤\n!".escape_unicode() {
3151    ///     print!("{c}");
3152    /// }
3153    /// println!();
3154    /// ```
3155    ///
3156    /// Using `println!` directly:
3157    ///
3158    /// ```
3159    /// println!("{}", "❤\n!".escape_unicode());
3160    /// ```
3161    ///
3162    ///
3163    /// Both are equivalent to:
3164    ///
3165    /// ```
3166    /// println!("\\u{{2764}}\\u{{a}}\\u{{21}}");
3167    /// ```
3168    ///
3169    /// Using `to_string`:
3170    ///
3171    /// ```
3172    /// assert_eq!("❤\n!".escape_unicode().to_string(), "\\u{2764}\\u{a}\\u{21}");
3173    /// ```
3174    #[must_use = "this returns the escaped string as an iterator, \
3175                  without modifying the original"]
3176    #[stable(feature = "str_escape", since = "1.34.0")]
3177    pub fn escape_unicode(&self) -> EscapeUnicode<'_> {
3178        EscapeUnicode { inner: self.chars().flat_map(CharEscapeUnicode) }
3179    }
3180
3181    /// Returns the range that a substring points to.
3182    ///
3183    /// Returns `None` if `substr` does not point within `self`.
3184    ///
3185    /// Unlike [`str::find`], **this does not search through the string**.
3186    /// Instead, it uses pointer arithmetic to find where in the string
3187    /// `substr` is derived from.
3188    ///
3189    /// This is useful for extending [`str::split`] and similar methods.
3190    ///
3191    /// Note that this method may return false positives (typically either
3192    /// `Some(0..0)` or `Some(self.len()..self.len())`) if `substr` is a
3193    /// zero-length `str` that points at the beginning or end of another,
3194    /// independent, `str`.
3195    ///
3196    /// # Examples
3197    /// ```
3198    /// #![feature(substr_range)]
3199    /// use core::range::Range;
3200    ///
3201    /// let data = "a, b, b, a";
3202    /// let mut iter = data.split(", ").map(|s| data.substr_range(s).unwrap());
3203    ///
3204    /// assert_eq!(iter.next(), Some(Range { start: 0, end: 1 }));
3205    /// assert_eq!(iter.next(), Some(Range { start: 3, end: 4 }));
3206    /// assert_eq!(iter.next(), Some(Range { start: 6, end: 7 }));
3207    /// assert_eq!(iter.next(), Some(Range { start: 9, end: 10 }));
3208    /// ```
3209    #[must_use]
3210    #[unstable(feature = "substr_range", issue = "126769")]
3211    pub fn substr_range(&self, substr: &str) -> Option<Range<usize>> {
3212        self.as_bytes().subslice_range(substr.as_bytes())
3213    }
3214
3215    /// Returns the same string as a string slice `&str`.
3216    ///
3217    /// This method is redundant when used directly on `&str`, but
3218    /// it helps dereferencing other string-like types to string slices,
3219    /// for example references to `Box<str>` or `Arc<str>`.
3220    #[inline]
3221    #[unstable(feature = "str_as_str", issue = "130366")]
3222    pub const fn as_str(&self) -> &str {
3223        self
3224    }
3225}
3226
3227#[stable(feature = "rust1", since = "1.0.0")]
3228#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
3229const impl AsRef<[u8]> for str {
3230    #[inline]
3231    fn as_ref(&self) -> &[u8] {
3232        self.as_bytes()
3233    }
3234}
3235
3236#[stable(feature = "rust1", since = "1.0.0")]
3237#[rustc_const_unstable(feature = "const_default", issue = "143894")]
3238const impl Default for &str {
3239    /// Creates an empty str
3240    #[inline]
3241    fn default() -> Self {
3242        ""
3243    }
3244}
3245
3246#[stable(feature = "default_mut_str", since = "1.28.0")]
3247#[rustc_const_unstable(feature = "const_default", issue = "143894")]
3248const impl Default for &mut str {
3249    /// Creates an empty mutable str
3250    #[inline]
3251    fn default() -> Self {
3252        // SAFETY: The empty string is valid UTF-8.
3253        unsafe { from_utf8_unchecked_mut(&mut []) }
3254    }
3255}
3256
3257impl_fn_for_zst! {
3258    /// A nameable, cloneable fn type
3259    #[derive(Clone)]
3260    struct LinesMap impl<'a> Fn = |line: &'a str| -> &'a str {
3261        let Some(line) = line.strip_suffix('\n') else { return line };
3262        let Some(line) = line.strip_suffix('\r') else { return line };
3263        line
3264    };
3265
3266    #[derive(Clone)]
3267    struct CharEscapeDebugContinue impl Fn = |c: char| -> char::EscapeDebug {
3268        c.escape_debug_ext(EscapeDebugExtArgs {
3269            escape_grapheme_extended: false,
3270            escape_single_quote: true,
3271            escape_double_quote: true
3272        })
3273    };
3274
3275    #[derive(Clone)]
3276    struct CharEscapeUnicode impl Fn = |c: char| -> char::EscapeUnicode {
3277        c.escape_unicode()
3278    };
3279    #[derive(Clone)]
3280    struct CharEscapeDefault impl Fn = |c: char| -> char::EscapeDefault {
3281        c.escape_default()
3282    };
3283
3284    #[derive(Clone)]
3285    struct IsWhitespace impl Fn = |c: char| -> bool {
3286        c.is_whitespace()
3287    };
3288
3289    #[derive(Clone)]
3290    struct IsAsciiWhitespace impl Fn = |byte: &u8| -> bool {
3291        byte.is_ascii_whitespace()
3292    };
3293
3294    #[derive(Clone)]
3295    struct IsNotEmpty impl<'a, 'b> Fn = |s: &'a &'b str| -> bool {
3296        !s.is_empty()
3297    };
3298
3299    #[derive(Clone)]
3300    struct BytesIsNotEmpty impl<'a, 'b> Fn = |s: &'a &'b [u8]| -> bool {
3301        !s.is_empty()
3302    };
3303
3304    #[derive(Clone)]
3305    struct UnsafeBytesToStr impl<'a> Fn = |bytes: &'a [u8]| -> &'a str {
3306        // SAFETY: not safe
3307        unsafe { from_utf8_unchecked(bytes) }
3308    };
3309}
3310
3311// This is required to make `impl From<&str> for Box<dyn Error>` and `impl<E> From<E> for Box<dyn Error>` not overlap.
3312#[stable(feature = "error_in_core_neg_impl", since = "1.65.0")]
3313impl !crate::error::Error for &str {}