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 
9 mod converts;
10 mod error;
11 mod iter;
12 mod traits;
13 mod validations;
14 
15 use self::pattern::Pattern;
16 use self::pattern::{DoubleEndedSearcher, ReverseSearcher, Searcher};
17 
18 use crate::char::{self, EscapeDebugExtArgs};
19 use crate::mem;
20 use crate::slice::{self, SliceIndex};
21 
22 pub mod pattern;
23 
24 #[unstable(feature = "str_internals", issue = "none")]
25 #[allow(missing_docs)]
26 pub mod lossy;
27 
28 #[stable(feature = "rust1", since = "1.0.0")]
29 pub use converts::{from_utf8, from_utf8_unchecked};
30 
31 #[stable(feature = "str_mut_extras", since = "1.20.0")]
32 pub use converts::{from_utf8_mut, from_utf8_unchecked_mut};
33 
34 #[stable(feature = "rust1", since = "1.0.0")]
35 pub use error::{ParseBoolError, Utf8Error};
36 
37 #[stable(feature = "rust1", since = "1.0.0")]
38 pub use traits::FromStr;
39 
40 #[stable(feature = "rust1", since = "1.0.0")]
41 pub use iter::{Bytes, CharIndices, Chars, Lines, SplitWhitespace};
42 
43 #[stable(feature = "rust1", since = "1.0.0")]
44 #[allow(deprecated)]
45 pub use iter::LinesAny;
46 
47 #[stable(feature = "rust1", since = "1.0.0")]
48 pub use iter::{RSplit, RSplitTerminator, Split, SplitTerminator};
49 
50 #[stable(feature = "rust1", since = "1.0.0")]
51 pub use iter::{RSplitN, SplitN};
52 
53 #[stable(feature = "str_matches", since = "1.2.0")]
54 pub use iter::{Matches, RMatches};
55 
56 #[stable(feature = "str_match_indices", since = "1.5.0")]
57 pub use iter::{MatchIndices, RMatchIndices};
58 
59 #[stable(feature = "encode_utf16", since = "1.8.0")]
60 pub use iter::EncodeUtf16;
61 
62 #[stable(feature = "str_escape", since = "1.34.0")]
63 pub use iter::{EscapeDebug, EscapeDefault, EscapeUnicode};
64 
65 #[stable(feature = "split_ascii_whitespace", since = "1.34.0")]
66 pub use iter::SplitAsciiWhitespace;
67 
68 #[stable(feature = "split_inclusive", since = "1.51.0")]
69 pub use iter::SplitInclusive;
70 
71 #[unstable(feature = "str_internals", issue = "none")]
72 pub use validations::{next_code_point, utf8_char_width};
73 
74 use iter::MatchIndicesInternal;
75 use iter::SplitInternal;
76 use iter::{MatchesInternal, SplitNInternal};
77 
78 use validations::truncate_to_char_boundary;
79 
80 #[inline(never)]
81 #[cold]
82 #[track_caller]
slice_error_fail(s: &str, begin: usize, end: usize) -> !83 fn slice_error_fail(s: &str, begin: usize, end: usize) -> ! {
84     const MAX_DISPLAY_LENGTH: usize = 256;
85     let (truncated, s_trunc) = truncate_to_char_boundary(s, MAX_DISPLAY_LENGTH);
86     let ellipsis = if truncated { "[...]" } else { "" };
87 
88     // 1. out of bounds
89     if begin > s.len() || end > s.len() {
90         let oob_index = if begin > s.len() { begin } else { end };
91         panic!("byte index {} is out of bounds of `{}`{}", oob_index, s_trunc, ellipsis);
92     }
93 
94     // 2. begin <= end
95     assert!(
96         begin <= end,
97         "begin <= end ({} <= {}) when slicing `{}`{}",
98         begin,
99         end,
100         s_trunc,
101         ellipsis
102     );
103 
104     // 3. character boundary
105     let index = if !s.is_char_boundary(begin) { begin } else { end };
106     // find the character
107     let mut char_start = index;
108     while !s.is_char_boundary(char_start) {
109         char_start -= 1;
110     }
111     // `char_start` must be less than len and a char boundary
112     let ch = s[char_start..].chars().next().unwrap();
113     let char_range = char_start..char_start + ch.len_utf8();
114     panic!(
115         "byte index {} is not a char boundary; it is inside {:?} (bytes {:?}) of `{}`{}",
116         index, ch, char_range, s_trunc, ellipsis
117     );
118 }
119 
120 #[lang = "str"]
121 #[cfg(not(test))]
122 impl str {
123     /// Returns the length of `self`.
124     ///
125     /// This length is in bytes, not [`char`]s or graphemes. In other words,
126     /// it might not be what a human considers the length of the string.
127     ///
128     /// [`char`]: prim@char
129     ///
130     /// # Examples
131     ///
132     /// Basic usage:
133     ///
134     /// ```
135     /// let len = "foo".len();
136     /// assert_eq!(3, len);
137     ///
138     /// assert_eq!("ƒoo".len(), 4); // fancy f!
139     /// assert_eq!("ƒoo".chars().count(), 3);
140     /// ```
141     #[stable(feature = "rust1", since = "1.0.0")]
142     #[rustc_const_stable(feature = "const_str_len", since = "1.39.0")]
143     #[must_use]
144     #[inline]
len(&self) -> usize145     pub const fn len(&self) -> usize {
146         self.as_bytes().len()
147     }
148 
149     /// Returns `true` if `self` has a length of zero bytes.
150     ///
151     /// # Examples
152     ///
153     /// Basic usage:
154     ///
155     /// ```
156     /// let s = "";
157     /// assert!(s.is_empty());
158     ///
159     /// let s = "not empty";
160     /// assert!(!s.is_empty());
161     /// ```
162     #[stable(feature = "rust1", since = "1.0.0")]
163     #[rustc_const_stable(feature = "const_str_is_empty", since = "1.39.0")]
164     #[must_use]
165     #[inline]
is_empty(&self) -> bool166     pub const fn is_empty(&self) -> bool {
167         self.len() == 0
168     }
169 
170     /// Checks that `index`-th byte is the first byte in a UTF-8 code point
171     /// sequence or the end of the string.
172     ///
173     /// The start and end of the string (when `index == self.len()`) are
174     /// considered to be boundaries.
175     ///
176     /// Returns `false` if `index` is greater than `self.len()`.
177     ///
178     /// # Examples
179     ///
180     /// ```
181     /// let s = "Löwe 老虎 Léopard";
182     /// assert!(s.is_char_boundary(0));
183     /// // start of `老`
184     /// assert!(s.is_char_boundary(6));
185     /// assert!(s.is_char_boundary(s.len()));
186     ///
187     /// // second byte of `ö`
188     /// assert!(!s.is_char_boundary(2));
189     ///
190     /// // third byte of `老`
191     /// assert!(!s.is_char_boundary(8));
192     /// ```
193     #[must_use]
194     #[stable(feature = "is_char_boundary", since = "1.9.0")]
195     #[inline]
is_char_boundary(&self, index: usize) -> bool196     pub fn is_char_boundary(&self, index: usize) -> bool {
197         // 0 is always ok.
198         // Test for 0 explicitly so that it can optimize out the check
199         // easily and skip reading string data for that case.
200         // Note that optimizing `self.get(..index)` relies on this.
201         if index == 0 {
202             return true;
203         }
204 
205         match self.as_bytes().get(index) {
206             // For `None` we have two options:
207             //
208             // - index == self.len()
209             //   Empty strings are valid, so return true
210             // - index > self.len()
211             //   In this case return false
212             //
213             // The check is placed exactly here, because it improves generated
214             // code on higher opt-levels. See PR #84751 for more details.
215             None => index == self.len(),
216 
217             // This is bit magic equivalent to: b < 128 || b >= 192
218             Some(&b) => (b as i8) >= -0x40,
219         }
220     }
221 
222     /// Converts a string slice to a byte slice. To convert the byte slice back
223     /// into a string slice, use the [`from_utf8`] function.
224     ///
225     /// # Examples
226     ///
227     /// Basic usage:
228     ///
229     /// ```
230     /// let bytes = "bors".as_bytes();
231     /// assert_eq!(b"bors", bytes);
232     /// ```
233     #[stable(feature = "rust1", since = "1.0.0")]
234     #[rustc_const_stable(feature = "str_as_bytes", since = "1.39.0")]
235     #[must_use]
236     #[inline(always)]
237     #[allow(unused_attributes)]
as_bytes(&self) -> &[u8]238     pub const fn as_bytes(&self) -> &[u8] {
239         // SAFETY: const sound because we transmute two types with the same layout
240         unsafe { mem::transmute(self) }
241     }
242 
243     /// Converts a mutable string slice to a mutable byte slice.
244     ///
245     /// # Safety
246     ///
247     /// The caller must ensure that the content of the slice is valid UTF-8
248     /// before the borrow ends and the underlying `str` is used.
249     ///
250     /// Use of a `str` whose contents are not valid UTF-8 is undefined behavior.
251     ///
252     /// # Examples
253     ///
254     /// Basic usage:
255     ///
256     /// ```
257     /// let mut s = String::from("Hello");
258     /// let bytes = unsafe { s.as_bytes_mut() };
259     ///
260     /// assert_eq!(b"Hello", bytes);
261     /// ```
262     ///
263     /// Mutability:
264     ///
265     /// ```
266     /// let mut s = String::from("��∈��");
267     ///
268     /// unsafe {
269     ///     let bytes = s.as_bytes_mut();
270     ///
271     ///     bytes[0] = 0xF0;
272     ///     bytes[1] = 0x9F;
273     ///     bytes[2] = 0x8D;
274     ///     bytes[3] = 0x94;
275     /// }
276     ///
277     /// assert_eq!("��∈��", s);
278     /// ```
279     #[stable(feature = "str_mut_extras", since = "1.20.0")]
280     #[must_use]
281     #[inline(always)]
as_bytes_mut(&mut self) -> &mut [u8]282     pub unsafe fn as_bytes_mut(&mut self) -> &mut [u8] {
283         // SAFETY: the cast from `&str` to `&[u8]` is safe since `str`
284         // has the same layout as `&[u8]` (only libstd can make this guarantee).
285         // The pointer dereference is safe since it comes from a mutable reference which
286         // is guaranteed to be valid for writes.
287         unsafe { &mut *(self as *mut str as *mut [u8]) }
288     }
289 
290     /// Converts a string slice to a raw pointer.
291     ///
292     /// As string slices are a slice of bytes, the raw pointer points to a
293     /// [`u8`]. This pointer will be pointing to the first byte of the string
294     /// slice.
295     ///
296     /// The caller must ensure that the returned pointer is never written to.
297     /// If you need to mutate the contents of the string slice, use [`as_mut_ptr`].
298     ///
299     /// [`as_mut_ptr`]: str::as_mut_ptr
300     ///
301     /// # Examples
302     ///
303     /// Basic usage:
304     ///
305     /// ```
306     /// let s = "Hello";
307     /// let ptr = s.as_ptr();
308     /// ```
309     #[stable(feature = "rust1", since = "1.0.0")]
310     #[rustc_const_stable(feature = "rustc_str_as_ptr", since = "1.32.0")]
311     #[must_use]
312     #[inline]
as_ptr(&self) -> *const u8313     pub const fn as_ptr(&self) -> *const u8 {
314         self as *const str as *const u8
315     }
316 
317     /// Converts a mutable string slice to a raw pointer.
318     ///
319     /// As string slices are a slice of bytes, the raw pointer points to a
320     /// [`u8`]. This pointer will be pointing to the first byte of the string
321     /// slice.
322     ///
323     /// It is your responsibility to make sure that the string slice only gets
324     /// modified in a way that it remains valid UTF-8.
325     #[stable(feature = "str_as_mut_ptr", since = "1.36.0")]
326     #[must_use]
327     #[inline]
as_mut_ptr(&mut self) -> *mut u8328     pub fn as_mut_ptr(&mut self) -> *mut u8 {
329         self as *mut str as *mut u8
330     }
331 
332     /// Returns a subslice of `str`.
333     ///
334     /// This is the non-panicking alternative to indexing the `str`. Returns
335     /// [`None`] whenever equivalent indexing operation would panic.
336     ///
337     /// # Examples
338     ///
339     /// ```
340     /// let v = String::from("��∈��");
341     ///
342     /// assert_eq!(Some("��"), v.get(0..4));
343     ///
344     /// // indices not on UTF-8 sequence boundaries
345     /// assert!(v.get(1..).is_none());
346     /// assert!(v.get(..8).is_none());
347     ///
348     /// // out of bounds
349     /// assert!(v.get(..42).is_none());
350     /// ```
351     #[stable(feature = "str_checked_slicing", since = "1.20.0")]
352     #[inline]
get<I: SliceIndex<str>>(&self, i: I) -> Option<&I::Output>353     pub fn get<I: SliceIndex<str>>(&self, i: I) -> Option<&I::Output> {
354         i.get(self)
355     }
356 
357     /// Returns a mutable subslice of `str`.
358     ///
359     /// This is the non-panicking alternative to indexing the `str`. Returns
360     /// [`None`] whenever equivalent indexing operation would panic.
361     ///
362     /// # Examples
363     ///
364     /// ```
365     /// let mut v = String::from("hello");
366     /// // correct length
367     /// assert!(v.get_mut(0..5).is_some());
368     /// // out of bounds
369     /// assert!(v.get_mut(..42).is_none());
370     /// assert_eq!(Some("he"), v.get_mut(0..2).map(|v| &*v));
371     ///
372     /// assert_eq!("hello", v);
373     /// {
374     ///     let s = v.get_mut(0..2);
375     ///     let s = s.map(|s| {
376     ///         s.make_ascii_uppercase();
377     ///         &*s
378     ///     });
379     ///     assert_eq!(Some("HE"), s);
380     /// }
381     /// assert_eq!("HEllo", v);
382     /// ```
383     #[stable(feature = "str_checked_slicing", since = "1.20.0")]
384     #[inline]
get_mut<I: SliceIndex<str>>(&mut self, i: I) -> Option<&mut I::Output>385     pub fn get_mut<I: SliceIndex<str>>(&mut self, i: I) -> Option<&mut I::Output> {
386         i.get_mut(self)
387     }
388 
389     /// Returns an unchecked subslice of `str`.
390     ///
391     /// This is the unchecked alternative to indexing the `str`.
392     ///
393     /// # Safety
394     ///
395     /// Callers of this function are responsible that these preconditions are
396     /// satisfied:
397     ///
398     /// * The starting index must not exceed the ending index;
399     /// * Indexes must be within bounds of the original slice;
400     /// * Indexes must lie on UTF-8 sequence boundaries.
401     ///
402     /// Failing that, the returned string slice may reference invalid memory or
403     /// violate the invariants communicated by the `str` type.
404     ///
405     /// # Examples
406     ///
407     /// ```
408     /// let v = "��∈��";
409     /// unsafe {
410     ///     assert_eq!("��", v.get_unchecked(0..4));
411     ///     assert_eq!("∈", v.get_unchecked(4..7));
412     ///     assert_eq!("��", v.get_unchecked(7..11));
413     /// }
414     /// ```
415     #[stable(feature = "str_checked_slicing", since = "1.20.0")]
416     #[inline]
get_unchecked<I: SliceIndex<str>>(&self, i: I) -> &I::Output417     pub unsafe fn get_unchecked<I: SliceIndex<str>>(&self, i: I) -> &I::Output {
418         // SAFETY: the caller must uphold the safety contract for `get_unchecked`;
419         // the slice is dereferencable because `self` is a safe reference.
420         // The returned pointer is safe because impls of `SliceIndex` have to guarantee that it is.
421         unsafe { &*i.get_unchecked(self) }
422     }
423 
424     /// Returns a mutable, unchecked subslice of `str`.
425     ///
426     /// This is the unchecked alternative to indexing the `str`.
427     ///
428     /// # Safety
429     ///
430     /// Callers of this function are responsible that these preconditions are
431     /// satisfied:
432     ///
433     /// * The starting index must not exceed the ending index;
434     /// * Indexes must be within bounds of the original slice;
435     /// * Indexes must lie on UTF-8 sequence boundaries.
436     ///
437     /// Failing that, the returned string slice may reference invalid memory or
438     /// violate the invariants communicated by the `str` type.
439     ///
440     /// # Examples
441     ///
442     /// ```
443     /// let mut v = String::from("��∈��");
444     /// unsafe {
445     ///     assert_eq!("��", v.get_unchecked_mut(0..4));
446     ///     assert_eq!("∈", v.get_unchecked_mut(4..7));
447     ///     assert_eq!("��", v.get_unchecked_mut(7..11));
448     /// }
449     /// ```
450     #[stable(feature = "str_checked_slicing", since = "1.20.0")]
451     #[inline]
get_unchecked_mut<I: SliceIndex<str>>(&mut self, i: I) -> &mut I::Output452     pub unsafe fn get_unchecked_mut<I: SliceIndex<str>>(&mut self, i: I) -> &mut I::Output {
453         // SAFETY: the caller must uphold the safety contract for `get_unchecked_mut`;
454         // the slice is dereferencable because `self` is a safe reference.
455         // The returned pointer is safe because impls of `SliceIndex` have to guarantee that it is.
456         unsafe { &mut *i.get_unchecked_mut(self) }
457     }
458 
459     /// Creates a string slice from another string slice, bypassing safety
460     /// checks.
461     ///
462     /// This is generally not recommended, use with caution! For a safe
463     /// alternative see [`str`] and [`Index`].
464     ///
465     /// [`Index`]: crate::ops::Index
466     ///
467     /// This new slice goes from `begin` to `end`, including `begin` but
468     /// excluding `end`.
469     ///
470     /// To get a mutable string slice instead, see the
471     /// [`slice_mut_unchecked`] method.
472     ///
473     /// [`slice_mut_unchecked`]: str::slice_mut_unchecked
474     ///
475     /// # Safety
476     ///
477     /// Callers of this function are responsible that three preconditions are
478     /// satisfied:
479     ///
480     /// * `begin` must not exceed `end`.
481     /// * `begin` and `end` must be byte positions within the string slice.
482     /// * `begin` and `end` must lie on UTF-8 sequence boundaries.
483     ///
484     /// # Examples
485     ///
486     /// Basic usage:
487     ///
488     /// ```
489     /// let s = "Löwe 老虎 Léopard";
490     ///
491     /// unsafe {
492     ///     assert_eq!("Löwe 老虎 Léopard", s.slice_unchecked(0, 21));
493     /// }
494     ///
495     /// let s = "Hello, world!";
496     ///
497     /// unsafe {
498     ///     assert_eq!("world", s.slice_unchecked(7, 12));
499     /// }
500     /// ```
501     #[stable(feature = "rust1", since = "1.0.0")]
502     #[rustc_deprecated(since = "1.29.0", reason = "use `get_unchecked(begin..end)` instead")]
503     #[must_use]
504     #[inline]
slice_unchecked(&self, begin: usize, end: usize) -> &str505     pub unsafe fn slice_unchecked(&self, begin: usize, end: usize) -> &str {
506         // SAFETY: the caller must uphold the safety contract for `get_unchecked`;
507         // the slice is dereferencable because `self` is a safe reference.
508         // The returned pointer is safe because impls of `SliceIndex` have to guarantee that it is.
509         unsafe { &*(begin..end).get_unchecked(self) }
510     }
511 
512     /// Creates a string slice from another string slice, bypassing safety
513     /// checks.
514     /// This is generally not recommended, use with caution! For a safe
515     /// alternative see [`str`] and [`IndexMut`].
516     ///
517     /// [`IndexMut`]: crate::ops::IndexMut
518     ///
519     /// This new slice goes from `begin` to `end`, including `begin` but
520     /// excluding `end`.
521     ///
522     /// To get an immutable string slice instead, see the
523     /// [`slice_unchecked`] method.
524     ///
525     /// [`slice_unchecked`]: str::slice_unchecked
526     ///
527     /// # Safety
528     ///
529     /// Callers of this function are responsible that three preconditions are
530     /// satisfied:
531     ///
532     /// * `begin` must not exceed `end`.
533     /// * `begin` and `end` must be byte positions within the string slice.
534     /// * `begin` and `end` must lie on UTF-8 sequence boundaries.
535     #[stable(feature = "str_slice_mut", since = "1.5.0")]
536     #[rustc_deprecated(since = "1.29.0", reason = "use `get_unchecked_mut(begin..end)` instead")]
537     #[inline]
slice_mut_unchecked(&mut self, begin: usize, end: usize) -> &mut str538     pub unsafe fn slice_mut_unchecked(&mut self, begin: usize, end: usize) -> &mut str {
539         // SAFETY: the caller must uphold the safety contract for `get_unchecked_mut`;
540         // the slice is dereferencable because `self` is a safe reference.
541         // The returned pointer is safe because impls of `SliceIndex` have to guarantee that it is.
542         unsafe { &mut *(begin..end).get_unchecked_mut(self) }
543     }
544 
545     /// Divide one string slice into two at an index.
546     ///
547     /// The argument, `mid`, should be a byte offset from the start of the
548     /// string. It must also be on the boundary of a UTF-8 code point.
549     ///
550     /// The two slices returned go from the start of the string slice to `mid`,
551     /// and from `mid` to the end of the string slice.
552     ///
553     /// To get mutable string slices instead, see the [`split_at_mut`]
554     /// method.
555     ///
556     /// [`split_at_mut`]: str::split_at_mut
557     ///
558     /// # Panics
559     ///
560     /// Panics if `mid` is not on a UTF-8 code point boundary, or if it is
561     /// past the end of the last code point of the string slice.
562     ///
563     /// # Examples
564     ///
565     /// Basic usage:
566     ///
567     /// ```
568     /// let s = "Per Martin-Löf";
569     ///
570     /// let (first, last) = s.split_at(3);
571     ///
572     /// assert_eq!("Per", first);
573     /// assert_eq!(" Martin-Löf", last);
574     /// ```
575     #[inline]
576     #[must_use]
577     #[stable(feature = "str_split_at", since = "1.4.0")]
split_at(&self, mid: usize) -> (&str, &str)578     pub fn split_at(&self, mid: usize) -> (&str, &str) {
579         // is_char_boundary checks that the index is in [0, .len()]
580         if self.is_char_boundary(mid) {
581             // SAFETY: just checked that `mid` is on a char boundary.
582             unsafe { (self.get_unchecked(0..mid), self.get_unchecked(mid..self.len())) }
583         } else {
584             slice_error_fail(self, 0, mid)
585         }
586     }
587 
588     /// Divide one mutable string slice into two at an index.
589     ///
590     /// The argument, `mid`, should be a byte offset from the start of the
591     /// string. It must also be on the boundary of a UTF-8 code point.
592     ///
593     /// The two slices returned go from the start of the string slice to `mid`,
594     /// and from `mid` to the end of the string slice.
595     ///
596     /// To get immutable string slices instead, see the [`split_at`] method.
597     ///
598     /// [`split_at`]: str::split_at
599     ///
600     /// # Panics
601     ///
602     /// Panics if `mid` is not on a UTF-8 code point boundary, or if it is
603     /// past the end of the last code point of the string slice.
604     ///
605     /// # Examples
606     ///
607     /// Basic usage:
608     ///
609     /// ```
610     /// let mut s = "Per Martin-Löf".to_string();
611     /// {
612     ///     let (first, last) = s.split_at_mut(3);
613     ///     first.make_ascii_uppercase();
614     ///     assert_eq!("PER", first);
615     ///     assert_eq!(" Martin-Löf", last);
616     /// }
617     /// assert_eq!("PER Martin-Löf", s);
618     /// ```
619     #[inline]
620     #[must_use]
621     #[stable(feature = "str_split_at", since = "1.4.0")]
split_at_mut(&mut self, mid: usize) -> (&mut str, &mut str)622     pub fn split_at_mut(&mut self, mid: usize) -> (&mut str, &mut str) {
623         // is_char_boundary checks that the index is in [0, .len()]
624         if self.is_char_boundary(mid) {
625             let len = self.len();
626             let ptr = self.as_mut_ptr();
627             // SAFETY: just checked that `mid` is on a char boundary.
628             unsafe {
629                 (
630                     from_utf8_unchecked_mut(slice::from_raw_parts_mut(ptr, mid)),
631                     from_utf8_unchecked_mut(slice::from_raw_parts_mut(ptr.add(mid), len - mid)),
632                 )
633             }
634         } else {
635             slice_error_fail(self, 0, mid)
636         }
637     }
638 
639     /// Returns an iterator over the [`char`]s of a string slice.
640     ///
641     /// As a string slice consists of valid UTF-8, we can iterate through a
642     /// string slice by [`char`]. This method returns such an iterator.
643     ///
644     /// It's important to remember that [`char`] represents a Unicode Scalar
645     /// Value, and might not match your idea of what a 'character' is. Iteration
646     /// over grapheme clusters may be what you actually want. This functionality
647     /// is not provided by Rust's standard library, check crates.io instead.
648     ///
649     /// # Examples
650     ///
651     /// Basic usage:
652     ///
653     /// ```
654     /// let word = "goodbye";
655     ///
656     /// let count = word.chars().count();
657     /// assert_eq!(7, count);
658     ///
659     /// let mut chars = word.chars();
660     ///
661     /// assert_eq!(Some('g'), chars.next());
662     /// assert_eq!(Some('o'), chars.next());
663     /// assert_eq!(Some('o'), chars.next());
664     /// assert_eq!(Some('d'), chars.next());
665     /// assert_eq!(Some('b'), chars.next());
666     /// assert_eq!(Some('y'), chars.next());
667     /// assert_eq!(Some('e'), chars.next());
668     ///
669     /// assert_eq!(None, chars.next());
670     /// ```
671     ///
672     /// Remember, [`char`]s might not match your intuition about characters:
673     ///
674     /// [`char`]: prim@char
675     ///
676     /// ```
677     /// let y = "y̆";
678     ///
679     /// let mut chars = y.chars();
680     ///
681     /// assert_eq!(Some('y'), chars.next()); // not 'y̆'
682     /// assert_eq!(Some('\u{0306}'), chars.next());
683     ///
684     /// assert_eq!(None, chars.next());
685     /// ```
686     #[stable(feature = "rust1", since = "1.0.0")]
687     #[inline]
chars(&self) -> Chars<'_>688     pub fn chars(&self) -> Chars<'_> {
689         Chars { iter: self.as_bytes().iter() }
690     }
691 
692     /// Returns an iterator over the [`char`]s of a string slice, and their
693     /// positions.
694     ///
695     /// As a string slice consists of valid UTF-8, we can iterate through a
696     /// string slice by [`char`]. This method returns an iterator of both
697     /// these [`char`]s, as well as their byte positions.
698     ///
699     /// The iterator yields tuples. The position is first, the [`char`] is
700     /// second.
701     ///
702     /// # Examples
703     ///
704     /// Basic usage:
705     ///
706     /// ```
707     /// let word = "goodbye";
708     ///
709     /// let count = word.char_indices().count();
710     /// assert_eq!(7, count);
711     ///
712     /// let mut char_indices = word.char_indices();
713     ///
714     /// assert_eq!(Some((0, 'g')), char_indices.next());
715     /// assert_eq!(Some((1, 'o')), char_indices.next());
716     /// assert_eq!(Some((2, 'o')), char_indices.next());
717     /// assert_eq!(Some((3, 'd')), char_indices.next());
718     /// assert_eq!(Some((4, 'b')), char_indices.next());
719     /// assert_eq!(Some((5, 'y')), char_indices.next());
720     /// assert_eq!(Some((6, 'e')), char_indices.next());
721     ///
722     /// assert_eq!(None, char_indices.next());
723     /// ```
724     ///
725     /// Remember, [`char`]s might not match your intuition about characters:
726     ///
727     /// [`char`]: prim@char
728     ///
729     /// ```
730     /// let yes = "y̆es";
731     ///
732     /// let mut char_indices = yes.char_indices();
733     ///
734     /// assert_eq!(Some((0, 'y')), char_indices.next()); // not (0, 'y̆')
735     /// assert_eq!(Some((1, '\u{0306}')), char_indices.next());
736     ///
737     /// // note the 3 here - the last character took up two bytes
738     /// assert_eq!(Some((3, 'e')), char_indices.next());
739     /// assert_eq!(Some((4, 's')), char_indices.next());
740     ///
741     /// assert_eq!(None, char_indices.next());
742     /// ```
743     #[stable(feature = "rust1", since = "1.0.0")]
744     #[inline]
char_indices(&self) -> CharIndices<'_>745     pub fn char_indices(&self) -> CharIndices<'_> {
746         CharIndices { front_offset: 0, iter: self.chars() }
747     }
748 
749     /// An iterator over the bytes of a string slice.
750     ///
751     /// As a string slice consists of a sequence of bytes, we can iterate
752     /// through a string slice by byte. This method returns such an iterator.
753     ///
754     /// # Examples
755     ///
756     /// Basic usage:
757     ///
758     /// ```
759     /// let mut bytes = "bors".bytes();
760     ///
761     /// assert_eq!(Some(b'b'), bytes.next());
762     /// assert_eq!(Some(b'o'), bytes.next());
763     /// assert_eq!(Some(b'r'), bytes.next());
764     /// assert_eq!(Some(b's'), bytes.next());
765     ///
766     /// assert_eq!(None, bytes.next());
767     /// ```
768     #[stable(feature = "rust1", since = "1.0.0")]
769     #[inline]
bytes(&self) -> Bytes<'_>770     pub fn bytes(&self) -> Bytes<'_> {
771         Bytes(self.as_bytes().iter().copied())
772     }
773 
774     /// Splits a string slice by whitespace.
775     ///
776     /// The iterator returned will return string slices that are sub-slices of
777     /// the original string slice, separated by any amount of whitespace.
778     ///
779     /// 'Whitespace' is defined according to the terms of the Unicode Derived
780     /// Core Property `White_Space`. If you only want to split on ASCII whitespace
781     /// instead, use [`split_ascii_whitespace`].
782     ///
783     /// [`split_ascii_whitespace`]: str::split_ascii_whitespace
784     ///
785     /// # Examples
786     ///
787     /// Basic usage:
788     ///
789     /// ```
790     /// let mut iter = "A few words".split_whitespace();
791     ///
792     /// assert_eq!(Some("A"), iter.next());
793     /// assert_eq!(Some("few"), iter.next());
794     /// assert_eq!(Some("words"), iter.next());
795     ///
796     /// assert_eq!(None, iter.next());
797     /// ```
798     ///
799     /// All kinds of whitespace are considered:
800     ///
801     /// ```
802     /// let mut iter = " Mary   had\ta\u{2009}little  \n\t lamb".split_whitespace();
803     /// assert_eq!(Some("Mary"), iter.next());
804     /// assert_eq!(Some("had"), iter.next());
805     /// assert_eq!(Some("a"), iter.next());
806     /// assert_eq!(Some("little"), iter.next());
807     /// assert_eq!(Some("lamb"), iter.next());
808     ///
809     /// assert_eq!(None, iter.next());
810     /// ```
811     #[must_use = "this returns the split string as an iterator, \
812                   without modifying the original"]
813     #[stable(feature = "split_whitespace", since = "1.1.0")]
814     #[inline]
split_whitespace(&self) -> SplitWhitespace<'_>815     pub fn split_whitespace(&self) -> SplitWhitespace<'_> {
816         SplitWhitespace { inner: self.split(IsWhitespace).filter(IsNotEmpty) }
817     }
818 
819     /// Splits a string slice by ASCII whitespace.
820     ///
821     /// The iterator returned will return string slices that are sub-slices of
822     /// the original string slice, separated by any amount of ASCII whitespace.
823     ///
824     /// To split by Unicode `Whitespace` instead, use [`split_whitespace`].
825     ///
826     /// [`split_whitespace`]: str::split_whitespace
827     ///
828     /// # Examples
829     ///
830     /// Basic usage:
831     ///
832     /// ```
833     /// let mut iter = "A few words".split_ascii_whitespace();
834     ///
835     /// assert_eq!(Some("A"), iter.next());
836     /// assert_eq!(Some("few"), iter.next());
837     /// assert_eq!(Some("words"), iter.next());
838     ///
839     /// assert_eq!(None, iter.next());
840     /// ```
841     ///
842     /// All kinds of ASCII whitespace are considered:
843     ///
844     /// ```
845     /// let mut iter = " Mary   had\ta little  \n\t lamb".split_ascii_whitespace();
846     /// assert_eq!(Some("Mary"), iter.next());
847     /// assert_eq!(Some("had"), iter.next());
848     /// assert_eq!(Some("a"), iter.next());
849     /// assert_eq!(Some("little"), iter.next());
850     /// assert_eq!(Some("lamb"), iter.next());
851     ///
852     /// assert_eq!(None, iter.next());
853     /// ```
854     #[must_use = "this returns the split string as an iterator, \
855                   without modifying the original"]
856     #[stable(feature = "split_ascii_whitespace", since = "1.34.0")]
857     #[inline]
split_ascii_whitespace(&self) -> SplitAsciiWhitespace<'_>858     pub fn split_ascii_whitespace(&self) -> SplitAsciiWhitespace<'_> {
859         let inner =
860             self.as_bytes().split(IsAsciiWhitespace).filter(BytesIsNotEmpty).map(UnsafeBytesToStr);
861         SplitAsciiWhitespace { inner }
862     }
863 
864     /// An iterator over the lines of a string, as string slices.
865     ///
866     /// Lines are ended with either a newline (`\n`) or a carriage return with
867     /// a line feed (`\r\n`).
868     ///
869     /// The final line ending is optional. A string that ends with a final line
870     /// ending will return the same lines as an otherwise identical string
871     /// without a final line ending.
872     ///
873     /// # Examples
874     ///
875     /// Basic usage:
876     ///
877     /// ```
878     /// let text = "foo\r\nbar\n\nbaz\n";
879     /// let mut lines = text.lines();
880     ///
881     /// assert_eq!(Some("foo"), lines.next());
882     /// assert_eq!(Some("bar"), lines.next());
883     /// assert_eq!(Some(""), lines.next());
884     /// assert_eq!(Some("baz"), lines.next());
885     ///
886     /// assert_eq!(None, lines.next());
887     /// ```
888     ///
889     /// The final line ending isn't required:
890     ///
891     /// ```
892     /// let text = "foo\nbar\n\r\nbaz";
893     /// let mut lines = text.lines();
894     ///
895     /// assert_eq!(Some("foo"), lines.next());
896     /// assert_eq!(Some("bar"), lines.next());
897     /// assert_eq!(Some(""), lines.next());
898     /// assert_eq!(Some("baz"), lines.next());
899     ///
900     /// assert_eq!(None, lines.next());
901     /// ```
902     #[stable(feature = "rust1", since = "1.0.0")]
903     #[inline]
lines(&self) -> Lines<'_>904     pub fn lines(&self) -> Lines<'_> {
905         Lines(self.split_terminator('\n').map(LinesAnyMap))
906     }
907 
908     /// An iterator over the lines of a string.
909     #[stable(feature = "rust1", since = "1.0.0")]
910     #[rustc_deprecated(since = "1.4.0", reason = "use lines() instead now")]
911     #[inline]
912     #[allow(deprecated)]
lines_any(&self) -> LinesAny<'_>913     pub fn lines_any(&self) -> LinesAny<'_> {
914         LinesAny(self.lines())
915     }
916 
917     /// Returns an iterator of `u16` over the string encoded as UTF-16.
918     ///
919     /// # Examples
920     ///
921     /// Basic usage:
922     ///
923     /// ```
924     /// let text = "Zażółć gęślą jaźń";
925     ///
926     /// let utf8_len = text.len();
927     /// let utf16_len = text.encode_utf16().count();
928     ///
929     /// assert!(utf16_len <= utf8_len);
930     /// ```
931     #[must_use = "this returns the encoded string as an iterator, \
932                   without modifying the original"]
933     #[stable(feature = "encode_utf16", since = "1.8.0")]
encode_utf16(&self) -> EncodeUtf16<'_>934     pub fn encode_utf16(&self) -> EncodeUtf16<'_> {
935         EncodeUtf16 { chars: self.chars(), extra: 0 }
936     }
937 
938     /// Returns `true` if the given pattern matches a sub-slice of
939     /// this string slice.
940     ///
941     /// Returns `false` if it does not.
942     ///
943     /// The [pattern] can be a `&str`, [`char`], a slice of [`char`]s, or a
944     /// function or closure that determines if a character matches.
945     ///
946     /// [`char`]: prim@char
947     /// [pattern]: self::pattern
948     ///
949     /// # Examples
950     ///
951     /// Basic usage:
952     ///
953     /// ```
954     /// let bananas = "bananas";
955     ///
956     /// assert!(bananas.contains("nana"));
957     /// assert!(!bananas.contains("apples"));
958     /// ```
959     #[stable(feature = "rust1", since = "1.0.0")]
960     #[inline]
contains<'a, P: Pattern<'a>>(&'a self, pat: P) -> bool961     pub fn contains<'a, P: Pattern<'a>>(&'a self, pat: P) -> bool {
962         pat.is_contained_in(self)
963     }
964 
965     /// Returns `true` if the given pattern matches a prefix of this
966     /// string slice.
967     ///
968     /// Returns `false` if it does not.
969     ///
970     /// The [pattern] can be a `&str`, [`char`], a slice of [`char`]s, or a
971     /// function or closure that determines if a character matches.
972     ///
973     /// [`char`]: prim@char
974     /// [pattern]: self::pattern
975     ///
976     /// # Examples
977     ///
978     /// Basic usage:
979     ///
980     /// ```
981     /// let bananas = "bananas";
982     ///
983     /// assert!(bananas.starts_with("bana"));
984     /// assert!(!bananas.starts_with("nana"));
985     /// ```
986     #[stable(feature = "rust1", since = "1.0.0")]
starts_with<'a, P: Pattern<'a>>(&'a self, pat: P) -> bool987     pub fn starts_with<'a, P: Pattern<'a>>(&'a self, pat: P) -> bool {
988         pat.is_prefix_of(self)
989     }
990 
991     /// Returns `true` if the given pattern matches a suffix of this
992     /// string slice.
993     ///
994     /// Returns `false` if it does not.
995     ///
996     /// The [pattern] can be a `&str`, [`char`], a slice of [`char`]s, or a
997     /// function or closure that determines if a character matches.
998     ///
999     /// [`char`]: prim@char
1000     /// [pattern]: self::pattern
1001     ///
1002     /// # Examples
1003     ///
1004     /// Basic usage:
1005     ///
1006     /// ```
1007     /// let bananas = "bananas";
1008     ///
1009     /// assert!(bananas.ends_with("anas"));
1010     /// assert!(!bananas.ends_with("nana"));
1011     /// ```
1012     #[stable(feature = "rust1", since = "1.0.0")]
ends_with<'a, P>(&'a self, pat: P) -> bool where P: Pattern<'a, Searcher: ReverseSearcher<'a>>,1013     pub fn ends_with<'a, P>(&'a self, pat: P) -> bool
1014     where
1015         P: Pattern<'a, Searcher: ReverseSearcher<'a>>,
1016     {
1017         pat.is_suffix_of(self)
1018     }
1019 
1020     /// Returns the byte index of the first character of this string slice that
1021     /// matches the pattern.
1022     ///
1023     /// Returns [`None`] if the pattern doesn't match.
1024     ///
1025     /// The [pattern] can be a `&str`, [`char`], a slice of [`char`]s, or a
1026     /// function or closure that determines if a character matches.
1027     ///
1028     /// [`char`]: prim@char
1029     /// [pattern]: self::pattern
1030     ///
1031     /// # Examples
1032     ///
1033     /// Simple patterns:
1034     ///
1035     /// ```
1036     /// let s = "Löwe 老虎 Léopard Gepardi";
1037     ///
1038     /// assert_eq!(s.find('L'), Some(0));
1039     /// assert_eq!(s.find('é'), Some(14));
1040     /// assert_eq!(s.find("pard"), Some(17));
1041     /// ```
1042     ///
1043     /// More complex patterns using point-free style and closures:
1044     ///
1045     /// ```
1046     /// let s = "Löwe 老虎 Léopard";
1047     ///
1048     /// assert_eq!(s.find(char::is_whitespace), Some(5));
1049     /// assert_eq!(s.find(char::is_lowercase), Some(1));
1050     /// assert_eq!(s.find(|c: char| c.is_whitespace() || c.is_lowercase()), Some(1));
1051     /// assert_eq!(s.find(|c: char| (c < 'o') && (c > 'a')), Some(4));
1052     /// ```
1053     ///
1054     /// Not finding the pattern:
1055     ///
1056     /// ```
1057     /// let s = "Löwe 老虎 Léopard";
1058     /// let x: &[_] = &['1', '2'];
1059     ///
1060     /// assert_eq!(s.find(x), None);
1061     /// ```
1062     #[stable(feature = "rust1", since = "1.0.0")]
1063     #[inline]
find<'a, P: Pattern<'a>>(&'a self, pat: P) -> Option<usize>1064     pub fn find<'a, P: Pattern<'a>>(&'a self, pat: P) -> Option<usize> {
1065         pat.into_searcher(self).next_match().map(|(i, _)| i)
1066     }
1067 
1068     /// Returns the byte index for the first character of the rightmost match of the pattern in
1069     /// this string slice.
1070     ///
1071     /// Returns [`None`] if the pattern doesn't match.
1072     ///
1073     /// The [pattern] can be a `&str`, [`char`], a slice of [`char`]s, or a
1074     /// function or closure that determines if a character matches.
1075     ///
1076     /// [`char`]: prim@char
1077     /// [pattern]: self::pattern
1078     ///
1079     /// # Examples
1080     ///
1081     /// Simple patterns:
1082     ///
1083     /// ```
1084     /// let s = "Löwe 老虎 Léopard Gepardi";
1085     ///
1086     /// assert_eq!(s.rfind('L'), Some(13));
1087     /// assert_eq!(s.rfind('é'), Some(14));
1088     /// assert_eq!(s.rfind("pard"), Some(24));
1089     /// ```
1090     ///
1091     /// More complex patterns with closures:
1092     ///
1093     /// ```
1094     /// let s = "Löwe 老虎 Léopard";
1095     ///
1096     /// assert_eq!(s.rfind(char::is_whitespace), Some(12));
1097     /// assert_eq!(s.rfind(char::is_lowercase), Some(20));
1098     /// ```
1099     ///
1100     /// Not finding the pattern:
1101     ///
1102     /// ```
1103     /// let s = "Löwe 老虎 Léopard";
1104     /// let x: &[_] = &['1', '2'];
1105     ///
1106     /// assert_eq!(s.rfind(x), None);
1107     /// ```
1108     #[stable(feature = "rust1", since = "1.0.0")]
1109     #[inline]
rfind<'a, P>(&'a self, pat: P) -> Option<usize> where P: Pattern<'a, Searcher: ReverseSearcher<'a>>,1110     pub fn rfind<'a, P>(&'a self, pat: P) -> Option<usize>
1111     where
1112         P: Pattern<'a, Searcher: ReverseSearcher<'a>>,
1113     {
1114         pat.into_searcher(self).next_match_back().map(|(i, _)| i)
1115     }
1116 
1117     /// An iterator over substrings of this string slice, separated by
1118     /// characters matched by a pattern.
1119     ///
1120     /// The [pattern] can be a `&str`, [`char`], a slice of [`char`]s, or a
1121     /// function or closure that determines if a character matches.
1122     ///
1123     /// [`char`]: prim@char
1124     /// [pattern]: self::pattern
1125     ///
1126     /// # Iterator behavior
1127     ///
1128     /// The returned iterator will be a [`DoubleEndedIterator`] if the pattern
1129     /// allows a reverse search and forward/reverse search yields the same
1130     /// elements. This is true for, e.g., [`char`], but not for `&str`.
1131     ///
1132     /// If the pattern allows a reverse search but its results might differ
1133     /// from a forward search, the [`rsplit`] method can be used.
1134     ///
1135     /// [`rsplit`]: str::rsplit
1136     ///
1137     /// # Examples
1138     ///
1139     /// Simple patterns:
1140     ///
1141     /// ```
1142     /// let v: Vec<&str> = "Mary had a little lamb".split(' ').collect();
1143     /// assert_eq!(v, ["Mary", "had", "a", "little", "lamb"]);
1144     ///
1145     /// let v: Vec<&str> = "".split('X').collect();
1146     /// assert_eq!(v, [""]);
1147     ///
1148     /// let v: Vec<&str> = "lionXXtigerXleopard".split('X').collect();
1149     /// assert_eq!(v, ["lion", "", "tiger", "leopard"]);
1150     ///
1151     /// let v: Vec<&str> = "lion::tiger::leopard".split("::").collect();
1152     /// assert_eq!(v, ["lion", "tiger", "leopard"]);
1153     ///
1154     /// let v: Vec<&str> = "abc1def2ghi".split(char::is_numeric).collect();
1155     /// assert_eq!(v, ["abc", "def", "ghi"]);
1156     ///
1157     /// let v: Vec<&str> = "lionXtigerXleopard".split(char::is_uppercase).collect();
1158     /// assert_eq!(v, ["lion", "tiger", "leopard"]);
1159     /// ```
1160     ///
1161     /// If the pattern is a slice of chars, split on each occurrence of any of the characters:
1162     ///
1163     /// ```
1164     /// let v: Vec<&str> = "2020-11-03 23:59".split(&['-', ' ', ':', '@'][..]).collect();
1165     /// assert_eq!(v, ["2020", "11", "03", "23", "59"]);
1166     /// ```
1167     ///
1168     /// A more complex pattern, using a closure:
1169     ///
1170     /// ```
1171     /// let v: Vec<&str> = "abc1defXghi".split(|c| c == '1' || c == 'X').collect();
1172     /// assert_eq!(v, ["abc", "def", "ghi"]);
1173     /// ```
1174     ///
1175     /// If a string contains multiple contiguous separators, you will end up
1176     /// with empty strings in the output:
1177     ///
1178     /// ```
1179     /// let x = "||||a||b|c".to_string();
1180     /// let d: Vec<_> = x.split('|').collect();
1181     ///
1182     /// assert_eq!(d, &["", "", "", "", "a", "", "b", "c"]);
1183     /// ```
1184     ///
1185     /// Contiguous separators are separated by the empty string.
1186     ///
1187     /// ```
1188     /// let x = "(///)".to_string();
1189     /// let d: Vec<_> = x.split('/').collect();
1190     ///
1191     /// assert_eq!(d, &["(", "", "", ")"]);
1192     /// ```
1193     ///
1194     /// Separators at the start or end of a string are neighbored
1195     /// by empty strings.
1196     ///
1197     /// ```
1198     /// let d: Vec<_> = "010".split("0").collect();
1199     /// assert_eq!(d, &["", "1", ""]);
1200     /// ```
1201     ///
1202     /// When the empty string is used as a separator, it separates
1203     /// every character in the string, along with the beginning
1204     /// and end of the string.
1205     ///
1206     /// ```
1207     /// let f: Vec<_> = "rust".split("").collect();
1208     /// assert_eq!(f, &["", "r", "u", "s", "t", ""]);
1209     /// ```
1210     ///
1211     /// Contiguous separators can lead to possibly surprising behavior
1212     /// when whitespace is used as the separator. This code is correct:
1213     ///
1214     /// ```
1215     /// let x = "    a  b c".to_string();
1216     /// let d: Vec<_> = x.split(' ').collect();
1217     ///
1218     /// assert_eq!(d, &["", "", "", "", "a", "", "b", "c"]);
1219     /// ```
1220     ///
1221     /// It does _not_ give you:
1222     ///
1223     /// ```,ignore
1224     /// assert_eq!(d, &["a", "b", "c"]);
1225     /// ```
1226     ///
1227     /// Use [`split_whitespace`] for this behavior.
1228     ///
1229     /// [`split_whitespace`]: str::split_whitespace
1230     #[stable(feature = "rust1", since = "1.0.0")]
1231     #[inline]
split<'a, P: Pattern<'a>>(&'a self, pat: P) -> Split<'a, P>1232     pub fn split<'a, P: Pattern<'a>>(&'a self, pat: P) -> Split<'a, P> {
1233         Split(SplitInternal {
1234             start: 0,
1235             end: self.len(),
1236             matcher: pat.into_searcher(self),
1237             allow_trailing_empty: true,
1238             finished: false,
1239         })
1240     }
1241 
1242     /// An iterator over substrings of this string slice, separated by
1243     /// characters matched by a pattern. Differs from the iterator produced by
1244     /// `split` in that `split_inclusive` leaves the matched part as the
1245     /// terminator of the substring.
1246     ///
1247     /// The [pattern] can be a `&str`, [`char`], a slice of [`char`]s, or a
1248     /// function or closure that determines if a character matches.
1249     ///
1250     /// [`char`]: prim@char
1251     /// [pattern]: self::pattern
1252     ///
1253     /// # Examples
1254     ///
1255     /// ```
1256     /// let v: Vec<&str> = "Mary had a little lamb\nlittle lamb\nlittle lamb."
1257     ///     .split_inclusive('\n').collect();
1258     /// assert_eq!(v, ["Mary had a little lamb\n", "little lamb\n", "little lamb."]);
1259     /// ```
1260     ///
1261     /// If the last element of the string is matched,
1262     /// that element will be considered the terminator of the preceding substring.
1263     /// That substring will be the last item returned by the iterator.
1264     ///
1265     /// ```
1266     /// let v: Vec<&str> = "Mary had a little lamb\nlittle lamb\nlittle lamb.\n"
1267     ///     .split_inclusive('\n').collect();
1268     /// assert_eq!(v, ["Mary had a little lamb\n", "little lamb\n", "little lamb.\n"]);
1269     /// ```
1270     #[stable(feature = "split_inclusive", since = "1.51.0")]
1271     #[inline]
split_inclusive<'a, P: Pattern<'a>>(&'a self, pat: P) -> SplitInclusive<'a, P>1272     pub fn split_inclusive<'a, P: Pattern<'a>>(&'a self, pat: P) -> SplitInclusive<'a, P> {
1273         SplitInclusive(SplitInternal {
1274             start: 0,
1275             end: self.len(),
1276             matcher: pat.into_searcher(self),
1277             allow_trailing_empty: false,
1278             finished: false,
1279         })
1280     }
1281 
1282     /// An iterator over substrings of the given string slice, separated by
1283     /// characters matched by a pattern and yielded in reverse order.
1284     ///
1285     /// The [pattern] can be a `&str`, [`char`], a slice of [`char`]s, or a
1286     /// function or closure that determines if a character matches.
1287     ///
1288     /// [`char`]: prim@char
1289     /// [pattern]: self::pattern
1290     ///
1291     /// # Iterator behavior
1292     ///
1293     /// The returned iterator requires that the pattern supports a reverse
1294     /// search, and it will be a [`DoubleEndedIterator`] if a forward/reverse
1295     /// search yields the same elements.
1296     ///
1297     /// For iterating from the front, the [`split`] method can be used.
1298     ///
1299     /// [`split`]: str::split
1300     ///
1301     /// # Examples
1302     ///
1303     /// Simple patterns:
1304     ///
1305     /// ```
1306     /// let v: Vec<&str> = "Mary had a little lamb".rsplit(' ').collect();
1307     /// assert_eq!(v, ["lamb", "little", "a", "had", "Mary"]);
1308     ///
1309     /// let v: Vec<&str> = "".rsplit('X').collect();
1310     /// assert_eq!(v, [""]);
1311     ///
1312     /// let v: Vec<&str> = "lionXXtigerXleopard".rsplit('X').collect();
1313     /// assert_eq!(v, ["leopard", "tiger", "", "lion"]);
1314     ///
1315     /// let v: Vec<&str> = "lion::tiger::leopard".rsplit("::").collect();
1316     /// assert_eq!(v, ["leopard", "tiger", "lion"]);
1317     /// ```
1318     ///
1319     /// A more complex pattern, using a closure:
1320     ///
1321     /// ```
1322     /// let v: Vec<&str> = "abc1defXghi".rsplit(|c| c == '1' || c == 'X').collect();
1323     /// assert_eq!(v, ["ghi", "def", "abc"]);
1324     /// ```
1325     #[stable(feature = "rust1", since = "1.0.0")]
1326     #[inline]
rsplit<'a, P>(&'a self, pat: P) -> RSplit<'a, P> where P: Pattern<'a, Searcher: ReverseSearcher<'a>>,1327     pub fn rsplit<'a, P>(&'a self, pat: P) -> RSplit<'a, P>
1328     where
1329         P: Pattern<'a, Searcher: ReverseSearcher<'a>>,
1330     {
1331         RSplit(self.split(pat).0)
1332     }
1333 
1334     /// An iterator over substrings of the given string slice, separated by
1335     /// characters matched by a pattern.
1336     ///
1337     /// The [pattern] can be a `&str`, [`char`], a slice of [`char`]s, or a
1338     /// function or closure that determines if a character matches.
1339     ///
1340     /// [`char`]: prim@char
1341     /// [pattern]: self::pattern
1342     ///
1343     /// Equivalent to [`split`], except that the trailing substring
1344     /// is skipped if empty.
1345     ///
1346     /// [`split`]: str::split
1347     ///
1348     /// This method can be used for string data that is _terminated_,
1349     /// rather than _separated_ by a pattern.
1350     ///
1351     /// # Iterator behavior
1352     ///
1353     /// The returned iterator will be a [`DoubleEndedIterator`] if the pattern
1354     /// allows a reverse search and forward/reverse search yields the same
1355     /// elements. This is true for, e.g., [`char`], but not for `&str`.
1356     ///
1357     /// If the pattern allows a reverse search but its results might differ
1358     /// from a forward search, the [`rsplit_terminator`] method can be used.
1359     ///
1360     /// [`rsplit_terminator`]: str::rsplit_terminator
1361     ///
1362     /// # Examples
1363     ///
1364     /// Basic usage:
1365     ///
1366     /// ```
1367     /// let v: Vec<&str> = "A.B.".split_terminator('.').collect();
1368     /// assert_eq!(v, ["A", "B"]);
1369     ///
1370     /// let v: Vec<&str> = "A..B..".split_terminator(".").collect();
1371     /// assert_eq!(v, ["A", "", "B", ""]);
1372     ///
1373     /// let v: Vec<&str> = "A.B:C.D".split_terminator(&['.', ':'][..]).collect();
1374     /// assert_eq!(v, ["A", "B", "C", "D"]);
1375     /// ```
1376     #[stable(feature = "rust1", since = "1.0.0")]
1377     #[inline]
split_terminator<'a, P: Pattern<'a>>(&'a self, pat: P) -> SplitTerminator<'a, P>1378     pub fn split_terminator<'a, P: Pattern<'a>>(&'a self, pat: P) -> SplitTerminator<'a, P> {
1379         SplitTerminator(SplitInternal { allow_trailing_empty: false, ..self.split(pat).0 })
1380     }
1381 
1382     /// An iterator over substrings of `self`, separated by characters
1383     /// matched by a pattern and yielded in reverse order.
1384     ///
1385     /// The [pattern] can be a `&str`, [`char`], a slice of [`char`]s, or a
1386     /// function or closure that determines if a character matches.
1387     ///
1388     /// [`char`]: prim@char
1389     /// [pattern]: self::pattern
1390     ///
1391     /// Equivalent to [`split`], except that the trailing substring is
1392     /// skipped if empty.
1393     ///
1394     /// [`split`]: str::split
1395     ///
1396     /// This method can be used for string data that is _terminated_,
1397     /// rather than _separated_ by a pattern.
1398     ///
1399     /// # Iterator behavior
1400     ///
1401     /// The returned iterator requires that the pattern supports a
1402     /// reverse search, and it will be double ended if a forward/reverse
1403     /// search yields the same elements.
1404     ///
1405     /// For iterating from the front, the [`split_terminator`] method can be
1406     /// used.
1407     ///
1408     /// [`split_terminator`]: str::split_terminator
1409     ///
1410     /// # Examples
1411     ///
1412     /// ```
1413     /// let v: Vec<&str> = "A.B.".rsplit_terminator('.').collect();
1414     /// assert_eq!(v, ["B", "A"]);
1415     ///
1416     /// let v: Vec<&str> = "A..B..".rsplit_terminator(".").collect();
1417     /// assert_eq!(v, ["", "B", "", "A"]);
1418     ///
1419     /// let v: Vec<&str> = "A.B:C.D".rsplit_terminator(&['.', ':'][..]).collect();
1420     /// assert_eq!(v, ["D", "C", "B", "A"]);
1421     /// ```
1422     #[stable(feature = "rust1", since = "1.0.0")]
1423     #[inline]
rsplit_terminator<'a, P>(&'a self, pat: P) -> RSplitTerminator<'a, P> where P: Pattern<'a, Searcher: ReverseSearcher<'a>>,1424     pub fn rsplit_terminator<'a, P>(&'a self, pat: P) -> RSplitTerminator<'a, P>
1425     where
1426         P: Pattern<'a, Searcher: ReverseSearcher<'a>>,
1427     {
1428         RSplitTerminator(self.split_terminator(pat).0)
1429     }
1430 
1431     /// An iterator over substrings of the given string slice, separated by a
1432     /// pattern, restricted to returning at most `n` items.
1433     ///
1434     /// If `n` substrings are returned, the last substring (the `n`th substring)
1435     /// will contain the remainder of the string.
1436     ///
1437     /// The [pattern] can be a `&str`, [`char`], a slice of [`char`]s, or a
1438     /// function or closure that determines if a character matches.
1439     ///
1440     /// [`char`]: prim@char
1441     /// [pattern]: self::pattern
1442     ///
1443     /// # Iterator behavior
1444     ///
1445     /// The returned iterator will not be double ended, because it is
1446     /// not efficient to support.
1447     ///
1448     /// If the pattern allows a reverse search, the [`rsplitn`] method can be
1449     /// used.
1450     ///
1451     /// [`rsplitn`]: str::rsplitn
1452     ///
1453     /// # Examples
1454     ///
1455     /// Simple patterns:
1456     ///
1457     /// ```
1458     /// let v: Vec<&str> = "Mary had a little lambda".splitn(3, ' ').collect();
1459     /// assert_eq!(v, ["Mary", "had", "a little lambda"]);
1460     ///
1461     /// let v: Vec<&str> = "lionXXtigerXleopard".splitn(3, "X").collect();
1462     /// assert_eq!(v, ["lion", "", "tigerXleopard"]);
1463     ///
1464     /// let v: Vec<&str> = "abcXdef".splitn(1, 'X').collect();
1465     /// assert_eq!(v, ["abcXdef"]);
1466     ///
1467     /// let v: Vec<&str> = "".splitn(1, 'X').collect();
1468     /// assert_eq!(v, [""]);
1469     /// ```
1470     ///
1471     /// A more complex pattern, using a closure:
1472     ///
1473     /// ```
1474     /// let v: Vec<&str> = "abc1defXghi".splitn(2, |c| c == '1' || c == 'X').collect();
1475     /// assert_eq!(v, ["abc", "defXghi"]);
1476     /// ```
1477     #[stable(feature = "rust1", since = "1.0.0")]
1478     #[inline]
splitn<'a, P: Pattern<'a>>(&'a self, n: usize, pat: P) -> SplitN<'a, P>1479     pub fn splitn<'a, P: Pattern<'a>>(&'a self, n: usize, pat: P) -> SplitN<'a, P> {
1480         SplitN(SplitNInternal { iter: self.split(pat).0, count: n })
1481     }
1482 
1483     /// An iterator over substrings of this string slice, separated by a
1484     /// pattern, starting from the end of the string, restricted to returning
1485     /// at most `n` items.
1486     ///
1487     /// If `n` substrings are returned, the last substring (the `n`th substring)
1488     /// will contain the remainder of the string.
1489     ///
1490     /// The [pattern] can be a `&str`, [`char`], a slice of [`char`]s, or a
1491     /// function or closure that determines if a character matches.
1492     ///
1493     /// [`char`]: prim@char
1494     /// [pattern]: self::pattern
1495     ///
1496     /// # Iterator behavior
1497     ///
1498     /// The returned iterator will not be double ended, because it is not
1499     /// efficient to support.
1500     ///
1501     /// For splitting from the front, the [`splitn`] method can be used.
1502     ///
1503     /// [`splitn`]: str::splitn
1504     ///
1505     /// # Examples
1506     ///
1507     /// Simple patterns:
1508     ///
1509     /// ```
1510     /// let v: Vec<&str> = "Mary had a little lamb".rsplitn(3, ' ').collect();
1511     /// assert_eq!(v, ["lamb", "little", "Mary had a"]);
1512     ///
1513     /// let v: Vec<&str> = "lionXXtigerXleopard".rsplitn(3, 'X').collect();
1514     /// assert_eq!(v, ["leopard", "tiger", "lionX"]);
1515     ///
1516     /// let v: Vec<&str> = "lion::tiger::leopard".rsplitn(2, "::").collect();
1517     /// assert_eq!(v, ["leopard", "lion::tiger"]);
1518     /// ```
1519     ///
1520     /// A more complex pattern, using a closure:
1521     ///
1522     /// ```
1523     /// let v: Vec<&str> = "abc1defXghi".rsplitn(2, |c| c == '1' || c == 'X').collect();
1524     /// assert_eq!(v, ["ghi", "abc1def"]);
1525     /// ```
1526     #[stable(feature = "rust1", since = "1.0.0")]
1527     #[inline]
rsplitn<'a, P>(&'a self, n: usize, pat: P) -> RSplitN<'a, P> where P: Pattern<'a, Searcher: ReverseSearcher<'a>>,1528     pub fn rsplitn<'a, P>(&'a self, n: usize, pat: P) -> RSplitN<'a, P>
1529     where
1530         P: Pattern<'a, Searcher: ReverseSearcher<'a>>,
1531     {
1532         RSplitN(self.splitn(n, pat).0)
1533     }
1534 
1535     /// Splits the string on the first occurrence of the specified delimiter and
1536     /// returns prefix before delimiter and suffix after delimiter.
1537     ///
1538     /// # Examples
1539     ///
1540     /// ```
1541     /// assert_eq!("cfg".split_once('='), None);
1542     /// assert_eq!("cfg=foo".split_once('='), Some(("cfg", "foo")));
1543     /// assert_eq!("cfg=foo=bar".split_once('='), Some(("cfg", "foo=bar")));
1544     /// ```
1545     #[stable(feature = "str_split_once", since = "1.52.0")]
1546     #[inline]
split_once<'a, P: Pattern<'a>>(&'a self, delimiter: P) -> Option<(&'a str, &'a str)>1547     pub fn split_once<'a, P: Pattern<'a>>(&'a self, delimiter: P) -> Option<(&'a str, &'a str)> {
1548         let (start, end) = delimiter.into_searcher(self).next_match()?;
1549         // SAFETY: `Searcher` is known to return valid indices.
1550         unsafe { Some((self.get_unchecked(..start), self.get_unchecked(end..))) }
1551     }
1552 
1553     /// Splits the string on the last occurrence of the specified delimiter and
1554     /// returns prefix before delimiter and suffix after delimiter.
1555     ///
1556     /// # Examples
1557     ///
1558     /// ```
1559     /// assert_eq!("cfg".rsplit_once('='), None);
1560     /// assert_eq!("cfg=foo".rsplit_once('='), Some(("cfg", "foo")));
1561     /// assert_eq!("cfg=foo=bar".rsplit_once('='), Some(("cfg=foo", "bar")));
1562     /// ```
1563     #[stable(feature = "str_split_once", since = "1.52.0")]
1564     #[inline]
rsplit_once<'a, P>(&'a self, delimiter: P) -> Option<(&'a str, &'a str)> where P: Pattern<'a, Searcher: ReverseSearcher<'a>>,1565     pub fn rsplit_once<'a, P>(&'a self, delimiter: P) -> Option<(&'a str, &'a str)>
1566     where
1567         P: Pattern<'a, Searcher: ReverseSearcher<'a>>,
1568     {
1569         let (start, end) = delimiter.into_searcher(self).next_match_back()?;
1570         // SAFETY: `Searcher` is known to return valid indices.
1571         unsafe { Some((self.get_unchecked(..start), self.get_unchecked(end..))) }
1572     }
1573 
1574     /// An iterator over the disjoint matches of a pattern within the given string
1575     /// slice.
1576     ///
1577     /// The [pattern] can be a `&str`, [`char`], a slice of [`char`]s, or a
1578     /// function or closure that determines if a character matches.
1579     ///
1580     /// [`char`]: prim@char
1581     /// [pattern]: self::pattern
1582     ///
1583     /// # Iterator behavior
1584     ///
1585     /// The returned iterator will be a [`DoubleEndedIterator`] if the pattern
1586     /// allows a reverse search and forward/reverse search yields the same
1587     /// elements. This is true for, e.g., [`char`], but not for `&str`.
1588     ///
1589     /// If the pattern allows a reverse search but its results might differ
1590     /// from a forward search, the [`rmatches`] method can be used.
1591     ///
1592     /// [`rmatches`]: str::matches
1593     ///
1594     /// # Examples
1595     ///
1596     /// Basic usage:
1597     ///
1598     /// ```
1599     /// let v: Vec<&str> = "abcXXXabcYYYabc".matches("abc").collect();
1600     /// assert_eq!(v, ["abc", "abc", "abc"]);
1601     ///
1602     /// let v: Vec<&str> = "1abc2abc3".matches(char::is_numeric).collect();
1603     /// assert_eq!(v, ["1", "2", "3"]);
1604     /// ```
1605     #[stable(feature = "str_matches", since = "1.2.0")]
1606     #[inline]
matches<'a, P: Pattern<'a>>(&'a self, pat: P) -> Matches<'a, P>1607     pub fn matches<'a, P: Pattern<'a>>(&'a self, pat: P) -> Matches<'a, P> {
1608         Matches(MatchesInternal(pat.into_searcher(self)))
1609     }
1610 
1611     /// An iterator over the disjoint matches of a pattern within this string slice,
1612     /// yielded in reverse order.
1613     ///
1614     /// The [pattern] can be a `&str`, [`char`], a slice of [`char`]s, or a
1615     /// function or closure that determines if a character matches.
1616     ///
1617     /// [`char`]: prim@char
1618     /// [pattern]: self::pattern
1619     ///
1620     /// # Iterator behavior
1621     ///
1622     /// The returned iterator requires that the pattern supports a reverse
1623     /// search, and it will be a [`DoubleEndedIterator`] if a forward/reverse
1624     /// search yields the same elements.
1625     ///
1626     /// For iterating from the front, the [`matches`] method can be used.
1627     ///
1628     /// [`matches`]: str::matches
1629     ///
1630     /// # Examples
1631     ///
1632     /// Basic usage:
1633     ///
1634     /// ```
1635     /// let v: Vec<&str> = "abcXXXabcYYYabc".rmatches("abc").collect();
1636     /// assert_eq!(v, ["abc", "abc", "abc"]);
1637     ///
1638     /// let v: Vec<&str> = "1abc2abc3".rmatches(char::is_numeric).collect();
1639     /// assert_eq!(v, ["3", "2", "1"]);
1640     /// ```
1641     #[stable(feature = "str_matches", since = "1.2.0")]
1642     #[inline]
rmatches<'a, P>(&'a self, pat: P) -> RMatches<'a, P> where P: Pattern<'a, Searcher: ReverseSearcher<'a>>,1643     pub fn rmatches<'a, P>(&'a self, pat: P) -> RMatches<'a, P>
1644     where
1645         P: Pattern<'a, Searcher: ReverseSearcher<'a>>,
1646     {
1647         RMatches(self.matches(pat).0)
1648     }
1649 
1650     /// An iterator over the disjoint matches of a pattern within this string
1651     /// slice as well as the index that the match starts at.
1652     ///
1653     /// For matches of `pat` within `self` that overlap, only the indices
1654     /// corresponding to the first match are returned.
1655     ///
1656     /// The [pattern] can be a `&str`, [`char`], a slice of [`char`]s, or a
1657     /// function or closure that determines if a character matches.
1658     ///
1659     /// [`char`]: prim@char
1660     /// [pattern]: self::pattern
1661     ///
1662     /// # Iterator behavior
1663     ///
1664     /// The returned iterator will be a [`DoubleEndedIterator`] if the pattern
1665     /// allows a reverse search and forward/reverse search yields the same
1666     /// elements. This is true for, e.g., [`char`], but not for `&str`.
1667     ///
1668     /// If the pattern allows a reverse search but its results might differ
1669     /// from a forward search, the [`rmatch_indices`] method can be used.
1670     ///
1671     /// [`rmatch_indices`]: str::rmatch_indices
1672     ///
1673     /// # Examples
1674     ///
1675     /// Basic usage:
1676     ///
1677     /// ```
1678     /// let v: Vec<_> = "abcXXXabcYYYabc".match_indices("abc").collect();
1679     /// assert_eq!(v, [(0, "abc"), (6, "abc"), (12, "abc")]);
1680     ///
1681     /// let v: Vec<_> = "1abcabc2".match_indices("abc").collect();
1682     /// assert_eq!(v, [(1, "abc"), (4, "abc")]);
1683     ///
1684     /// let v: Vec<_> = "ababa".match_indices("aba").collect();
1685     /// assert_eq!(v, [(0, "aba")]); // only the first `aba`
1686     /// ```
1687     #[stable(feature = "str_match_indices", since = "1.5.0")]
1688     #[inline]
match_indices<'a, P: Pattern<'a>>(&'a self, pat: P) -> MatchIndices<'a, P>1689     pub fn match_indices<'a, P: Pattern<'a>>(&'a self, pat: P) -> MatchIndices<'a, P> {
1690         MatchIndices(MatchIndicesInternal(pat.into_searcher(self)))
1691     }
1692 
1693     /// An iterator over the disjoint matches of a pattern within `self`,
1694     /// yielded in reverse order along with the index of the match.
1695     ///
1696     /// For matches of `pat` within `self` that overlap, only the indices
1697     /// corresponding to the last match are returned.
1698     ///
1699     /// The [pattern] can be a `&str`, [`char`], a slice of [`char`]s, or a
1700     /// function or closure that determines if a character matches.
1701     ///
1702     /// [`char`]: prim@char
1703     /// [pattern]: self::pattern
1704     ///
1705     /// # Iterator behavior
1706     ///
1707     /// The returned iterator requires that the pattern supports a reverse
1708     /// search, and it will be a [`DoubleEndedIterator`] if a forward/reverse
1709     /// search yields the same elements.
1710     ///
1711     /// For iterating from the front, the [`match_indices`] method can be used.
1712     ///
1713     /// [`match_indices`]: str::match_indices
1714     ///
1715     /// # Examples
1716     ///
1717     /// Basic usage:
1718     ///
1719     /// ```
1720     /// let v: Vec<_> = "abcXXXabcYYYabc".rmatch_indices("abc").collect();
1721     /// assert_eq!(v, [(12, "abc"), (6, "abc"), (0, "abc")]);
1722     ///
1723     /// let v: Vec<_> = "1abcabc2".rmatch_indices("abc").collect();
1724     /// assert_eq!(v, [(4, "abc"), (1, "abc")]);
1725     ///
1726     /// let v: Vec<_> = "ababa".rmatch_indices("aba").collect();
1727     /// assert_eq!(v, [(2, "aba")]); // only the last `aba`
1728     /// ```
1729     #[stable(feature = "str_match_indices", since = "1.5.0")]
1730     #[inline]
rmatch_indices<'a, P>(&'a self, pat: P) -> RMatchIndices<'a, P> where P: Pattern<'a, Searcher: ReverseSearcher<'a>>,1731     pub fn rmatch_indices<'a, P>(&'a self, pat: P) -> RMatchIndices<'a, P>
1732     where
1733         P: Pattern<'a, Searcher: ReverseSearcher<'a>>,
1734     {
1735         RMatchIndices(self.match_indices(pat).0)
1736     }
1737 
1738     /// Returns a string slice with leading and trailing whitespace removed.
1739     ///
1740     /// 'Whitespace' is defined according to the terms of the Unicode Derived
1741     /// Core Property `White_Space`.
1742     ///
1743     /// # Examples
1744     ///
1745     /// Basic usage:
1746     ///
1747     /// ```
1748     /// let s = " Hello\tworld\t";
1749     ///
1750     /// assert_eq!("Hello\tworld", s.trim());
1751     /// ```
1752     #[inline]
1753     #[must_use = "this returns the trimmed string as a slice, \
1754                   without modifying the original"]
1755     #[stable(feature = "rust1", since = "1.0.0")]
trim(&self) -> &str1756     pub fn trim(&self) -> &str {
1757         self.trim_matches(|c: char| c.is_whitespace())
1758     }
1759 
1760     /// Returns a string slice with leading whitespace removed.
1761     ///
1762     /// 'Whitespace' is defined according to the terms of the Unicode Derived
1763     /// Core Property `White_Space`.
1764     ///
1765     /// # Text directionality
1766     ///
1767     /// A string is a sequence of bytes. `start` in this context means the first
1768     /// position of that byte string; for a left-to-right language like English or
1769     /// Russian, this will be left side, and for right-to-left languages like
1770     /// Arabic or Hebrew, this will be the right side.
1771     ///
1772     /// # Examples
1773     ///
1774     /// Basic usage:
1775     ///
1776     /// ```
1777     /// let s = " Hello\tworld\t";
1778     /// assert_eq!("Hello\tworld\t", s.trim_start());
1779     /// ```
1780     ///
1781     /// Directionality:
1782     ///
1783     /// ```
1784     /// let s = "  English  ";
1785     /// assert!(Some('E') == s.trim_start().chars().next());
1786     ///
1787     /// let s = "  עברית  ";
1788     /// assert!(Some('ע') == s.trim_start().chars().next());
1789     /// ```
1790     #[inline]
1791     #[must_use = "this returns the trimmed string as a new slice, \
1792                   without modifying the original"]
1793     #[stable(feature = "trim_direction", since = "1.30.0")]
trim_start(&self) -> &str1794     pub fn trim_start(&self) -> &str {
1795         self.trim_start_matches(|c: char| c.is_whitespace())
1796     }
1797 
1798     /// Returns a string slice with trailing whitespace removed.
1799     ///
1800     /// 'Whitespace' is defined according to the terms of the Unicode Derived
1801     /// Core Property `White_Space`.
1802     ///
1803     /// # Text directionality
1804     ///
1805     /// A string is a sequence of bytes. `end` in this context means the last
1806     /// position of that byte string; for a left-to-right language like English or
1807     /// Russian, this will be right side, and for right-to-left languages like
1808     /// Arabic or Hebrew, this will be the left side.
1809     ///
1810     /// # Examples
1811     ///
1812     /// Basic usage:
1813     ///
1814     /// ```
1815     /// let s = " Hello\tworld\t";
1816     /// assert_eq!(" Hello\tworld", s.trim_end());
1817     /// ```
1818     ///
1819     /// Directionality:
1820     ///
1821     /// ```
1822     /// let s = "  English  ";
1823     /// assert!(Some('h') == s.trim_end().chars().rev().next());
1824     ///
1825     /// let s = "  עברית  ";
1826     /// assert!(Some('ת') == s.trim_end().chars().rev().next());
1827     /// ```
1828     #[inline]
1829     #[must_use = "this returns the trimmed string as a new slice, \
1830                   without modifying the original"]
1831     #[stable(feature = "trim_direction", since = "1.30.0")]
trim_end(&self) -> &str1832     pub fn trim_end(&self) -> &str {
1833         self.trim_end_matches(|c: char| c.is_whitespace())
1834     }
1835 
1836     /// Returns a string slice with leading whitespace removed.
1837     ///
1838     /// 'Whitespace' is defined according to the terms of the Unicode Derived
1839     /// Core Property `White_Space`.
1840     ///
1841     /// # Text directionality
1842     ///
1843     /// A string is a sequence of bytes. 'Left' in this context means the first
1844     /// position of that byte string; for a language like Arabic or Hebrew
1845     /// which are 'right to left' rather than 'left to right', this will be
1846     /// the _right_ side, not the left.
1847     ///
1848     /// # Examples
1849     ///
1850     /// Basic usage:
1851     ///
1852     /// ```
1853     /// let s = " Hello\tworld\t";
1854     ///
1855     /// assert_eq!("Hello\tworld\t", s.trim_left());
1856     /// ```
1857     ///
1858     /// Directionality:
1859     ///
1860     /// ```
1861     /// let s = "  English";
1862     /// assert!(Some('E') == s.trim_left().chars().next());
1863     ///
1864     /// let s = "  עברית";
1865     /// assert!(Some('ע') == s.trim_left().chars().next());
1866     /// ```
1867     #[must_use = "this returns the trimmed string as a new slice, \
1868                   without modifying the original"]
1869     #[inline]
1870     #[stable(feature = "rust1", since = "1.0.0")]
1871     #[rustc_deprecated(
1872         since = "1.33.0",
1873         reason = "superseded by `trim_start`",
1874         suggestion = "trim_start"
1875     )]
trim_left(&self) -> &str1876     pub fn trim_left(&self) -> &str {
1877         self.trim_start()
1878     }
1879 
1880     /// Returns a string slice with trailing whitespace removed.
1881     ///
1882     /// 'Whitespace' is defined according to the terms of the Unicode Derived
1883     /// Core Property `White_Space`.
1884     ///
1885     /// # Text directionality
1886     ///
1887     /// A string is a sequence of bytes. 'Right' in this context means the last
1888     /// position of that byte string; for a language like Arabic or Hebrew
1889     /// which are 'right to left' rather than 'left to right', this will be
1890     /// the _left_ side, not the right.
1891     ///
1892     /// # Examples
1893     ///
1894     /// Basic usage:
1895     ///
1896     /// ```
1897     /// let s = " Hello\tworld\t";
1898     ///
1899     /// assert_eq!(" Hello\tworld", s.trim_right());
1900     /// ```
1901     ///
1902     /// Directionality:
1903     ///
1904     /// ```
1905     /// let s = "English  ";
1906     /// assert!(Some('h') == s.trim_right().chars().rev().next());
1907     ///
1908     /// let s = "עברית  ";
1909     /// assert!(Some('ת') == s.trim_right().chars().rev().next());
1910     /// ```
1911     #[must_use = "this returns the trimmed string as a new slice, \
1912                   without modifying the original"]
1913     #[inline]
1914     #[stable(feature = "rust1", since = "1.0.0")]
1915     #[rustc_deprecated(
1916         since = "1.33.0",
1917         reason = "superseded by `trim_end`",
1918         suggestion = "trim_end"
1919     )]
trim_right(&self) -> &str1920     pub fn trim_right(&self) -> &str {
1921         self.trim_end()
1922     }
1923 
1924     /// Returns a string slice with all prefixes and suffixes that match a
1925     /// pattern repeatedly removed.
1926     ///
1927     /// The [pattern] can be a [`char`], a slice of [`char`]s, or a function
1928     /// or closure that determines if a character matches.
1929     ///
1930     /// [`char`]: prim@char
1931     /// [pattern]: self::pattern
1932     ///
1933     /// # Examples
1934     ///
1935     /// Simple patterns:
1936     ///
1937     /// ```
1938     /// assert_eq!("11foo1bar11".trim_matches('1'), "foo1bar");
1939     /// assert_eq!("123foo1bar123".trim_matches(char::is_numeric), "foo1bar");
1940     ///
1941     /// let x: &[_] = &['1', '2'];
1942     /// assert_eq!("12foo1bar12".trim_matches(x), "foo1bar");
1943     /// ```
1944     ///
1945     /// A more complex pattern, using a closure:
1946     ///
1947     /// ```
1948     /// assert_eq!("1foo1barXX".trim_matches(|c| c == '1' || c == 'X'), "foo1bar");
1949     /// ```
1950     #[must_use = "this returns the trimmed string as a new slice, \
1951                   without modifying the original"]
1952     #[stable(feature = "rust1", since = "1.0.0")]
trim_matches<'a, P>(&'a self, pat: P) -> &'a str where P: Pattern<'a, Searcher: DoubleEndedSearcher<'a>>,1953     pub fn trim_matches<'a, P>(&'a self, pat: P) -> &'a str
1954     where
1955         P: Pattern<'a, Searcher: DoubleEndedSearcher<'a>>,
1956     {
1957         let mut i = 0;
1958         let mut j = 0;
1959         let mut matcher = pat.into_searcher(self);
1960         if let Some((a, b)) = matcher.next_reject() {
1961             i = a;
1962             j = b; // Remember earliest known match, correct it below if
1963             // last match is different
1964         }
1965         if let Some((_, b)) = matcher.next_reject_back() {
1966             j = b;
1967         }
1968         // SAFETY: `Searcher` is known to return valid indices.
1969         unsafe { self.get_unchecked(i..j) }
1970     }
1971 
1972     /// Returns a string slice with all prefixes that match a pattern
1973     /// repeatedly removed.
1974     ///
1975     /// The [pattern] can be a `&str`, [`char`], a slice of [`char`]s, or a
1976     /// function or closure that determines if a character matches.
1977     ///
1978     /// [`char`]: prim@char
1979     /// [pattern]: self::pattern
1980     ///
1981     /// # Text directionality
1982     ///
1983     /// A string is a sequence of bytes. `start` in this context means the first
1984     /// position of that byte string; for a left-to-right language like English or
1985     /// Russian, this will be left side, and for right-to-left languages like
1986     /// Arabic or Hebrew, this will be the right side.
1987     ///
1988     /// # Examples
1989     ///
1990     /// Basic usage:
1991     ///
1992     /// ```
1993     /// assert_eq!("11foo1bar11".trim_start_matches('1'), "foo1bar11");
1994     /// assert_eq!("123foo1bar123".trim_start_matches(char::is_numeric), "foo1bar123");
1995     ///
1996     /// let x: &[_] = &['1', '2'];
1997     /// assert_eq!("12foo1bar12".trim_start_matches(x), "foo1bar12");
1998     /// ```
1999     #[must_use = "this returns the trimmed string as a new slice, \
2000                   without modifying the original"]
2001     #[stable(feature = "trim_direction", since = "1.30.0")]
trim_start_matches<'a, P: Pattern<'a>>(&'a self, pat: P) -> &'a str2002     pub fn trim_start_matches<'a, P: Pattern<'a>>(&'a self, pat: P) -> &'a str {
2003         let mut i = self.len();
2004         let mut matcher = pat.into_searcher(self);
2005         if let Some((a, _)) = matcher.next_reject() {
2006             i = a;
2007         }
2008         // SAFETY: `Searcher` is known to return valid indices.
2009         unsafe { self.get_unchecked(i..self.len()) }
2010     }
2011 
2012     /// Returns a string slice with the prefix removed.
2013     ///
2014     /// If the string starts with the pattern `prefix`, returns substring after the prefix, wrapped
2015     /// in `Some`.  Unlike `trim_start_matches`, this method removes the prefix exactly once.
2016     ///
2017     /// If the string does not start with `prefix`, returns `None`.
2018     ///
2019     /// The [pattern] can be a `&str`, [`char`], a slice of [`char`]s, or a
2020     /// function or closure that determines if a character matches.
2021     ///
2022     /// [`char`]: prim@char
2023     /// [pattern]: self::pattern
2024     ///
2025     /// # Examples
2026     ///
2027     /// ```
2028     /// assert_eq!("foo:bar".strip_prefix("foo:"), Some("bar"));
2029     /// assert_eq!("foo:bar".strip_prefix("bar"), None);
2030     /// assert_eq!("foofoo".strip_prefix("foo"), Some("foo"));
2031     /// ```
2032     #[must_use = "this returns the remaining substring as a new slice, \
2033                   without modifying the original"]
2034     #[stable(feature = "str_strip", since = "1.45.0")]
strip_prefix<'a, P: Pattern<'a>>(&'a self, prefix: P) -> Option<&'a str>2035     pub fn strip_prefix<'a, P: Pattern<'a>>(&'a self, prefix: P) -> Option<&'a str> {
2036         prefix.strip_prefix_of(self)
2037     }
2038 
2039     /// Returns a string slice with the suffix removed.
2040     ///
2041     /// If the string ends with the pattern `suffix`, returns the substring before the suffix,
2042     /// wrapped in `Some`.  Unlike `trim_end_matches`, this method removes the suffix exactly once.
2043     ///
2044     /// If the string does not end with `suffix`, returns `None`.
2045     ///
2046     /// The [pattern] can be a `&str`, [`char`], a slice of [`char`]s, or a
2047     /// function or closure that determines if a character matches.
2048     ///
2049     /// [`char`]: prim@char
2050     /// [pattern]: self::pattern
2051     ///
2052     /// # Examples
2053     ///
2054     /// ```
2055     /// assert_eq!("bar:foo".strip_suffix(":foo"), Some("bar"));
2056     /// assert_eq!("bar:foo".strip_suffix("bar"), None);
2057     /// assert_eq!("foofoo".strip_suffix("foo"), Some("foo"));
2058     /// ```
2059     #[must_use = "this returns the remaining substring as a new slice, \
2060                   without modifying the original"]
2061     #[stable(feature = "str_strip", since = "1.45.0")]
strip_suffix<'a, P>(&'a self, suffix: P) -> Option<&'a str> where P: Pattern<'a>, <P as Pattern<'a>>::Searcher: ReverseSearcher<'a>,2062     pub fn strip_suffix<'a, P>(&'a self, suffix: P) -> Option<&'a str>
2063     where
2064         P: Pattern<'a>,
2065         <P as Pattern<'a>>::Searcher: ReverseSearcher<'a>,
2066     {
2067         suffix.strip_suffix_of(self)
2068     }
2069 
2070     /// Returns a string slice with all suffixes that match a pattern
2071     /// repeatedly removed.
2072     ///
2073     /// The [pattern] can be a `&str`, [`char`], a slice of [`char`]s, or a
2074     /// function or closure that determines if a character matches.
2075     ///
2076     /// [`char`]: prim@char
2077     /// [pattern]: self::pattern
2078     ///
2079     /// # Text directionality
2080     ///
2081     /// A string is a sequence of bytes. `end` in this context means the last
2082     /// position of that byte string; for a left-to-right language like English or
2083     /// Russian, this will be right side, and for right-to-left languages like
2084     /// Arabic or Hebrew, this will be the left side.
2085     ///
2086     /// # Examples
2087     ///
2088     /// Simple patterns:
2089     ///
2090     /// ```
2091     /// assert_eq!("11foo1bar11".trim_end_matches('1'), "11foo1bar");
2092     /// assert_eq!("123foo1bar123".trim_end_matches(char::is_numeric), "123foo1bar");
2093     ///
2094     /// let x: &[_] = &['1', '2'];
2095     /// assert_eq!("12foo1bar12".trim_end_matches(x), "12foo1bar");
2096     /// ```
2097     ///
2098     /// A more complex pattern, using a closure:
2099     ///
2100     /// ```
2101     /// assert_eq!("1fooX".trim_end_matches(|c| c == '1' || c == 'X'), "1foo");
2102     /// ```
2103     #[must_use = "this returns the trimmed string as a new slice, \
2104                   without modifying the original"]
2105     #[stable(feature = "trim_direction", since = "1.30.0")]
trim_end_matches<'a, P>(&'a self, pat: P) -> &'a str where P: Pattern<'a, Searcher: ReverseSearcher<'a>>,2106     pub fn trim_end_matches<'a, P>(&'a self, pat: P) -> &'a str
2107     where
2108         P: Pattern<'a, Searcher: ReverseSearcher<'a>>,
2109     {
2110         let mut j = 0;
2111         let mut matcher = pat.into_searcher(self);
2112         if let Some((_, b)) = matcher.next_reject_back() {
2113             j = b;
2114         }
2115         // SAFETY: `Searcher` is known to return valid indices.
2116         unsafe { self.get_unchecked(0..j) }
2117     }
2118 
2119     /// Returns a string slice with all prefixes that match a pattern
2120     /// repeatedly removed.
2121     ///
2122     /// The [pattern] can be a `&str`, [`char`], a slice of [`char`]s, or a
2123     /// function or closure that determines if a character matches.
2124     ///
2125     /// [`char`]: prim@char
2126     /// [pattern]: self::pattern
2127     ///
2128     /// # Text directionality
2129     ///
2130     /// A string is a sequence of bytes. 'Left' in this context means the first
2131     /// position of that byte string; for a language like Arabic or Hebrew
2132     /// which are 'right to left' rather than 'left to right', this will be
2133     /// the _right_ side, not the left.
2134     ///
2135     /// # Examples
2136     ///
2137     /// Basic usage:
2138     ///
2139     /// ```
2140     /// assert_eq!("11foo1bar11".trim_left_matches('1'), "foo1bar11");
2141     /// assert_eq!("123foo1bar123".trim_left_matches(char::is_numeric), "foo1bar123");
2142     ///
2143     /// let x: &[_] = &['1', '2'];
2144     /// assert_eq!("12foo1bar12".trim_left_matches(x), "foo1bar12");
2145     /// ```
2146     #[stable(feature = "rust1", since = "1.0.0")]
2147     #[rustc_deprecated(
2148         since = "1.33.0",
2149         reason = "superseded by `trim_start_matches`",
2150         suggestion = "trim_start_matches"
2151     )]
trim_left_matches<'a, P: Pattern<'a>>(&'a self, pat: P) -> &'a str2152     pub fn trim_left_matches<'a, P: Pattern<'a>>(&'a self, pat: P) -> &'a str {
2153         self.trim_start_matches(pat)
2154     }
2155 
2156     /// Returns a string slice with all suffixes that match a pattern
2157     /// repeatedly removed.
2158     ///
2159     /// The [pattern] can be a `&str`, [`char`], a slice of [`char`]s, or a
2160     /// function or closure that determines if a character matches.
2161     ///
2162     /// [`char`]: prim@char
2163     /// [pattern]: self::pattern
2164     ///
2165     /// # Text directionality
2166     ///
2167     /// A string is a sequence of bytes. 'Right' in this context means the last
2168     /// position of that byte string; for a language like Arabic or Hebrew
2169     /// which are 'right to left' rather than 'left to right', this will be
2170     /// the _left_ side, not the right.
2171     ///
2172     /// # Examples
2173     ///
2174     /// Simple patterns:
2175     ///
2176     /// ```
2177     /// assert_eq!("11foo1bar11".trim_right_matches('1'), "11foo1bar");
2178     /// assert_eq!("123foo1bar123".trim_right_matches(char::is_numeric), "123foo1bar");
2179     ///
2180     /// let x: &[_] = &['1', '2'];
2181     /// assert_eq!("12foo1bar12".trim_right_matches(x), "12foo1bar");
2182     /// ```
2183     ///
2184     /// A more complex pattern, using a closure:
2185     ///
2186     /// ```
2187     /// assert_eq!("1fooX".trim_right_matches(|c| c == '1' || c == 'X'), "1foo");
2188     /// ```
2189     #[stable(feature = "rust1", since = "1.0.0")]
2190     #[rustc_deprecated(
2191         since = "1.33.0",
2192         reason = "superseded by `trim_end_matches`",
2193         suggestion = "trim_end_matches"
2194     )]
trim_right_matches<'a, P>(&'a self, pat: P) -> &'a str where P: Pattern<'a, Searcher: ReverseSearcher<'a>>,2195     pub fn trim_right_matches<'a, P>(&'a self, pat: P) -> &'a str
2196     where
2197         P: Pattern<'a, Searcher: ReverseSearcher<'a>>,
2198     {
2199         self.trim_end_matches(pat)
2200     }
2201 
2202     /// Parses this string slice into another type.
2203     ///
2204     /// Because `parse` is so general, it can cause problems with type
2205     /// inference. As such, `parse` is one of the few times you'll see
2206     /// the syntax affectionately known as the 'turbofish': `::<>`. This
2207     /// helps the inference algorithm understand specifically which type
2208     /// you're trying to parse into.
2209     ///
2210     /// `parse` can parse into any type that implements the [`FromStr`] trait.
2211 
2212     ///
2213     /// # Errors
2214     ///
2215     /// Will return [`Err`] if it's not possible to parse this string slice into
2216     /// the desired type.
2217     ///
2218     /// [`Err`]: FromStr::Err
2219     ///
2220     /// # Examples
2221     ///
2222     /// Basic usage
2223     ///
2224     /// ```
2225     /// let four: u32 = "4".parse().unwrap();
2226     ///
2227     /// assert_eq!(4, four);
2228     /// ```
2229     ///
2230     /// Using the 'turbofish' instead of annotating `four`:
2231     ///
2232     /// ```
2233     /// let four = "4".parse::<u32>();
2234     ///
2235     /// assert_eq!(Ok(4), four);
2236     /// ```
2237     ///
2238     /// Failing to parse:
2239     ///
2240     /// ```
2241     /// let nope = "j".parse::<u32>();
2242     ///
2243     /// assert!(nope.is_err());
2244     /// ```
2245     #[inline]
2246     #[stable(feature = "rust1", since = "1.0.0")]
parse<F: FromStr>(&self) -> Result<F, F::Err>2247     pub fn parse<F: FromStr>(&self) -> Result<F, F::Err> {
2248         FromStr::from_str(self)
2249     }
2250 
2251     /// Checks if all characters in this string are within the ASCII range.
2252     ///
2253     /// # Examples
2254     ///
2255     /// ```
2256     /// let ascii = "hello!\n";
2257     /// let non_ascii = "Grüße, Jürgen ❤";
2258     ///
2259     /// assert!(ascii.is_ascii());
2260     /// assert!(!non_ascii.is_ascii());
2261     /// ```
2262     #[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
2263     #[must_use]
2264     #[inline]
is_ascii(&self) -> bool2265     pub fn is_ascii(&self) -> bool {
2266         // We can treat each byte as character here: all multibyte characters
2267         // start with a byte that is not in the ascii range, so we will stop
2268         // there already.
2269         self.as_bytes().is_ascii()
2270     }
2271 
2272     /// Checks that two strings are an ASCII case-insensitive match.
2273     ///
2274     /// Same as `to_ascii_lowercase(a) == to_ascii_lowercase(b)`,
2275     /// but without allocating and copying temporaries.
2276     ///
2277     /// # Examples
2278     ///
2279     /// ```
2280     /// assert!("Ferris".eq_ignore_ascii_case("FERRIS"));
2281     /// assert!("Ferrös".eq_ignore_ascii_case("FERRöS"));
2282     /// assert!(!"Ferrös".eq_ignore_ascii_case("FERRÖS"));
2283     /// ```
2284     #[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
2285     #[must_use]
2286     #[inline]
eq_ignore_ascii_case(&self, other: &str) -> bool2287     pub fn eq_ignore_ascii_case(&self, other: &str) -> bool {
2288         self.as_bytes().eq_ignore_ascii_case(other.as_bytes())
2289     }
2290 
2291     /// Converts this string to its ASCII upper case equivalent in-place.
2292     ///
2293     /// ASCII letters 'a' to 'z' are mapped to 'A' to 'Z',
2294     /// but non-ASCII letters are unchanged.
2295     ///
2296     /// To return a new uppercased value without modifying the existing one, use
2297     /// [`to_ascii_uppercase()`].
2298     ///
2299     /// [`to_ascii_uppercase()`]: #method.to_ascii_uppercase
2300     ///
2301     /// # Examples
2302     ///
2303     /// ```
2304     /// let mut s = String::from("Grüße, Jürgen ❤");
2305     ///
2306     /// s.make_ascii_uppercase();
2307     ///
2308     /// assert_eq!("GRüßE, JüRGEN ❤", s);
2309     /// ```
2310     #[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
2311     #[inline]
make_ascii_uppercase(&mut self)2312     pub fn make_ascii_uppercase(&mut self) {
2313         // SAFETY: safe because we transmute two types with the same layout.
2314         let me = unsafe { self.as_bytes_mut() };
2315         me.make_ascii_uppercase()
2316     }
2317 
2318     /// Converts this string to its ASCII lower case equivalent in-place.
2319     ///
2320     /// ASCII letters 'A' to 'Z' are mapped to 'a' to 'z',
2321     /// but non-ASCII letters are unchanged.
2322     ///
2323     /// To return a new lowercased value without modifying the existing one, use
2324     /// [`to_ascii_lowercase()`].
2325     ///
2326     /// [`to_ascii_lowercase()`]: #method.to_ascii_lowercase
2327     ///
2328     /// # Examples
2329     ///
2330     /// ```
2331     /// let mut s = String::from("GRÜßE, JÜRGEN ❤");
2332     ///
2333     /// s.make_ascii_lowercase();
2334     ///
2335     /// assert_eq!("grÜße, jÜrgen ❤", s);
2336     /// ```
2337     #[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
2338     #[inline]
make_ascii_lowercase(&mut self)2339     pub fn make_ascii_lowercase(&mut self) {
2340         // SAFETY: safe because we transmute two types with the same layout.
2341         let me = unsafe { self.as_bytes_mut() };
2342         me.make_ascii_lowercase()
2343     }
2344 
2345     /// Return an iterator that escapes each char in `self` with [`char::escape_debug`].
2346     ///
2347     /// Note: only extended grapheme codepoints that begin the string will be
2348     /// escaped.
2349     ///
2350     /// # Examples
2351     ///
2352     /// As an iterator:
2353     ///
2354     /// ```
2355     /// for c in "❤\n!".escape_debug() {
2356     ///     print!("{}", c);
2357     /// }
2358     /// println!();
2359     /// ```
2360     ///
2361     /// Using `println!` directly:
2362     ///
2363     /// ```
2364     /// println!("{}", "❤\n!".escape_debug());
2365     /// ```
2366     ///
2367     ///
2368     /// Both are equivalent to:
2369     ///
2370     /// ```
2371     /// println!("❤\\n!");
2372     /// ```
2373     ///
2374     /// Using `to_string`:
2375     ///
2376     /// ```
2377     /// assert_eq!("❤\n!".escape_debug().to_string(), "❤\\n!");
2378     /// ```
2379     #[must_use = "this returns the escaped string as an iterator, \
2380                   without modifying the original"]
2381     #[stable(feature = "str_escape", since = "1.34.0")]
escape_debug(&self) -> EscapeDebug<'_>2382     pub fn escape_debug(&self) -> EscapeDebug<'_> {
2383         let mut chars = self.chars();
2384         EscapeDebug {
2385             inner: chars
2386                 .next()
2387                 .map(|first| first.escape_debug_ext(EscapeDebugExtArgs::ESCAPE_ALL))
2388                 .into_iter()
2389                 .flatten()
2390                 .chain(chars.flat_map(CharEscapeDebugContinue)),
2391         }
2392     }
2393 
2394     /// Return an iterator that escapes each char in `self` with [`char::escape_default`].
2395     ///
2396     /// # Examples
2397     ///
2398     /// As an iterator:
2399     ///
2400     /// ```
2401     /// for c in "❤\n!".escape_default() {
2402     ///     print!("{}", c);
2403     /// }
2404     /// println!();
2405     /// ```
2406     ///
2407     /// Using `println!` directly:
2408     ///
2409     /// ```
2410     /// println!("{}", "❤\n!".escape_default());
2411     /// ```
2412     ///
2413     ///
2414     /// Both are equivalent to:
2415     ///
2416     /// ```
2417     /// println!("\\u{{2764}}\\n!");
2418     /// ```
2419     ///
2420     /// Using `to_string`:
2421     ///
2422     /// ```
2423     /// assert_eq!("❤\n!".escape_default().to_string(), "\\u{2764}\\n!");
2424     /// ```
2425     #[must_use = "this returns the escaped string as an iterator, \
2426                   without modifying the original"]
2427     #[stable(feature = "str_escape", since = "1.34.0")]
escape_default(&self) -> EscapeDefault<'_>2428     pub fn escape_default(&self) -> EscapeDefault<'_> {
2429         EscapeDefault { inner: self.chars().flat_map(CharEscapeDefault) }
2430     }
2431 
2432     /// Return an iterator that escapes each char in `self` with [`char::escape_unicode`].
2433     ///
2434     /// # Examples
2435     ///
2436     /// As an iterator:
2437     ///
2438     /// ```
2439     /// for c in "❤\n!".escape_unicode() {
2440     ///     print!("{}", c);
2441     /// }
2442     /// println!();
2443     /// ```
2444     ///
2445     /// Using `println!` directly:
2446     ///
2447     /// ```
2448     /// println!("{}", "❤\n!".escape_unicode());
2449     /// ```
2450     ///
2451     ///
2452     /// Both are equivalent to:
2453     ///
2454     /// ```
2455     /// println!("\\u{{2764}}\\u{{a}}\\u{{21}}");
2456     /// ```
2457     ///
2458     /// Using `to_string`:
2459     ///
2460     /// ```
2461     /// assert_eq!("❤\n!".escape_unicode().to_string(), "\\u{2764}\\u{a}\\u{21}");
2462     /// ```
2463     #[must_use = "this returns the escaped string as an iterator, \
2464                   without modifying the original"]
2465     #[stable(feature = "str_escape", since = "1.34.0")]
escape_unicode(&self) -> EscapeUnicode<'_>2466     pub fn escape_unicode(&self) -> EscapeUnicode<'_> {
2467         EscapeUnicode { inner: self.chars().flat_map(CharEscapeUnicode) }
2468     }
2469 }
2470 
2471 #[stable(feature = "rust1", since = "1.0.0")]
2472 impl AsRef<[u8]> for str {
2473     #[inline]
as_ref(&self) -> &[u8]2474     fn as_ref(&self) -> &[u8] {
2475         self.as_bytes()
2476     }
2477 }
2478 
2479 #[stable(feature = "rust1", since = "1.0.0")]
2480 #[rustc_const_unstable(feature = "const_default_impls", issue = "87864")]
2481 impl const Default for &str {
2482     /// Creates an empty str
2483     #[inline]
default() -> Self2484     fn default() -> Self {
2485         ""
2486     }
2487 }
2488 
2489 #[stable(feature = "default_mut_str", since = "1.28.0")]
2490 impl Default for &mut str {
2491     /// Creates an empty mutable str
2492     #[inline]
default() -> Self2493     fn default() -> Self {
2494         // SAFETY: The empty string is valid UTF-8.
2495         unsafe { from_utf8_unchecked_mut(&mut []) }
2496     }
2497 }
2498 
2499 impl_fn_for_zst! {
2500     /// A nameable, cloneable fn type
2501     #[derive(Clone)]
2502     struct LinesAnyMap impl<'a> Fn = |line: &'a str| -> &'a str {
2503         let l = line.len();
2504         if l > 0 && line.as_bytes()[l - 1] == b'\r' { &line[0 .. l - 1] }
2505         else { line }
2506     };
2507 
2508     #[derive(Clone)]
2509     struct CharEscapeDebugContinue impl Fn = |c: char| -> char::EscapeDebug {
2510         c.escape_debug_ext(EscapeDebugExtArgs {
2511             escape_grapheme_extended: false,
2512             escape_single_quote: true,
2513             escape_double_quote: true
2514         })
2515     };
2516 
2517     #[derive(Clone)]
2518     struct CharEscapeUnicode impl Fn = |c: char| -> char::EscapeUnicode {
2519         c.escape_unicode()
2520     };
2521     #[derive(Clone)]
2522     struct CharEscapeDefault impl Fn = |c: char| -> char::EscapeDefault {
2523         c.escape_default()
2524     };
2525 
2526     #[derive(Clone)]
2527     struct IsWhitespace impl Fn = |c: char| -> bool {
2528         c.is_whitespace()
2529     };
2530 
2531     #[derive(Clone)]
2532     struct IsAsciiWhitespace impl Fn = |byte: &u8| -> bool {
2533         byte.is_ascii_whitespace()
2534     };
2535 
2536     #[derive(Clone)]
2537     struct IsNotEmpty impl<'a, 'b> Fn = |s: &'a &'b str| -> bool {
2538         !s.is_empty()
2539     };
2540 
2541     #[derive(Clone)]
2542     struct BytesIsNotEmpty impl<'a, 'b> Fn = |s: &'a &'b [u8]| -> bool {
2543         !s.is_empty()
2544     };
2545 
2546     #[derive(Clone)]
2547     struct UnsafeBytesToStr impl<'a> Fn = |bytes: &'a [u8]| -> &'a str {
2548         // SAFETY: not safe
2549         unsafe { from_utf8_unchecked(bytes) }
2550     };
2551 }
2552