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