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