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