alloc/
string.rs

1//! A UTF-8โ€“encoded, growable string.
2//!
3//! This module contains the [`String`] type, the [`ToString`] trait for
4//! converting to strings, and several error types that may result from
5//! working with [`String`]s.
6//!
7//! # Examples
8//!
9//! There are multiple ways to create a new [`String`] from a string literal:
10//!
11//! ```
12//! let s = "Hello".to_string();
13//!
14//! let s = String::from("world");
15//! let s: String = "also this".into();
16//! ```
17//!
18//! You can create a new [`String`] from an existing one by concatenating with
19//! `+`:
20//!
21//! ```
22//! let s = "Hello".to_string();
23//!
24//! let message = s + " world!";
25//! ```
26//!
27//! If you have a vector of valid UTF-8 bytes, you can make a [`String`] out of
28//! it. You can do the reverse too.
29//!
30//! ```
31//! let sparkle_heart = vec![240, 159, 146, 150];
32//!
33//! // We know these bytes are valid, so we'll use `unwrap()`.
34//! let sparkle_heart = String::from_utf8(sparkle_heart).unwrap();
35//!
36//! assert_eq!("๐Ÿ’–", sparkle_heart);
37//!
38//! let bytes = sparkle_heart.into_bytes();
39//!
40//! assert_eq!(bytes, [240, 159, 146, 150]);
41//! ```
42
43#![stable(feature = "rust1", since = "1.0.0")]
44
45use core::error::Error;
46use core::iter::FusedIterator;
47#[cfg(not(no_global_oom_handling))]
48use core::iter::from_fn;
49#[cfg(not(no_global_oom_handling))]
50use core::ops::Add;
51#[cfg(not(no_global_oom_handling))]
52use core::ops::AddAssign;
53#[cfg(not(no_global_oom_handling))]
54use core::ops::Bound::{Excluded, Included, Unbounded};
55use core::ops::{self, Range, RangeBounds};
56use core::str::pattern::{Pattern, Utf8Pattern};
57use core::{fmt, hash, ptr, slice};
58
59#[cfg(not(no_global_oom_handling))]
60use crate::alloc::Allocator;
61#[cfg(not(no_global_oom_handling))]
62use crate::borrow::{Cow, ToOwned};
63use crate::boxed::Box;
64use crate::collections::TryReserveError;
65use crate::str::{self, CharIndices, Chars, Utf8Error, from_utf8_unchecked_mut};
66#[cfg(not(no_global_oom_handling))]
67use crate::str::{FromStr, from_boxed_utf8_unchecked};
68use crate::vec::{self, Vec};
69
70/// A UTF-8โ€“encoded, growable string.
71///
72/// `String` is the most common string type. It has ownership over the contents
73/// of the string, stored in a heap-allocated buffer (see [Representation](#representation)).
74/// It is closely related to its borrowed counterpart, the primitive [`str`].
75///
76/// # Examples
77///
78/// You can create a `String` from [a literal string][`&str`] with [`String::from`]:
79///
80/// [`String::from`]: From::from
81///
82/// ```
83/// let hello = String::from("Hello, world!");
84/// ```
85///
86/// You can append a [`char`] to a `String` with the [`push`] method, and
87/// append a [`&str`] with the [`push_str`] method:
88///
89/// ```
90/// let mut hello = String::from("Hello, ");
91///
92/// hello.push('w');
93/// hello.push_str("orld!");
94/// ```
95///
96/// [`push`]: String::push
97/// [`push_str`]: String::push_str
98///
99/// If you have a vector of UTF-8 bytes, you can create a `String` from it with
100/// the [`from_utf8`] method:
101///
102/// ```
103/// // some bytes, in a vector
104/// let sparkle_heart = vec![240, 159, 146, 150];
105///
106/// // We know these bytes are valid, so we'll use `unwrap()`.
107/// let sparkle_heart = String::from_utf8(sparkle_heart).unwrap();
108///
109/// assert_eq!("๐Ÿ’–", sparkle_heart);
110/// ```
111///
112/// [`from_utf8`]: String::from_utf8
113///
114/// # UTF-8
115///
116/// `String`s are always valid UTF-8. If you need a non-UTF-8 string, consider
117/// [`OsString`]. It is similar, but without the UTF-8 constraint. Because UTF-8
118/// is a variable width encoding, `String`s are typically smaller than an array of
119/// the same `char`s:
120///
121/// ```
122/// use std::mem;
123///
124/// // `s` is ASCII which represents each `char` as one byte
125/// let s = "hello";
126/// assert_eq!(s.len(), 5);
127///
128/// // A `char` array with the same contents would be longer because
129/// // every `char` is four bytes
130/// let s = ['h', 'e', 'l', 'l', 'o'];
131/// let size: usize = s.into_iter().map(|c| mem::size_of_val(&c)).sum();
132/// assert_eq!(size, 20);
133///
134/// // However, for non-ASCII strings, the difference will be smaller
135/// // and sometimes they are the same
136/// let s = "๐Ÿ’–๐Ÿ’–๐Ÿ’–๐Ÿ’–๐Ÿ’–";
137/// assert_eq!(s.len(), 20);
138///
139/// let s = ['๐Ÿ’–', '๐Ÿ’–', '๐Ÿ’–', '๐Ÿ’–', '๐Ÿ’–'];
140/// let size: usize = s.into_iter().map(|c| mem::size_of_val(&c)).sum();
141/// assert_eq!(size, 20);
142/// ```
143///
144/// This raises interesting questions as to how `s[i]` should work.
145/// What should `i` be here? Several options include byte indices and
146/// `char` indices but, because of UTF-8 encoding, only byte indices
147/// would provide constant time indexing. Getting the `i`th `char`, for
148/// example, is available using [`chars`]:
149///
150/// ```
151/// let s = "hello";
152/// let third_character = s.chars().nth(2);
153/// assert_eq!(third_character, Some('l'));
154///
155/// let s = "๐Ÿ’–๐Ÿ’–๐Ÿ’–๐Ÿ’–๐Ÿ’–";
156/// let third_character = s.chars().nth(2);
157/// assert_eq!(third_character, Some('๐Ÿ’–'));
158/// ```
159///
160/// Next, what should `s[i]` return? Because indexing returns a reference
161/// to underlying data it could be `&u8`, `&[u8]`, or something else similar.
162/// Since we're only providing one index, `&u8` makes the most sense but that
163/// might not be what the user expects and can be explicitly achieved with
164/// [`as_bytes()`]:
165///
166/// ```
167/// // The first byte is 104 - the byte value of `'h'`
168/// let s = "hello";
169/// assert_eq!(s.as_bytes()[0], 104);
170/// // or
171/// assert_eq!(s.as_bytes()[0], b'h');
172///
173/// // The first byte is 240 which isn't obviously useful
174/// let s = "๐Ÿ’–๐Ÿ’–๐Ÿ’–๐Ÿ’–๐Ÿ’–";
175/// assert_eq!(s.as_bytes()[0], 240);
176/// ```
177///
178/// Due to these ambiguities/restrictions, indexing with a `usize` is simply
179/// forbidden:
180///
181/// ```compile_fail,E0277
182/// let s = "hello";
183///
184/// // The following will not compile!
185/// println!("The first letter of s is {}", s[0]);
186/// ```
187///
188/// It is more clear, however, how `&s[i..j]` should work (that is,
189/// indexing with a range). It should accept byte indices (to be constant-time)
190/// and return a `&str` which is UTF-8 encoded. This is also called "string slicing".
191/// Note this will panic if the byte indices provided are not character
192/// boundaries - see [`is_char_boundary`] for more details. See the implementations
193/// for [`SliceIndex<str>`] for more details on string slicing. For a non-panicking
194/// version of string slicing, see [`get`].
195///
196/// [`OsString`]: ../../std/ffi/struct.OsString.html "ffi::OsString"
197/// [`SliceIndex<str>`]: core::slice::SliceIndex
198/// [`as_bytes()`]: str::as_bytes
199/// [`get`]: str::get
200/// [`is_char_boundary`]: str::is_char_boundary
201///
202/// The [`bytes`] and [`chars`] methods return iterators over the bytes and
203/// codepoints of the string, respectively. To iterate over codepoints along
204/// with byte indices, use [`char_indices`].
205///
206/// [`bytes`]: str::bytes
207/// [`chars`]: str::chars
208/// [`char_indices`]: str::char_indices
209///
210/// # Deref
211///
212/// `String` implements <code>[Deref]<Target = [str]></code>, and so inherits all of [`str`]'s
213/// methods. In addition, this means that you can pass a `String` to a
214/// function which takes a [`&str`] by using an ampersand (`&`):
215///
216/// ```
217/// fn takes_str(s: &str) { }
218///
219/// let s = String::from("Hello");
220///
221/// takes_str(&s);
222/// ```
223///
224/// This will create a [`&str`] from the `String` and pass it in. This
225/// conversion is very inexpensive, and so generally, functions will accept
226/// [`&str`]s as arguments unless they need a `String` for some specific
227/// reason.
228///
229/// In certain cases Rust doesn't have enough information to make this
230/// conversion, known as [`Deref`] coercion. In the following example a string
231/// slice [`&'a str`][`&str`] implements the trait `TraitExample`, and the function
232/// `example_func` takes anything that implements the trait. In this case Rust
233/// would need to make two implicit conversions, which Rust doesn't have the
234/// means to do. For that reason, the following example will not compile.
235///
236/// ```compile_fail,E0277
237/// trait TraitExample {}
238///
239/// impl<'a> TraitExample for &'a str {}
240///
241/// fn example_func<A: TraitExample>(example_arg: A) {}
242///
243/// let example_string = String::from("example_string");
244/// example_func(&example_string);
245/// ```
246///
247/// There are two options that would work instead. The first would be to
248/// change the line `example_func(&example_string);` to
249/// `example_func(example_string.as_str());`, using the method [`as_str()`]
250/// to explicitly extract the string slice containing the string. The second
251/// way changes `example_func(&example_string);` to
252/// `example_func(&*example_string);`. In this case we are dereferencing a
253/// `String` to a [`str`], then referencing the [`str`] back to
254/// [`&str`]. The second way is more idiomatic, however both work to do the
255/// conversion explicitly rather than relying on the implicit conversion.
256///
257/// # Representation
258///
259/// A `String` is made up of three components: a pointer to some bytes, a
260/// length, and a capacity. The pointer points to the internal buffer which `String`
261/// uses to store its data. The length is the number of bytes currently stored
262/// in the buffer, and the capacity is the size of the buffer in bytes. As such,
263/// the length will always be less than or equal to the capacity.
264///
265/// This buffer is always stored on the heap.
266///
267/// You can look at these with the [`as_ptr`], [`len`], and [`capacity`]
268/// methods:
269///
270/// ```
271/// use std::mem;
272///
273/// let story = String::from("Once upon a time...");
274///
275// FIXME Update this when vec_into_raw_parts is stabilized
276/// // Prevent automatically dropping the String's data
277/// let mut story = mem::ManuallyDrop::new(story);
278///
279/// let ptr = story.as_mut_ptr();
280/// let len = story.len();
281/// let capacity = story.capacity();
282///
283/// // story has nineteen bytes
284/// assert_eq!(19, len);
285///
286/// // We can re-build a String out of ptr, len, and capacity. This is all
287/// // unsafe because we are responsible for making sure the components are
288/// // valid:
289/// let s = unsafe { String::from_raw_parts(ptr, len, capacity) } ;
290///
291/// assert_eq!(String::from("Once upon a time..."), s);
292/// ```
293///
294/// [`as_ptr`]: str::as_ptr
295/// [`len`]: String::len
296/// [`capacity`]: String::capacity
297///
298/// If a `String` has enough capacity, adding elements to it will not
299/// re-allocate. For example, consider this program:
300///
301/// ```
302/// let mut s = String::new();
303///
304/// println!("{}", s.capacity());
305///
306/// for _ in 0..5 {
307///     s.push_str("hello");
308///     println!("{}", s.capacity());
309/// }
310/// ```
311///
312/// This will output the following:
313///
314/// ```text
315/// 0
316/// 8
317/// 16
318/// 16
319/// 32
320/// 32
321/// ```
322///
323/// At first, we have no memory allocated at all, but as we append to the
324/// string, it increases its capacity appropriately. If we instead use the
325/// [`with_capacity`] method to allocate the correct capacity initially:
326///
327/// ```
328/// let mut s = String::with_capacity(25);
329///
330/// println!("{}", s.capacity());
331///
332/// for _ in 0..5 {
333///     s.push_str("hello");
334///     println!("{}", s.capacity());
335/// }
336/// ```
337///
338/// [`with_capacity`]: String::with_capacity
339///
340/// We end up with a different output:
341///
342/// ```text
343/// 25
344/// 25
345/// 25
346/// 25
347/// 25
348/// 25
349/// ```
350///
351/// Here, there's no need to allocate more memory inside the loop.
352///
353/// [str]: prim@str "str"
354/// [`str`]: prim@str "str"
355/// [`&str`]: prim@str "&str"
356/// [Deref]: core::ops::Deref "ops::Deref"
357/// [`Deref`]: core::ops::Deref "ops::Deref"
358/// [`as_str()`]: String::as_str
359#[derive(PartialEq, PartialOrd, Eq, Ord)]
360#[stable(feature = "rust1", since = "1.0.0")]
361#[cfg_attr(not(test), lang = "String")]
362pub struct String {
363    vec: Vec<u8>,
364}
365
366/// A possible error value when converting a `String` from a UTF-8 byte vector.
367///
368/// This type is the error type for the [`from_utf8`] method on [`String`]. It
369/// is designed in such a way to carefully avoid reallocations: the
370/// [`into_bytes`] method will give back the byte vector that was used in the
371/// conversion attempt.
372///
373/// [`from_utf8`]: String::from_utf8
374/// [`into_bytes`]: FromUtf8Error::into_bytes
375///
376/// The [`Utf8Error`] type provided by [`std::str`] represents an error that may
377/// occur when converting a slice of [`u8`]s to a [`&str`]. In this sense, it's
378/// an analogue to `FromUtf8Error`, and you can get one from a `FromUtf8Error`
379/// through the [`utf8_error`] method.
380///
381/// [`Utf8Error`]: str::Utf8Error "std::str::Utf8Error"
382/// [`std::str`]: core::str "std::str"
383/// [`&str`]: prim@str "&str"
384/// [`utf8_error`]: FromUtf8Error::utf8_error
385///
386/// # Examples
387///
388/// ```
389/// // some invalid bytes, in a vector
390/// let bytes = vec![0, 159];
391///
392/// let value = String::from_utf8(bytes);
393///
394/// assert!(value.is_err());
395/// assert_eq!(vec![0, 159], value.unwrap_err().into_bytes());
396/// ```
397#[stable(feature = "rust1", since = "1.0.0")]
398#[cfg_attr(not(no_global_oom_handling), derive(Clone))]
399#[derive(Debug, PartialEq, Eq)]
400pub struct FromUtf8Error {
401    bytes: Vec<u8>,
402    error: Utf8Error,
403}
404
405/// A possible error value when converting a `String` from a UTF-16 byte slice.
406///
407/// This type is the error type for the [`from_utf16`] method on [`String`].
408///
409/// [`from_utf16`]: String::from_utf16
410///
411/// # Examples
412///
413/// ```
414/// // ๐„žmu<invalid>ic
415/// let v = &[0xD834, 0xDD1E, 0x006d, 0x0075,
416///           0xD800, 0x0069, 0x0063];
417///
418/// assert!(String::from_utf16(v).is_err());
419/// ```
420#[stable(feature = "rust1", since = "1.0.0")]
421#[derive(Debug)]
422pub struct FromUtf16Error(());
423
424impl String {
425    /// Creates a new empty `String`.
426    ///
427    /// Given that the `String` is empty, this will not allocate any initial
428    /// buffer. While that means that this initial operation is very
429    /// inexpensive, it may cause excessive allocation later when you add
430    /// data. If you have an idea of how much data the `String` will hold,
431    /// consider the [`with_capacity`] method to prevent excessive
432    /// re-allocation.
433    ///
434    /// [`with_capacity`]: String::with_capacity
435    ///
436    /// # Examples
437    ///
438    /// ```
439    /// let s = String::new();
440    /// ```
441    #[inline]
442    #[rustc_const_stable(feature = "const_string_new", since = "1.39.0")]
443    #[cfg_attr(not(test), rustc_diagnostic_item = "string_new")]
444    #[stable(feature = "rust1", since = "1.0.0")]
445    #[must_use]
446    pub const fn new() -> String {
447        String { vec: Vec::new() }
448    }
449
450    /// Creates a new empty `String` with at least the specified capacity.
451    ///
452    /// `String`s have an internal buffer to hold their data. The capacity is
453    /// the length of that buffer, and can be queried with the [`capacity`]
454    /// method. This method creates an empty `String`, but one with an initial
455    /// buffer that can hold at least `capacity` bytes. This is useful when you
456    /// may be appending a bunch of data to the `String`, reducing the number of
457    /// reallocations it needs to do.
458    ///
459    /// [`capacity`]: String::capacity
460    ///
461    /// If the given capacity is `0`, no allocation will occur, and this method
462    /// is identical to the [`new`] method.
463    ///
464    /// [`new`]: String::new
465    ///
466    /// # Examples
467    ///
468    /// ```
469    /// let mut s = String::with_capacity(10);
470    ///
471    /// // The String contains no chars, even though it has capacity for more
472    /// assert_eq!(s.len(), 0);
473    ///
474    /// // These are all done without reallocating...
475    /// let cap = s.capacity();
476    /// for _ in 0..10 {
477    ///     s.push('a');
478    /// }
479    ///
480    /// assert_eq!(s.capacity(), cap);
481    ///
482    /// // ...but this may make the string reallocate
483    /// s.push('a');
484    /// ```
485    #[cfg(not(no_global_oom_handling))]
486    #[inline]
487    #[stable(feature = "rust1", since = "1.0.0")]
488    #[must_use]
489    pub fn with_capacity(capacity: usize) -> String {
490        String { vec: Vec::with_capacity(capacity) }
491    }
492
493    /// Creates a new empty `String` with at least the specified capacity.
494    ///
495    /// # Errors
496    ///
497    /// Returns [`Err`] if the capacity exceeds `isize::MAX` bytes,
498    /// or if the memory allocator reports failure.
499    ///
500    #[inline]
501    #[unstable(feature = "try_with_capacity", issue = "91913")]
502    pub fn try_with_capacity(capacity: usize) -> Result<String, TryReserveError> {
503        Ok(String { vec: Vec::try_with_capacity(capacity)? })
504    }
505
506    // HACK(japaric): with cfg(test) the inherent `[T]::to_vec` method, which is
507    // required for this method definition, is not available. Since we don't
508    // require this method for testing purposes, I'll just stub it
509    // NB see the slice::hack module in slice.rs for more information
510    #[inline]
511    #[cfg(test)]
512    #[allow(missing_docs)]
513    pub fn from_str(_: &str) -> String {
514        panic!("not available with cfg(test)");
515    }
516
517    /// Converts a vector of bytes to a `String`.
518    ///
519    /// A string ([`String`]) is made of bytes ([`u8`]), and a vector of bytes
520    /// ([`Vec<u8>`]) is made of bytes, so this function converts between the
521    /// two. Not all byte slices are valid `String`s, however: `String`
522    /// requires that it is valid UTF-8. `from_utf8()` checks to ensure that
523    /// the bytes are valid UTF-8, and then does the conversion.
524    ///
525    /// If you are sure that the byte slice is valid UTF-8, and you don't want
526    /// to incur the overhead of the validity check, there is an unsafe version
527    /// of this function, [`from_utf8_unchecked`], which has the same behavior
528    /// but skips the check.
529    ///
530    /// This method will take care to not copy the vector, for efficiency's
531    /// sake.
532    ///
533    /// If you need a [`&str`] instead of a `String`, consider
534    /// [`str::from_utf8`].
535    ///
536    /// The inverse of this method is [`into_bytes`].
537    ///
538    /// # Errors
539    ///
540    /// Returns [`Err`] if the slice is not UTF-8 with a description as to why the
541    /// provided bytes are not UTF-8. The vector you moved in is also included.
542    ///
543    /// # Examples
544    ///
545    /// Basic usage:
546    ///
547    /// ```
548    /// // some bytes, in a vector
549    /// let sparkle_heart = vec![240, 159, 146, 150];
550    ///
551    /// // We know these bytes are valid, so we'll use `unwrap()`.
552    /// let sparkle_heart = String::from_utf8(sparkle_heart).unwrap();
553    ///
554    /// assert_eq!("๐Ÿ’–", sparkle_heart);
555    /// ```
556    ///
557    /// Incorrect bytes:
558    ///
559    /// ```
560    /// // some invalid bytes, in a vector
561    /// let sparkle_heart = vec![0, 159, 146, 150];
562    ///
563    /// assert!(String::from_utf8(sparkle_heart).is_err());
564    /// ```
565    ///
566    /// See the docs for [`FromUtf8Error`] for more details on what you can do
567    /// with this error.
568    ///
569    /// [`from_utf8_unchecked`]: String::from_utf8_unchecked
570    /// [`Vec<u8>`]: crate::vec::Vec "Vec"
571    /// [`&str`]: prim@str "&str"
572    /// [`into_bytes`]: String::into_bytes
573    #[inline]
574    #[stable(feature = "rust1", since = "1.0.0")]
575    #[cfg_attr(not(test), rustc_diagnostic_item = "string_from_utf8")]
576    pub fn from_utf8(vec: Vec<u8>) -> Result<String, FromUtf8Error> {
577        match str::from_utf8(&vec) {
578            Ok(..) => Ok(String { vec }),
579            Err(e) => Err(FromUtf8Error { bytes: vec, error: e }),
580        }
581    }
582
583    /// Converts a slice of bytes to a string, including invalid characters.
584    ///
585    /// Strings are made of bytes ([`u8`]), and a slice of bytes
586    /// ([`&[u8]`][byteslice]) is made of bytes, so this function converts
587    /// between the two. Not all byte slices are valid strings, however: strings
588    /// are required to be valid UTF-8. During this conversion,
589    /// `from_utf8_lossy()` will replace any invalid UTF-8 sequences with
590    /// [`U+FFFD REPLACEMENT CHARACTER`][U+FFFD], which looks like this: ๏ฟฝ
591    ///
592    /// [byteslice]: prim@slice
593    /// [U+FFFD]: core::char::REPLACEMENT_CHARACTER
594    ///
595    /// If you are sure that the byte slice is valid UTF-8, and you don't want
596    /// to incur the overhead of the conversion, there is an unsafe version
597    /// of this function, [`from_utf8_unchecked`], which has the same behavior
598    /// but skips the checks.
599    ///
600    /// [`from_utf8_unchecked`]: String::from_utf8_unchecked
601    ///
602    /// This function returns a [`Cow<'a, str>`]. If our byte slice is invalid
603    /// UTF-8, then we need to insert the replacement characters, which will
604    /// change the size of the string, and hence, require a `String`. But if
605    /// it's already valid UTF-8, we don't need a new allocation. This return
606    /// type allows us to handle both cases.
607    ///
608    /// [`Cow<'a, str>`]: crate::borrow::Cow "borrow::Cow"
609    ///
610    /// # Examples
611    ///
612    /// Basic usage:
613    ///
614    /// ```
615    /// // some bytes, in a vector
616    /// let sparkle_heart = vec![240, 159, 146, 150];
617    ///
618    /// let sparkle_heart = String::from_utf8_lossy(&sparkle_heart);
619    ///
620    /// assert_eq!("๐Ÿ’–", sparkle_heart);
621    /// ```
622    ///
623    /// Incorrect bytes:
624    ///
625    /// ```
626    /// // some invalid bytes
627    /// let input = b"Hello \xF0\x90\x80World";
628    /// let output = String::from_utf8_lossy(input);
629    ///
630    /// assert_eq!("Hello ๏ฟฝWorld", output);
631    /// ```
632    #[must_use]
633    #[cfg(not(no_global_oom_handling))]
634    #[stable(feature = "rust1", since = "1.0.0")]
635    pub fn from_utf8_lossy(v: &[u8]) -> Cow<'_, str> {
636        let mut iter = v.utf8_chunks();
637
638        let first_valid = if let Some(chunk) = iter.next() {
639            let valid = chunk.valid();
640            if chunk.invalid().is_empty() {
641                debug_assert_eq!(valid.len(), v.len());
642                return Cow::Borrowed(valid);
643            }
644            valid
645        } else {
646            return Cow::Borrowed("");
647        };
648
649        const REPLACEMENT: &str = "\u{FFFD}";
650
651        let mut res = String::with_capacity(v.len());
652        res.push_str(first_valid);
653        res.push_str(REPLACEMENT);
654
655        for chunk in iter {
656            res.push_str(chunk.valid());
657            if !chunk.invalid().is_empty() {
658                res.push_str(REPLACEMENT);
659            }
660        }
661
662        Cow::Owned(res)
663    }
664
665    /// Converts a [`Vec<u8>`] to a `String`, substituting invalid UTF-8
666    /// sequences with replacement characters.
667    ///
668    /// See [`from_utf8_lossy`] for more details.
669    ///
670    /// [`from_utf8_lossy`]: String::from_utf8_lossy
671    ///
672    /// Note that this function does not guarantee reuse of the original `Vec`
673    /// allocation.
674    ///
675    /// # Examples
676    ///
677    /// Basic usage:
678    ///
679    /// ```
680    /// #![feature(string_from_utf8_lossy_owned)]
681    /// // some bytes, in a vector
682    /// let sparkle_heart = vec![240, 159, 146, 150];
683    ///
684    /// let sparkle_heart = String::from_utf8_lossy_owned(sparkle_heart);
685    ///
686    /// assert_eq!(String::from("๐Ÿ’–"), sparkle_heart);
687    /// ```
688    ///
689    /// Incorrect bytes:
690    ///
691    /// ```
692    /// #![feature(string_from_utf8_lossy_owned)]
693    /// // some invalid bytes
694    /// let input: Vec<u8> = b"Hello \xF0\x90\x80World".into();
695    /// let output = String::from_utf8_lossy_owned(input);
696    ///
697    /// assert_eq!(String::from("Hello ๏ฟฝWorld"), output);
698    /// ```
699    #[must_use]
700    #[cfg(not(no_global_oom_handling))]
701    #[unstable(feature = "string_from_utf8_lossy_owned", issue = "129436")]
702    pub fn from_utf8_lossy_owned(v: Vec<u8>) -> String {
703        if let Cow::Owned(string) = String::from_utf8_lossy(&v) {
704            string
705        } else {
706            // SAFETY: `String::from_utf8_lossy`'s contract ensures that if
707            // it returns a `Cow::Borrowed`, it is a valid UTF-8 string.
708            // Otherwise, it returns a new allocation of an owned `String`, with
709            // replacement characters for invalid sequences, which is returned
710            // above.
711            unsafe { String::from_utf8_unchecked(v) }
712        }
713    }
714
715    /// Decode a native endian UTF-16โ€“encoded vector `v` into a `String`,
716    /// returning [`Err`] if `v` contains any invalid data.
717    ///
718    /// # Examples
719    ///
720    /// ```
721    /// // ๐„žmusic
722    /// let v = &[0xD834, 0xDD1E, 0x006d, 0x0075,
723    ///           0x0073, 0x0069, 0x0063];
724    /// assert_eq!(String::from("๐„žmusic"),
725    ///            String::from_utf16(v).unwrap());
726    ///
727    /// // ๐„žmu<invalid>ic
728    /// let v = &[0xD834, 0xDD1E, 0x006d, 0x0075,
729    ///           0xD800, 0x0069, 0x0063];
730    /// assert!(String::from_utf16(v).is_err());
731    /// ```
732    #[cfg(not(no_global_oom_handling))]
733    #[stable(feature = "rust1", since = "1.0.0")]
734    pub fn from_utf16(v: &[u16]) -> Result<String, FromUtf16Error> {
735        // This isn't done via collect::<Result<_, _>>() for performance reasons.
736        // FIXME: the function can be simplified again when #48994 is closed.
737        let mut ret = String::with_capacity(v.len());
738        for c in char::decode_utf16(v.iter().cloned()) {
739            if let Ok(c) = c {
740                ret.push(c);
741            } else {
742                return Err(FromUtf16Error(()));
743            }
744        }
745        Ok(ret)
746    }
747
748    /// Decode a native endian UTF-16โ€“encoded slice `v` into a `String`,
749    /// replacing invalid data with [the replacement character (`U+FFFD`)][U+FFFD].
750    ///
751    /// Unlike [`from_utf8_lossy`] which returns a [`Cow<'a, str>`],
752    /// `from_utf16_lossy` returns a `String` since the UTF-16 to UTF-8
753    /// conversion requires a memory allocation.
754    ///
755    /// [`from_utf8_lossy`]: String::from_utf8_lossy
756    /// [`Cow<'a, str>`]: crate::borrow::Cow "borrow::Cow"
757    /// [U+FFFD]: core::char::REPLACEMENT_CHARACTER
758    ///
759    /// # Examples
760    ///
761    /// ```
762    /// // ๐„žmus<invalid>ic<invalid>
763    /// let v = &[0xD834, 0xDD1E, 0x006d, 0x0075,
764    ///           0x0073, 0xDD1E, 0x0069, 0x0063,
765    ///           0xD834];
766    ///
767    /// assert_eq!(String::from("๐„žmus\u{FFFD}ic\u{FFFD}"),
768    ///            String::from_utf16_lossy(v));
769    /// ```
770    #[cfg(not(no_global_oom_handling))]
771    #[must_use]
772    #[inline]
773    #[stable(feature = "rust1", since = "1.0.0")]
774    pub fn from_utf16_lossy(v: &[u16]) -> String {
775        char::decode_utf16(v.iter().cloned())
776            .map(|r| r.unwrap_or(char::REPLACEMENT_CHARACTER))
777            .collect()
778    }
779
780    /// Decode a UTF-16LEโ€“encoded vector `v` into a `String`,
781    /// returning [`Err`] if `v` contains any invalid data.
782    ///
783    /// # Examples
784    ///
785    /// Basic usage:
786    ///
787    /// ```
788    /// #![feature(str_from_utf16_endian)]
789    /// // ๐„žmusic
790    /// let v = &[0x34, 0xD8, 0x1E, 0xDD, 0x6d, 0x00, 0x75, 0x00,
791    ///           0x73, 0x00, 0x69, 0x00, 0x63, 0x00];
792    /// assert_eq!(String::from("๐„žmusic"),
793    ///            String::from_utf16le(v).unwrap());
794    ///
795    /// // ๐„žmu<invalid>ic
796    /// let v = &[0x34, 0xD8, 0x1E, 0xDD, 0x6d, 0x00, 0x75, 0x00,
797    ///           0x00, 0xD8, 0x69, 0x00, 0x63, 0x00];
798    /// assert!(String::from_utf16le(v).is_err());
799    /// ```
800    #[cfg(not(no_global_oom_handling))]
801    #[unstable(feature = "str_from_utf16_endian", issue = "116258")]
802    pub fn from_utf16le(v: &[u8]) -> Result<String, FromUtf16Error> {
803        if v.len() % 2 != 0 {
804            return Err(FromUtf16Error(()));
805        }
806        match (cfg!(target_endian = "little"), unsafe { v.align_to::<u16>() }) {
807            (true, ([], v, [])) => Self::from_utf16(v),
808            _ => char::decode_utf16(v.array_chunks::<2>().copied().map(u16::from_le_bytes))
809                .collect::<Result<_, _>>()
810                .map_err(|_| FromUtf16Error(())),
811        }
812    }
813
814    /// Decode a UTF-16LEโ€“encoded slice `v` into a `String`, replacing
815    /// invalid data with [the replacement character (`U+FFFD`)][U+FFFD].
816    ///
817    /// Unlike [`from_utf8_lossy`] which returns a [`Cow<'a, str>`],
818    /// `from_utf16le_lossy` returns a `String` since the UTF-16 to UTF-8
819    /// conversion requires a memory allocation.
820    ///
821    /// [`from_utf8_lossy`]: String::from_utf8_lossy
822    /// [`Cow<'a, str>`]: crate::borrow::Cow "borrow::Cow"
823    /// [U+FFFD]: core::char::REPLACEMENT_CHARACTER
824    ///
825    /// # Examples
826    ///
827    /// Basic usage:
828    ///
829    /// ```
830    /// #![feature(str_from_utf16_endian)]
831    /// // ๐„žmus<invalid>ic<invalid>
832    /// let v = &[0x34, 0xD8, 0x1E, 0xDD, 0x6d, 0x00, 0x75, 0x00,
833    ///           0x73, 0x00, 0x1E, 0xDD, 0x69, 0x00, 0x63, 0x00,
834    ///           0x34, 0xD8];
835    ///
836    /// assert_eq!(String::from("๐„žmus\u{FFFD}ic\u{FFFD}"),
837    ///            String::from_utf16le_lossy(v));
838    /// ```
839    #[cfg(not(no_global_oom_handling))]
840    #[unstable(feature = "str_from_utf16_endian", issue = "116258")]
841    pub fn from_utf16le_lossy(v: &[u8]) -> String {
842        match (cfg!(target_endian = "little"), unsafe { v.align_to::<u16>() }) {
843            (true, ([], v, [])) => Self::from_utf16_lossy(v),
844            (true, ([], v, [_remainder])) => Self::from_utf16_lossy(v) + "\u{FFFD}",
845            _ => {
846                let mut iter = v.array_chunks::<2>();
847                let string = char::decode_utf16(iter.by_ref().copied().map(u16::from_le_bytes))
848                    .map(|r| r.unwrap_or(char::REPLACEMENT_CHARACTER))
849                    .collect();
850                if iter.remainder().is_empty() { string } else { string + "\u{FFFD}" }
851            }
852        }
853    }
854
855    /// Decode a UTF-16BEโ€“encoded vector `v` into a `String`,
856    /// returning [`Err`] if `v` contains any invalid data.
857    ///
858    /// # Examples
859    ///
860    /// Basic usage:
861    ///
862    /// ```
863    /// #![feature(str_from_utf16_endian)]
864    /// // ๐„žmusic
865    /// let v = &[0xD8, 0x34, 0xDD, 0x1E, 0x00, 0x6d, 0x00, 0x75,
866    ///           0x00, 0x73, 0x00, 0x69, 0x00, 0x63];
867    /// assert_eq!(String::from("๐„žmusic"),
868    ///            String::from_utf16be(v).unwrap());
869    ///
870    /// // ๐„žmu<invalid>ic
871    /// let v = &[0xD8, 0x34, 0xDD, 0x1E, 0x00, 0x6d, 0x00, 0x75,
872    ///           0xD8, 0x00, 0x00, 0x69, 0x00, 0x63];
873    /// assert!(String::from_utf16be(v).is_err());
874    /// ```
875    #[cfg(not(no_global_oom_handling))]
876    #[unstable(feature = "str_from_utf16_endian", issue = "116258")]
877    pub fn from_utf16be(v: &[u8]) -> Result<String, FromUtf16Error> {
878        if v.len() % 2 != 0 {
879            return Err(FromUtf16Error(()));
880        }
881        match (cfg!(target_endian = "big"), unsafe { v.align_to::<u16>() }) {
882            (true, ([], v, [])) => Self::from_utf16(v),
883            _ => char::decode_utf16(v.array_chunks::<2>().copied().map(u16::from_be_bytes))
884                .collect::<Result<_, _>>()
885                .map_err(|_| FromUtf16Error(())),
886        }
887    }
888
889    /// Decode a UTF-16BEโ€“encoded slice `v` into a `String`, replacing
890    /// invalid data with [the replacement character (`U+FFFD`)][U+FFFD].
891    ///
892    /// Unlike [`from_utf8_lossy`] which returns a [`Cow<'a, str>`],
893    /// `from_utf16le_lossy` returns a `String` since the UTF-16 to UTF-8
894    /// conversion requires a memory allocation.
895    ///
896    /// [`from_utf8_lossy`]: String::from_utf8_lossy
897    /// [`Cow<'a, str>`]: crate::borrow::Cow "borrow::Cow"
898    /// [U+FFFD]: core::char::REPLACEMENT_CHARACTER
899    ///
900    /// # Examples
901    ///
902    /// Basic usage:
903    ///
904    /// ```
905    /// #![feature(str_from_utf16_endian)]
906    /// // ๐„žmus<invalid>ic<invalid>
907    /// let v = &[0xD8, 0x34, 0xDD, 0x1E, 0x00, 0x6d, 0x00, 0x75,
908    ///           0x00, 0x73, 0xDD, 0x1E, 0x00, 0x69, 0x00, 0x63,
909    ///           0xD8, 0x34];
910    ///
911    /// assert_eq!(String::from("๐„žmus\u{FFFD}ic\u{FFFD}"),
912    ///            String::from_utf16be_lossy(v));
913    /// ```
914    #[cfg(not(no_global_oom_handling))]
915    #[unstable(feature = "str_from_utf16_endian", issue = "116258")]
916    pub fn from_utf16be_lossy(v: &[u8]) -> String {
917        match (cfg!(target_endian = "big"), unsafe { v.align_to::<u16>() }) {
918            (true, ([], v, [])) => Self::from_utf16_lossy(v),
919            (true, ([], v, [_remainder])) => Self::from_utf16_lossy(v) + "\u{FFFD}",
920            _ => {
921                let mut iter = v.array_chunks::<2>();
922                let string = char::decode_utf16(iter.by_ref().copied().map(u16::from_be_bytes))
923                    .map(|r| r.unwrap_or(char::REPLACEMENT_CHARACTER))
924                    .collect();
925                if iter.remainder().is_empty() { string } else { string + "\u{FFFD}" }
926            }
927        }
928    }
929
930    /// Decomposes a `String` into its raw components: `(pointer, length, capacity)`.
931    ///
932    /// Returns the raw pointer to the underlying data, the length of
933    /// the string (in bytes), and the allocated capacity of the data
934    /// (in bytes). These are the same arguments in the same order as
935    /// the arguments to [`from_raw_parts`].
936    ///
937    /// After calling this function, the caller is responsible for the
938    /// memory previously managed by the `String`. The only way to do
939    /// this is to convert the raw pointer, length, and capacity back
940    /// into a `String` with the [`from_raw_parts`] function, allowing
941    /// the destructor to perform the cleanup.
942    ///
943    /// [`from_raw_parts`]: String::from_raw_parts
944    ///
945    /// # Examples
946    ///
947    /// ```
948    /// #![feature(vec_into_raw_parts)]
949    /// let s = String::from("hello");
950    ///
951    /// let (ptr, len, cap) = s.into_raw_parts();
952    ///
953    /// let rebuilt = unsafe { String::from_raw_parts(ptr, len, cap) };
954    /// assert_eq!(rebuilt, "hello");
955    /// ```
956    #[must_use = "losing the pointer will leak memory"]
957    #[unstable(feature = "vec_into_raw_parts", reason = "new API", issue = "65816")]
958    pub fn into_raw_parts(self) -> (*mut u8, usize, usize) {
959        self.vec.into_raw_parts()
960    }
961
962    /// Creates a new `String` from a pointer, a length and a capacity.
963    ///
964    /// # Safety
965    ///
966    /// This is highly unsafe, due to the number of invariants that aren't
967    /// checked:
968    ///
969    /// * all safety requirements for [`Vec::<u8>::from_raw_parts`].
970    /// * all safety requirements for [`String::from_utf8_unchecked`].
971    ///
972    /// Violating these may cause problems like corrupting the allocator's
973    /// internal data structures. For example, it is normally **not** safe to
974    /// build a `String` from a pointer to a C `char` array containing UTF-8
975    /// _unless_ you are certain that array was originally allocated by the
976    /// Rust standard library's allocator.
977    ///
978    /// The ownership of `buf` is effectively transferred to the
979    /// `String` which may then deallocate, reallocate or change the
980    /// contents of memory pointed to by the pointer at will. Ensure
981    /// that nothing else uses the pointer after calling this
982    /// function.
983    ///
984    /// # Examples
985    ///
986    /// ```
987    /// use std::mem;
988    ///
989    /// unsafe {
990    ///     let s = String::from("hello");
991    ///
992    // FIXME Update this when vec_into_raw_parts is stabilized
993    ///     // Prevent automatically dropping the String's data
994    ///     let mut s = mem::ManuallyDrop::new(s);
995    ///
996    ///     let ptr = s.as_mut_ptr();
997    ///     let len = s.len();
998    ///     let capacity = s.capacity();
999    ///
1000    ///     let s = String::from_raw_parts(ptr, len, capacity);
1001    ///
1002    ///     assert_eq!(String::from("hello"), s);
1003    /// }
1004    /// ```
1005    #[inline]
1006    #[stable(feature = "rust1", since = "1.0.0")]
1007    pub unsafe fn from_raw_parts(buf: *mut u8, length: usize, capacity: usize) -> String {
1008        unsafe { String { vec: Vec::from_raw_parts(buf, length, capacity) } }
1009    }
1010
1011    /// Converts a vector of bytes to a `String` without checking that the
1012    /// string contains valid UTF-8.
1013    ///
1014    /// See the safe version, [`from_utf8`], for more details.
1015    ///
1016    /// [`from_utf8`]: String::from_utf8
1017    ///
1018    /// # Safety
1019    ///
1020    /// This function is unsafe because it does not check that the bytes passed
1021    /// to it are valid UTF-8. If this constraint is violated, it may cause
1022    /// memory unsafety issues with future users of the `String`, as the rest of
1023    /// the standard library assumes that `String`s are valid UTF-8.
1024    ///
1025    /// # Examples
1026    ///
1027    /// ```
1028    /// // some bytes, in a vector
1029    /// let sparkle_heart = vec![240, 159, 146, 150];
1030    ///
1031    /// let sparkle_heart = unsafe {
1032    ///     String::from_utf8_unchecked(sparkle_heart)
1033    /// };
1034    ///
1035    /// assert_eq!("๐Ÿ’–", sparkle_heart);
1036    /// ```
1037    #[inline]
1038    #[must_use]
1039    #[stable(feature = "rust1", since = "1.0.0")]
1040    pub unsafe fn from_utf8_unchecked(bytes: Vec<u8>) -> String {
1041        String { vec: bytes }
1042    }
1043
1044    /// Converts a `String` into a byte vector.
1045    ///
1046    /// This consumes the `String`, so we do not need to copy its contents.
1047    ///
1048    /// # Examples
1049    ///
1050    /// ```
1051    /// let s = String::from("hello");
1052    /// let bytes = s.into_bytes();
1053    ///
1054    /// assert_eq!(&[104, 101, 108, 108, 111][..], &bytes[..]);
1055    /// ```
1056    #[inline]
1057    #[must_use = "`self` will be dropped if the result is not used"]
1058    #[stable(feature = "rust1", since = "1.0.0")]
1059    #[rustc_const_unstable(feature = "const_vec_string_slice", issue = "129041")]
1060    pub const fn into_bytes(self) -> Vec<u8> {
1061        self.vec
1062    }
1063
1064    /// Extracts a string slice containing the entire `String`.
1065    ///
1066    /// # Examples
1067    ///
1068    /// ```
1069    /// let s = String::from("foo");
1070    ///
1071    /// assert_eq!("foo", s.as_str());
1072    /// ```
1073    #[inline]
1074    #[must_use]
1075    #[stable(feature = "string_as_str", since = "1.7.0")]
1076    #[cfg_attr(not(test), rustc_diagnostic_item = "string_as_str")]
1077    #[rustc_const_unstable(feature = "const_vec_string_slice", issue = "129041")]
1078    pub const fn as_str(&self) -> &str {
1079        // SAFETY: String contents are stipulated to be valid UTF-8, invalid contents are an error
1080        // at construction.
1081        unsafe { str::from_utf8_unchecked(self.vec.as_slice()) }
1082    }
1083
1084    /// Converts a `String` into a mutable string slice.
1085    ///
1086    /// # Examples
1087    ///
1088    /// ```
1089    /// let mut s = String::from("foobar");
1090    /// let s_mut_str = s.as_mut_str();
1091    ///
1092    /// s_mut_str.make_ascii_uppercase();
1093    ///
1094    /// assert_eq!("FOOBAR", s_mut_str);
1095    /// ```
1096    #[inline]
1097    #[must_use]
1098    #[stable(feature = "string_as_str", since = "1.7.0")]
1099    #[cfg_attr(not(test), rustc_diagnostic_item = "string_as_mut_str")]
1100    #[rustc_const_unstable(feature = "const_vec_string_slice", issue = "129041")]
1101    pub const fn as_mut_str(&mut self) -> &mut str {
1102        // SAFETY: String contents are stipulated to be valid UTF-8, invalid contents are an error
1103        // at construction.
1104        unsafe { str::from_utf8_unchecked_mut(self.vec.as_mut_slice()) }
1105    }
1106
1107    /// Appends a given string slice onto the end of this `String`.
1108    ///
1109    /// # Examples
1110    ///
1111    /// ```
1112    /// let mut s = String::from("foo");
1113    ///
1114    /// s.push_str("bar");
1115    ///
1116    /// assert_eq!("foobar", s);
1117    /// ```
1118    #[cfg(not(no_global_oom_handling))]
1119    #[inline]
1120    #[stable(feature = "rust1", since = "1.0.0")]
1121    #[rustc_confusables("append", "push")]
1122    #[cfg_attr(not(test), rustc_diagnostic_item = "string_push_str")]
1123    pub fn push_str(&mut self, string: &str) {
1124        self.vec.extend_from_slice(string.as_bytes())
1125    }
1126
1127    /// Copies elements from `src` range to the end of the string.
1128    ///
1129    /// # Panics
1130    ///
1131    /// Panics if the starting point or end point do not lie on a [`char`]
1132    /// boundary, or if they're out of bounds.
1133    ///
1134    /// # Examples
1135    ///
1136    /// ```
1137    /// #![feature(string_extend_from_within)]
1138    /// let mut string = String::from("abcde");
1139    ///
1140    /// string.extend_from_within(2..);
1141    /// assert_eq!(string, "abcdecde");
1142    ///
1143    /// string.extend_from_within(..2);
1144    /// assert_eq!(string, "abcdecdeab");
1145    ///
1146    /// string.extend_from_within(4..8);
1147    /// assert_eq!(string, "abcdecdeabecde");
1148    /// ```
1149    #[cfg(not(no_global_oom_handling))]
1150    #[unstable(feature = "string_extend_from_within", issue = "103806")]
1151    pub fn extend_from_within<R>(&mut self, src: R)
1152    where
1153        R: RangeBounds<usize>,
1154    {
1155        let src @ Range { start, end } = slice::range(src, ..self.len());
1156
1157        assert!(self.is_char_boundary(start));
1158        assert!(self.is_char_boundary(end));
1159
1160        self.vec.extend_from_within(src);
1161    }
1162
1163    /// Returns this `String`'s capacity, in bytes.
1164    ///
1165    /// # Examples
1166    ///
1167    /// ```
1168    /// let s = String::with_capacity(10);
1169    ///
1170    /// assert!(s.capacity() >= 10);
1171    /// ```
1172    #[inline]
1173    #[must_use]
1174    #[stable(feature = "rust1", since = "1.0.0")]
1175    #[rustc_const_unstable(feature = "const_vec_string_slice", issue = "129041")]
1176    pub const fn capacity(&self) -> usize {
1177        self.vec.capacity()
1178    }
1179
1180    /// Reserves capacity for at least `additional` bytes more than the
1181    /// current length. The allocator may reserve more space to speculatively
1182    /// avoid frequent allocations. After calling `reserve`,
1183    /// capacity will be greater than or equal to `self.len() + additional`.
1184    /// Does nothing if capacity is already sufficient.
1185    ///
1186    /// # Panics
1187    ///
1188    /// Panics if the new capacity overflows [`usize`].
1189    ///
1190    /// # Examples
1191    ///
1192    /// Basic usage:
1193    ///
1194    /// ```
1195    /// let mut s = String::new();
1196    ///
1197    /// s.reserve(10);
1198    ///
1199    /// assert!(s.capacity() >= 10);
1200    /// ```
1201    ///
1202    /// This might not actually increase the capacity:
1203    ///
1204    /// ```
1205    /// let mut s = String::with_capacity(10);
1206    /// s.push('a');
1207    /// s.push('b');
1208    ///
1209    /// // s now has a length of 2 and a capacity of at least 10
1210    /// let capacity = s.capacity();
1211    /// assert_eq!(2, s.len());
1212    /// assert!(capacity >= 10);
1213    ///
1214    /// // Since we already have at least an extra 8 capacity, calling this...
1215    /// s.reserve(8);
1216    ///
1217    /// // ... doesn't actually increase.
1218    /// assert_eq!(capacity, s.capacity());
1219    /// ```
1220    #[cfg(not(no_global_oom_handling))]
1221    #[inline]
1222    #[stable(feature = "rust1", since = "1.0.0")]
1223    pub fn reserve(&mut self, additional: usize) {
1224        self.vec.reserve(additional)
1225    }
1226
1227    /// Reserves the minimum capacity for at least `additional` bytes more than
1228    /// the current length. Unlike [`reserve`], this will not
1229    /// deliberately over-allocate to speculatively avoid frequent allocations.
1230    /// After calling `reserve_exact`, capacity will be greater than or equal to
1231    /// `self.len() + additional`. Does nothing if the capacity is already
1232    /// sufficient.
1233    ///
1234    /// [`reserve`]: String::reserve
1235    ///
1236    /// # Panics
1237    ///
1238    /// Panics if the new capacity overflows [`usize`].
1239    ///
1240    /// # Examples
1241    ///
1242    /// Basic usage:
1243    ///
1244    /// ```
1245    /// let mut s = String::new();
1246    ///
1247    /// s.reserve_exact(10);
1248    ///
1249    /// assert!(s.capacity() >= 10);
1250    /// ```
1251    ///
1252    /// This might not actually increase the capacity:
1253    ///
1254    /// ```
1255    /// let mut s = String::with_capacity(10);
1256    /// s.push('a');
1257    /// s.push('b');
1258    ///
1259    /// // s now has a length of 2 and a capacity of at least 10
1260    /// let capacity = s.capacity();
1261    /// assert_eq!(2, s.len());
1262    /// assert!(capacity >= 10);
1263    ///
1264    /// // Since we already have at least an extra 8 capacity, calling this...
1265    /// s.reserve_exact(8);
1266    ///
1267    /// // ... doesn't actually increase.
1268    /// assert_eq!(capacity, s.capacity());
1269    /// ```
1270    #[cfg(not(no_global_oom_handling))]
1271    #[inline]
1272    #[stable(feature = "rust1", since = "1.0.0")]
1273    pub fn reserve_exact(&mut self, additional: usize) {
1274        self.vec.reserve_exact(additional)
1275    }
1276
1277    /// Tries to reserve capacity for at least `additional` bytes more than the
1278    /// current length. The allocator may reserve more space to speculatively
1279    /// avoid frequent allocations. After calling `try_reserve`, capacity will be
1280    /// greater than or equal to `self.len() + additional` if it returns
1281    /// `Ok(())`. Does nothing if capacity is already sufficient. This method
1282    /// preserves the contents even if an error occurs.
1283    ///
1284    /// # Errors
1285    ///
1286    /// If the capacity overflows, or the allocator reports a failure, then an error
1287    /// is returned.
1288    ///
1289    /// # Examples
1290    ///
1291    /// ```
1292    /// use std::collections::TryReserveError;
1293    ///
1294    /// fn process_data(data: &str) -> Result<String, TryReserveError> {
1295    ///     let mut output = String::new();
1296    ///
1297    ///     // Pre-reserve the memory, exiting if we can't
1298    ///     output.try_reserve(data.len())?;
1299    ///
1300    ///     // Now we know this can't OOM in the middle of our complex work
1301    ///     output.push_str(data);
1302    ///
1303    ///     Ok(output)
1304    /// }
1305    /// # process_data("rust").expect("why is the test harness OOMing on 4 bytes?");
1306    /// ```
1307    #[stable(feature = "try_reserve", since = "1.57.0")]
1308    pub fn try_reserve(&mut self, additional: usize) -> Result<(), TryReserveError> {
1309        self.vec.try_reserve(additional)
1310    }
1311
1312    /// Tries to reserve the minimum capacity for at least `additional` bytes
1313    /// more than the current length. Unlike [`try_reserve`], this will not
1314    /// deliberately over-allocate to speculatively avoid frequent allocations.
1315    /// After calling `try_reserve_exact`, capacity will be greater than or
1316    /// equal to `self.len() + additional` if it returns `Ok(())`.
1317    /// Does nothing if the capacity is already sufficient.
1318    ///
1319    /// Note that the allocator may give the collection more space than it
1320    /// requests. Therefore, capacity can not be relied upon to be precisely
1321    /// minimal. Prefer [`try_reserve`] if future insertions are expected.
1322    ///
1323    /// [`try_reserve`]: String::try_reserve
1324    ///
1325    /// # Errors
1326    ///
1327    /// If the capacity overflows, or the allocator reports a failure, then an error
1328    /// is returned.
1329    ///
1330    /// # Examples
1331    ///
1332    /// ```
1333    /// use std::collections::TryReserveError;
1334    ///
1335    /// fn process_data(data: &str) -> Result<String, TryReserveError> {
1336    ///     let mut output = String::new();
1337    ///
1338    ///     // Pre-reserve the memory, exiting if we can't
1339    ///     output.try_reserve_exact(data.len())?;
1340    ///
1341    ///     // Now we know this can't OOM in the middle of our complex work
1342    ///     output.push_str(data);
1343    ///
1344    ///     Ok(output)
1345    /// }
1346    /// # process_data("rust").expect("why is the test harness OOMing on 4 bytes?");
1347    /// ```
1348    #[stable(feature = "try_reserve", since = "1.57.0")]
1349    pub fn try_reserve_exact(&mut self, additional: usize) -> Result<(), TryReserveError> {
1350        self.vec.try_reserve_exact(additional)
1351    }
1352
1353    /// Shrinks the capacity of this `String` to match its length.
1354    ///
1355    /// # Examples
1356    ///
1357    /// ```
1358    /// let mut s = String::from("foo");
1359    ///
1360    /// s.reserve(100);
1361    /// assert!(s.capacity() >= 100);
1362    ///
1363    /// s.shrink_to_fit();
1364    /// assert_eq!(3, s.capacity());
1365    /// ```
1366    #[cfg(not(no_global_oom_handling))]
1367    #[inline]
1368    #[stable(feature = "rust1", since = "1.0.0")]
1369    pub fn shrink_to_fit(&mut self) {
1370        self.vec.shrink_to_fit()
1371    }
1372
1373    /// Shrinks the capacity of this `String` with a lower bound.
1374    ///
1375    /// The capacity will remain at least as large as both the length
1376    /// and the supplied value.
1377    ///
1378    /// If the current capacity is less than the lower limit, this is a no-op.
1379    ///
1380    /// # Examples
1381    ///
1382    /// ```
1383    /// let mut s = String::from("foo");
1384    ///
1385    /// s.reserve(100);
1386    /// assert!(s.capacity() >= 100);
1387    ///
1388    /// s.shrink_to(10);
1389    /// assert!(s.capacity() >= 10);
1390    /// s.shrink_to(0);
1391    /// assert!(s.capacity() >= 3);
1392    /// ```
1393    #[cfg(not(no_global_oom_handling))]
1394    #[inline]
1395    #[stable(feature = "shrink_to", since = "1.56.0")]
1396    pub fn shrink_to(&mut self, min_capacity: usize) {
1397        self.vec.shrink_to(min_capacity)
1398    }
1399
1400    /// Appends the given [`char`] to the end of this `String`.
1401    ///
1402    /// # Examples
1403    ///
1404    /// ```
1405    /// let mut s = String::from("abc");
1406    ///
1407    /// s.push('1');
1408    /// s.push('2');
1409    /// s.push('3');
1410    ///
1411    /// assert_eq!("abc123", s);
1412    /// ```
1413    #[cfg(not(no_global_oom_handling))]
1414    #[inline]
1415    #[stable(feature = "rust1", since = "1.0.0")]
1416    pub fn push(&mut self, ch: char) {
1417        match ch.len_utf8() {
1418            1 => self.vec.push(ch as u8),
1419            _ => {
1420                self.vec.extend_from_slice(ch.encode_utf8(&mut [0; char::MAX_LEN_UTF8]).as_bytes())
1421            }
1422        }
1423    }
1424
1425    /// Returns a byte slice of this `String`'s contents.
1426    ///
1427    /// The inverse of this method is [`from_utf8`].
1428    ///
1429    /// [`from_utf8`]: String::from_utf8
1430    ///
1431    /// # Examples
1432    ///
1433    /// ```
1434    /// let s = String::from("hello");
1435    ///
1436    /// assert_eq!(&[104, 101, 108, 108, 111], s.as_bytes());
1437    /// ```
1438    #[inline]
1439    #[must_use]
1440    #[stable(feature = "rust1", since = "1.0.0")]
1441    #[rustc_const_unstable(feature = "const_vec_string_slice", issue = "129041")]
1442    pub const fn as_bytes(&self) -> &[u8] {
1443        self.vec.as_slice()
1444    }
1445
1446    /// Shortens this `String` to the specified length.
1447    ///
1448    /// If `new_len` is greater than or equal to the string's current length, this has no
1449    /// effect.
1450    ///
1451    /// Note that this method has no effect on the allocated capacity
1452    /// of the string
1453    ///
1454    /// # Panics
1455    ///
1456    /// Panics if `new_len` does not lie on a [`char`] boundary.
1457    ///
1458    /// # Examples
1459    ///
1460    /// ```
1461    /// let mut s = String::from("hello");
1462    ///
1463    /// s.truncate(2);
1464    ///
1465    /// assert_eq!("he", s);
1466    /// ```
1467    #[inline]
1468    #[stable(feature = "rust1", since = "1.0.0")]
1469    pub fn truncate(&mut self, new_len: usize) {
1470        if new_len <= self.len() {
1471            assert!(self.is_char_boundary(new_len));
1472            self.vec.truncate(new_len)
1473        }
1474    }
1475
1476    /// Removes the last character from the string buffer and returns it.
1477    ///
1478    /// Returns [`None`] if this `String` is empty.
1479    ///
1480    /// # Examples
1481    ///
1482    /// ```
1483    /// let mut s = String::from("abฤ");
1484    ///
1485    /// assert_eq!(s.pop(), Some('ฤ'));
1486    /// assert_eq!(s.pop(), Some('b'));
1487    /// assert_eq!(s.pop(), Some('a'));
1488    ///
1489    /// assert_eq!(s.pop(), None);
1490    /// ```
1491    #[inline]
1492    #[stable(feature = "rust1", since = "1.0.0")]
1493    pub fn pop(&mut self) -> Option<char> {
1494        let ch = self.chars().rev().next()?;
1495        let newlen = self.len() - ch.len_utf8();
1496        unsafe {
1497            self.vec.set_len(newlen);
1498        }
1499        Some(ch)
1500    }
1501
1502    /// Removes a [`char`] from this `String` at a byte position and returns it.
1503    ///
1504    /// This is an *O*(*n*) operation, as it requires copying every element in the
1505    /// buffer.
1506    ///
1507    /// # Panics
1508    ///
1509    /// Panics if `idx` is larger than or equal to the `String`'s length,
1510    /// or if it does not lie on a [`char`] boundary.
1511    ///
1512    /// # Examples
1513    ///
1514    /// ```
1515    /// let mut s = String::from("abรง");
1516    ///
1517    /// assert_eq!(s.remove(0), 'a');
1518    /// assert_eq!(s.remove(1), 'รง');
1519    /// assert_eq!(s.remove(0), 'b');
1520    /// ```
1521    #[inline]
1522    #[stable(feature = "rust1", since = "1.0.0")]
1523    #[rustc_confusables("delete", "take")]
1524    pub fn remove(&mut self, idx: usize) -> char {
1525        let ch = match self[idx..].chars().next() {
1526            Some(ch) => ch,
1527            None => panic!("cannot remove a char from the end of a string"),
1528        };
1529
1530        let next = idx + ch.len_utf8();
1531        let len = self.len();
1532        unsafe {
1533            ptr::copy(self.vec.as_ptr().add(next), self.vec.as_mut_ptr().add(idx), len - next);
1534            self.vec.set_len(len - (next - idx));
1535        }
1536        ch
1537    }
1538
1539    /// Remove all matches of pattern `pat` in the `String`.
1540    ///
1541    /// # Examples
1542    ///
1543    /// ```
1544    /// #![feature(string_remove_matches)]
1545    /// let mut s = String::from("Trees are not green, the sky is not blue.");
1546    /// s.remove_matches("not ");
1547    /// assert_eq!("Trees are green, the sky is blue.", s);
1548    /// ```
1549    ///
1550    /// Matches will be detected and removed iteratively, so in cases where
1551    /// patterns overlap, only the first pattern will be removed:
1552    ///
1553    /// ```
1554    /// #![feature(string_remove_matches)]
1555    /// let mut s = String::from("banana");
1556    /// s.remove_matches("ana");
1557    /// assert_eq!("bna", s);
1558    /// ```
1559    #[cfg(not(no_global_oom_handling))]
1560    #[unstable(feature = "string_remove_matches", reason = "new API", issue = "72826")]
1561    pub fn remove_matches<P: Pattern>(&mut self, pat: P) {
1562        use core::str::pattern::Searcher;
1563
1564        let rejections = {
1565            let mut searcher = pat.into_searcher(self);
1566            // Per Searcher::next:
1567            //
1568            // A Match result needs to contain the whole matched pattern,
1569            // however Reject results may be split up into arbitrary many
1570            // adjacent fragments. Both ranges may have zero length.
1571            //
1572            // In practice the implementation of Searcher::next_match tends to
1573            // be more efficient, so we use it here and do some work to invert
1574            // matches into rejections since that's what we want to copy below.
1575            let mut front = 0;
1576            let rejections: Vec<_> = from_fn(|| {
1577                let (start, end) = searcher.next_match()?;
1578                let prev_front = front;
1579                front = end;
1580                Some((prev_front, start))
1581            })
1582            .collect();
1583            rejections.into_iter().chain(core::iter::once((front, self.len())))
1584        };
1585
1586        let mut len = 0;
1587        let ptr = self.vec.as_mut_ptr();
1588
1589        for (start, end) in rejections {
1590            let count = end - start;
1591            if start != len {
1592                // SAFETY: per Searcher::next:
1593                //
1594                // The stream of Match and Reject values up to a Done will
1595                // contain index ranges that are adjacent, non-overlapping,
1596                // covering the whole haystack, and laying on utf8
1597                // boundaries.
1598                unsafe {
1599                    ptr::copy(ptr.add(start), ptr.add(len), count);
1600                }
1601            }
1602            len += count;
1603        }
1604
1605        unsafe {
1606            self.vec.set_len(len);
1607        }
1608    }
1609
1610    /// Retains only the characters specified by the predicate.
1611    ///
1612    /// In other words, remove all characters `c` such that `f(c)` returns `false`.
1613    /// This method operates in place, visiting each character exactly once in the
1614    /// original order, and preserves the order of the retained characters.
1615    ///
1616    /// # Examples
1617    ///
1618    /// ```
1619    /// let mut s = String::from("f_o_ob_ar");
1620    ///
1621    /// s.retain(|c| c != '_');
1622    ///
1623    /// assert_eq!(s, "foobar");
1624    /// ```
1625    ///
1626    /// Because the elements are visited exactly once in the original order,
1627    /// external state may be used to decide which elements to keep.
1628    ///
1629    /// ```
1630    /// let mut s = String::from("abcde");
1631    /// let keep = [false, true, true, false, true];
1632    /// let mut iter = keep.iter();
1633    /// s.retain(|_| *iter.next().unwrap());
1634    /// assert_eq!(s, "bce");
1635    /// ```
1636    #[inline]
1637    #[stable(feature = "string_retain", since = "1.26.0")]
1638    pub fn retain<F>(&mut self, mut f: F)
1639    where
1640        F: FnMut(char) -> bool,
1641    {
1642        struct SetLenOnDrop<'a> {
1643            s: &'a mut String,
1644            idx: usize,
1645            del_bytes: usize,
1646        }
1647
1648        impl<'a> Drop for SetLenOnDrop<'a> {
1649            fn drop(&mut self) {
1650                let new_len = self.idx - self.del_bytes;
1651                debug_assert!(new_len <= self.s.len());
1652                unsafe { self.s.vec.set_len(new_len) };
1653            }
1654        }
1655
1656        let len = self.len();
1657        let mut guard = SetLenOnDrop { s: self, idx: 0, del_bytes: 0 };
1658
1659        while guard.idx < len {
1660            let ch =
1661                // SAFETY: `guard.idx` is positive-or-zero and less that len so the `get_unchecked`
1662                // is in bound. `self` is valid UTF-8 like string and the returned slice starts at
1663                // a unicode code point so the `Chars` always return one character.
1664                unsafe { guard.s.get_unchecked(guard.idx..len).chars().next().unwrap_unchecked() };
1665            let ch_len = ch.len_utf8();
1666
1667            if !f(ch) {
1668                guard.del_bytes += ch_len;
1669            } else if guard.del_bytes > 0 {
1670                // SAFETY: `guard.idx` is in bound and `guard.del_bytes` represent the number of
1671                // bytes that are erased from the string so the resulting `guard.idx -
1672                // guard.del_bytes` always represent a valid unicode code point.
1673                //
1674                // `guard.del_bytes` >= `ch.len_utf8()`, so taking a slice with `ch.len_utf8()` len
1675                // is safe.
1676                ch.encode_utf8(unsafe {
1677                    crate::slice::from_raw_parts_mut(
1678                        guard.s.as_mut_ptr().add(guard.idx - guard.del_bytes),
1679                        ch.len_utf8(),
1680                    )
1681                });
1682            }
1683
1684            // Point idx to the next char
1685            guard.idx += ch_len;
1686        }
1687
1688        drop(guard);
1689    }
1690
1691    /// Inserts a character into this `String` at a byte position.
1692    ///
1693    /// This is an *O*(*n*) operation as it requires copying every element in the
1694    /// buffer.
1695    ///
1696    /// # Panics
1697    ///
1698    /// Panics if `idx` is larger than the `String`'s length, or if it does not
1699    /// lie on a [`char`] boundary.
1700    ///
1701    /// # Examples
1702    ///
1703    /// ```
1704    /// let mut s = String::with_capacity(3);
1705    ///
1706    /// s.insert(0, 'f');
1707    /// s.insert(1, 'o');
1708    /// s.insert(2, 'o');
1709    ///
1710    /// assert_eq!("foo", s);
1711    /// ```
1712    #[cfg(not(no_global_oom_handling))]
1713    #[inline]
1714    #[stable(feature = "rust1", since = "1.0.0")]
1715    #[rustc_confusables("set")]
1716    pub fn insert(&mut self, idx: usize, ch: char) {
1717        assert!(self.is_char_boundary(idx));
1718        let mut bits = [0; char::MAX_LEN_UTF8];
1719        let bits = ch.encode_utf8(&mut bits).as_bytes();
1720
1721        unsafe {
1722            self.insert_bytes(idx, bits);
1723        }
1724    }
1725
1726    #[cfg(not(no_global_oom_handling))]
1727    unsafe fn insert_bytes(&mut self, idx: usize, bytes: &[u8]) {
1728        let len = self.len();
1729        let amt = bytes.len();
1730        self.vec.reserve(amt);
1731
1732        unsafe {
1733            ptr::copy(self.vec.as_ptr().add(idx), self.vec.as_mut_ptr().add(idx + amt), len - idx);
1734            ptr::copy_nonoverlapping(bytes.as_ptr(), self.vec.as_mut_ptr().add(idx), amt);
1735            self.vec.set_len(len + amt);
1736        }
1737    }
1738
1739    /// Inserts a string slice into this `String` at a byte position.
1740    ///
1741    /// This is an *O*(*n*) operation as it requires copying every element in the
1742    /// buffer.
1743    ///
1744    /// # Panics
1745    ///
1746    /// Panics if `idx` is larger than the `String`'s length, or if it does not
1747    /// lie on a [`char`] boundary.
1748    ///
1749    /// # Examples
1750    ///
1751    /// ```
1752    /// let mut s = String::from("bar");
1753    ///
1754    /// s.insert_str(0, "foo");
1755    ///
1756    /// assert_eq!("foobar", s);
1757    /// ```
1758    #[cfg(not(no_global_oom_handling))]
1759    #[inline]
1760    #[stable(feature = "insert_str", since = "1.16.0")]
1761    #[cfg_attr(not(test), rustc_diagnostic_item = "string_insert_str")]
1762    pub fn insert_str(&mut self, idx: usize, string: &str) {
1763        assert!(self.is_char_boundary(idx));
1764
1765        unsafe {
1766            self.insert_bytes(idx, string.as_bytes());
1767        }
1768    }
1769
1770    /// Returns a mutable reference to the contents of this `String`.
1771    ///
1772    /// # Safety
1773    ///
1774    /// This function is unsafe because the returned `&mut Vec` allows writing
1775    /// bytes which are not valid UTF-8. If this constraint is violated, using
1776    /// the original `String` after dropping the `&mut Vec` may violate memory
1777    /// safety, as the rest of the standard library assumes that `String`s are
1778    /// valid UTF-8.
1779    ///
1780    /// # Examples
1781    ///
1782    /// ```
1783    /// let mut s = String::from("hello");
1784    ///
1785    /// unsafe {
1786    ///     let vec = s.as_mut_vec();
1787    ///     assert_eq!(&[104, 101, 108, 108, 111][..], &vec[..]);
1788    ///
1789    ///     vec.reverse();
1790    /// }
1791    /// assert_eq!(s, "olleh");
1792    /// ```
1793    #[inline]
1794    #[stable(feature = "rust1", since = "1.0.0")]
1795    #[rustc_const_unstable(feature = "const_vec_string_slice", issue = "129041")]
1796    pub const unsafe fn as_mut_vec(&mut self) -> &mut Vec<u8> {
1797        &mut self.vec
1798    }
1799
1800    /// Returns the length of this `String`, in bytes, not [`char`]s or
1801    /// graphemes. In other words, it might not be what a human considers the
1802    /// length of the string.
1803    ///
1804    /// # Examples
1805    ///
1806    /// ```
1807    /// let a = String::from("foo");
1808    /// assert_eq!(a.len(), 3);
1809    ///
1810    /// let fancy_f = String::from("ฦ’oo");
1811    /// assert_eq!(fancy_f.len(), 4);
1812    /// assert_eq!(fancy_f.chars().count(), 3);
1813    /// ```
1814    #[inline]
1815    #[must_use]
1816    #[stable(feature = "rust1", since = "1.0.0")]
1817    #[rustc_const_unstable(feature = "const_vec_string_slice", issue = "129041")]
1818    #[rustc_confusables("length", "size")]
1819    pub const fn len(&self) -> usize {
1820        self.vec.len()
1821    }
1822
1823    /// Returns `true` if this `String` has a length of zero, and `false` otherwise.
1824    ///
1825    /// # Examples
1826    ///
1827    /// ```
1828    /// let mut v = String::new();
1829    /// assert!(v.is_empty());
1830    ///
1831    /// v.push('a');
1832    /// assert!(!v.is_empty());
1833    /// ```
1834    #[inline]
1835    #[must_use]
1836    #[stable(feature = "rust1", since = "1.0.0")]
1837    #[rustc_const_unstable(feature = "const_vec_string_slice", issue = "129041")]
1838    pub const fn is_empty(&self) -> bool {
1839        self.len() == 0
1840    }
1841
1842    /// Splits the string into two at the given byte index.
1843    ///
1844    /// Returns a newly allocated `String`. `self` contains bytes `[0, at)`, and
1845    /// the returned `String` contains bytes `[at, len)`. `at` must be on the
1846    /// boundary of a UTF-8 code point.
1847    ///
1848    /// Note that the capacity of `self` does not change.
1849    ///
1850    /// # Panics
1851    ///
1852    /// Panics if `at` is not on a `UTF-8` code point boundary, or if it is beyond the last
1853    /// code point of the string.
1854    ///
1855    /// # Examples
1856    ///
1857    /// ```
1858    /// # fn main() {
1859    /// let mut hello = String::from("Hello, World!");
1860    /// let world = hello.split_off(7);
1861    /// assert_eq!(hello, "Hello, ");
1862    /// assert_eq!(world, "World!");
1863    /// # }
1864    /// ```
1865    #[cfg(not(no_global_oom_handling))]
1866    #[inline]
1867    #[stable(feature = "string_split_off", since = "1.16.0")]
1868    #[must_use = "use `.truncate()` if you don't need the other half"]
1869    pub fn split_off(&mut self, at: usize) -> String {
1870        assert!(self.is_char_boundary(at));
1871        let other = self.vec.split_off(at);
1872        unsafe { String::from_utf8_unchecked(other) }
1873    }
1874
1875    /// Truncates this `String`, removing all contents.
1876    ///
1877    /// While this means the `String` will have a length of zero, it does not
1878    /// touch its capacity.
1879    ///
1880    /// # Examples
1881    ///
1882    /// ```
1883    /// let mut s = String::from("foo");
1884    ///
1885    /// s.clear();
1886    ///
1887    /// assert!(s.is_empty());
1888    /// assert_eq!(0, s.len());
1889    /// assert_eq!(3, s.capacity());
1890    /// ```
1891    #[inline]
1892    #[stable(feature = "rust1", since = "1.0.0")]
1893    pub fn clear(&mut self) {
1894        self.vec.clear()
1895    }
1896
1897    /// Removes the specified range from the string in bulk, returning all
1898    /// removed characters as an iterator.
1899    ///
1900    /// The returned iterator keeps a mutable borrow on the string to optimize
1901    /// its implementation.
1902    ///
1903    /// # Panics
1904    ///
1905    /// Panics if the starting point or end point do not lie on a [`char`]
1906    /// boundary, or if they're out of bounds.
1907    ///
1908    /// # Leaking
1909    ///
1910    /// If the returned iterator goes out of scope without being dropped (due to
1911    /// [`core::mem::forget`], for example), the string may still contain a copy
1912    /// of any drained characters, or may have lost characters arbitrarily,
1913    /// including characters outside the range.
1914    ///
1915    /// # Examples
1916    ///
1917    /// ```
1918    /// let mut s = String::from("ฮฑ is alpha, ฮฒ is beta");
1919    /// let beta_offset = s.find('ฮฒ').unwrap_or(s.len());
1920    ///
1921    /// // Remove the range up until the ฮฒ from the string
1922    /// let t: String = s.drain(..beta_offset).collect();
1923    /// assert_eq!(t, "ฮฑ is alpha, ");
1924    /// assert_eq!(s, "ฮฒ is beta");
1925    ///
1926    /// // A full range clears the string, like `clear()` does
1927    /// s.drain(..);
1928    /// assert_eq!(s, "");
1929    /// ```
1930    #[stable(feature = "drain", since = "1.6.0")]
1931    pub fn drain<R>(&mut self, range: R) -> Drain<'_>
1932    where
1933        R: RangeBounds<usize>,
1934    {
1935        // Memory safety
1936        //
1937        // The String version of Drain does not have the memory safety issues
1938        // of the vector version. The data is just plain bytes.
1939        // Because the range removal happens in Drop, if the Drain iterator is leaked,
1940        // the removal will not happen.
1941        let Range { start, end } = slice::range(range, ..self.len());
1942        assert!(self.is_char_boundary(start));
1943        assert!(self.is_char_boundary(end));
1944
1945        // Take out two simultaneous borrows. The &mut String won't be accessed
1946        // until iteration is over, in Drop.
1947        let self_ptr = self as *mut _;
1948        // SAFETY: `slice::range` and `is_char_boundary` do the appropriate bounds checks.
1949        let chars_iter = unsafe { self.get_unchecked(start..end) }.chars();
1950
1951        Drain { start, end, iter: chars_iter, string: self_ptr }
1952    }
1953
1954    /// Converts a `String` into an iterator over the [`char`]s of the string.
1955    ///
1956    /// As a string consists of valid UTF-8, we can iterate through a string
1957    /// by [`char`]. This method returns such an iterator.
1958    ///
1959    /// It's important to remember that [`char`] represents a Unicode Scalar
1960    /// Value, and might not match your idea of what a 'character' is. Iteration
1961    /// over grapheme clusters may be what you actually want. That functionality
1962    /// is not provided by Rust's standard library, check crates.io instead.
1963    ///
1964    /// # Examples
1965    ///
1966    /// Basic usage:
1967    ///
1968    /// ```
1969    /// #![feature(string_into_chars)]
1970    ///
1971    /// let word = String::from("goodbye");
1972    ///
1973    /// let mut chars = word.into_chars();
1974    ///
1975    /// assert_eq!(Some('g'), chars.next());
1976    /// assert_eq!(Some('o'), chars.next());
1977    /// assert_eq!(Some('o'), chars.next());
1978    /// assert_eq!(Some('d'), chars.next());
1979    /// assert_eq!(Some('b'), chars.next());
1980    /// assert_eq!(Some('y'), chars.next());
1981    /// assert_eq!(Some('e'), chars.next());
1982    ///
1983    /// assert_eq!(None, chars.next());
1984    /// ```
1985    ///
1986    /// Remember, [`char`]s might not match your intuition about characters:
1987    ///
1988    /// ```
1989    /// #![feature(string_into_chars)]
1990    ///
1991    /// let y = String::from("yฬ†");
1992    ///
1993    /// let mut chars = y.into_chars();
1994    ///
1995    /// assert_eq!(Some('y'), chars.next()); // not 'yฬ†'
1996    /// assert_eq!(Some('\u{0306}'), chars.next());
1997    ///
1998    /// assert_eq!(None, chars.next());
1999    /// ```
2000    ///
2001    /// [`char`]: prim@char
2002    #[inline]
2003    #[must_use = "`self` will be dropped if the result is not used"]
2004    #[unstable(feature = "string_into_chars", issue = "133125")]
2005    pub fn into_chars(self) -> IntoChars {
2006        IntoChars { bytes: self.into_bytes().into_iter() }
2007    }
2008
2009    /// Removes the specified range in the string,
2010    /// and replaces it with the given string.
2011    /// The given string doesn't need to be the same length as the range.
2012    ///
2013    /// # Panics
2014    ///
2015    /// Panics if the starting point or end point do not lie on a [`char`]
2016    /// boundary, or if they're out of bounds.
2017    ///
2018    /// # Examples
2019    ///
2020    /// ```
2021    /// let mut s = String::from("ฮฑ is alpha, ฮฒ is beta");
2022    /// let beta_offset = s.find('ฮฒ').unwrap_or(s.len());
2023    ///
2024    /// // Replace the range up until the ฮฒ from the string
2025    /// s.replace_range(..beta_offset, "ฮ‘ is capital alpha; ");
2026    /// assert_eq!(s, "ฮ‘ is capital alpha; ฮฒ is beta");
2027    /// ```
2028    #[cfg(not(no_global_oom_handling))]
2029    #[stable(feature = "splice", since = "1.27.0")]
2030    pub fn replace_range<R>(&mut self, range: R, replace_with: &str)
2031    where
2032        R: RangeBounds<usize>,
2033    {
2034        // Memory safety
2035        //
2036        // Replace_range does not have the memory safety issues of a vector Splice.
2037        // of the vector version. The data is just plain bytes.
2038
2039        // WARNING: Inlining this variable would be unsound (#81138)
2040        let start = range.start_bound();
2041        match start {
2042            Included(&n) => assert!(self.is_char_boundary(n)),
2043            Excluded(&n) => assert!(self.is_char_boundary(n + 1)),
2044            Unbounded => {}
2045        };
2046        // WARNING: Inlining this variable would be unsound (#81138)
2047        let end = range.end_bound();
2048        match end {
2049            Included(&n) => assert!(self.is_char_boundary(n + 1)),
2050            Excluded(&n) => assert!(self.is_char_boundary(n)),
2051            Unbounded => {}
2052        };
2053
2054        // Using `range` again would be unsound (#81138)
2055        // We assume the bounds reported by `range` remain the same, but
2056        // an adversarial implementation could change between calls
2057        unsafe { self.as_mut_vec() }.splice((start, end), replace_with.bytes());
2058    }
2059
2060    /// Converts this `String` into a <code>[Box]<[str]></code>.
2061    ///
2062    /// Before doing the conversion, this method discards excess capacity like [`shrink_to_fit`].
2063    /// Note that this call may reallocate and copy the bytes of the string.
2064    ///
2065    /// [`shrink_to_fit`]: String::shrink_to_fit
2066    /// [str]: prim@str "str"
2067    ///
2068    /// # Examples
2069    ///
2070    /// ```
2071    /// let s = String::from("hello");
2072    ///
2073    /// let b = s.into_boxed_str();
2074    /// ```
2075    #[cfg(not(no_global_oom_handling))]
2076    #[stable(feature = "box_str", since = "1.4.0")]
2077    #[must_use = "`self` will be dropped if the result is not used"]
2078    #[inline]
2079    pub fn into_boxed_str(self) -> Box<str> {
2080        let slice = self.vec.into_boxed_slice();
2081        unsafe { from_boxed_utf8_unchecked(slice) }
2082    }
2083
2084    /// Consumes and leaks the `String`, returning a mutable reference to the contents,
2085    /// `&'a mut str`.
2086    ///
2087    /// The caller has free choice over the returned lifetime, including `'static`. Indeed,
2088    /// this function is ideally used for data that lives for the remainder of the program's life,
2089    /// as dropping the returned reference will cause a memory leak.
2090    ///
2091    /// It does not reallocate or shrink the `String`, so the leaked allocation may include unused
2092    /// capacity that is not part of the returned slice. If you want to discard excess capacity,
2093    /// call [`into_boxed_str`], and then [`Box::leak`] instead. However, keep in mind that
2094    /// trimming the capacity may result in a reallocation and copy.
2095    ///
2096    /// [`into_boxed_str`]: Self::into_boxed_str
2097    ///
2098    /// # Examples
2099    ///
2100    /// ```
2101    /// let x = String::from("bucket");
2102    /// let static_ref: &'static mut str = x.leak();
2103    /// assert_eq!(static_ref, "bucket");
2104    /// # // FIXME(https://github.com/rust-lang/miri/issues/3670):
2105    /// # // use -Zmiri-disable-leak-check instead of unleaking in tests meant to leak.
2106    /// # drop(unsafe { Box::from_raw(static_ref) });
2107    /// ```
2108    #[stable(feature = "string_leak", since = "1.72.0")]
2109    #[inline]
2110    pub fn leak<'a>(self) -> &'a mut str {
2111        let slice = self.vec.leak();
2112        unsafe { from_utf8_unchecked_mut(slice) }
2113    }
2114}
2115
2116impl FromUtf8Error {
2117    /// Returns a slice of [`u8`]s bytes that were attempted to convert to a `String`.
2118    ///
2119    /// # Examples
2120    ///
2121    /// ```
2122    /// // some invalid bytes, in a vector
2123    /// let bytes = vec![0, 159];
2124    ///
2125    /// let value = String::from_utf8(bytes);
2126    ///
2127    /// assert_eq!(&[0, 159], value.unwrap_err().as_bytes());
2128    /// ```
2129    #[must_use]
2130    #[stable(feature = "from_utf8_error_as_bytes", since = "1.26.0")]
2131    pub fn as_bytes(&self) -> &[u8] {
2132        &self.bytes[..]
2133    }
2134
2135    /// Converts the bytes into a `String` lossily, substituting invalid UTF-8
2136    /// sequences with replacement characters.
2137    ///
2138    /// See [`String::from_utf8_lossy`] for more details on replacement of
2139    /// invalid sequences, and [`String::from_utf8_lossy_owned`] for the
2140    /// `String` function which corresponds to this function.
2141    ///
2142    /// # Examples
2143    ///
2144    /// ```
2145    /// #![feature(string_from_utf8_lossy_owned)]
2146    /// // some invalid bytes
2147    /// let input: Vec<u8> = b"Hello \xF0\x90\x80World".into();
2148    /// let output = String::from_utf8(input).unwrap_or_else(|e| e.into_utf8_lossy());
2149    ///
2150    /// assert_eq!(String::from("Hello ๏ฟฝWorld"), output);
2151    /// ```
2152    #[must_use]
2153    #[cfg(not(no_global_oom_handling))]
2154    #[unstable(feature = "string_from_utf8_lossy_owned", issue = "129436")]
2155    pub fn into_utf8_lossy(self) -> String {
2156        const REPLACEMENT: &str = "\u{FFFD}";
2157
2158        let mut res = {
2159            let mut v = Vec::with_capacity(self.bytes.len());
2160
2161            // `Utf8Error::valid_up_to` returns the maximum index of validated
2162            // UTF-8 bytes. Copy the valid bytes into the output buffer.
2163            v.extend_from_slice(&self.bytes[..self.error.valid_up_to()]);
2164
2165            // SAFETY: This is safe because the only bytes present in the buffer
2166            // were validated as UTF-8 by the call to `String::from_utf8` which
2167            // produced this `FromUtf8Error`.
2168            unsafe { String::from_utf8_unchecked(v) }
2169        };
2170
2171        let iter = self.bytes[self.error.valid_up_to()..].utf8_chunks();
2172
2173        for chunk in iter {
2174            res.push_str(chunk.valid());
2175            if !chunk.invalid().is_empty() {
2176                res.push_str(REPLACEMENT);
2177            }
2178        }
2179
2180        res
2181    }
2182
2183    /// Returns the bytes that were attempted to convert to a `String`.
2184    ///
2185    /// This method is carefully constructed to avoid allocation. It will
2186    /// consume the error, moving out the bytes, so that a copy of the bytes
2187    /// does not need to be made.
2188    ///
2189    /// # Examples
2190    ///
2191    /// ```
2192    /// // some invalid bytes, in a vector
2193    /// let bytes = vec![0, 159];
2194    ///
2195    /// let value = String::from_utf8(bytes);
2196    ///
2197    /// assert_eq!(vec![0, 159], value.unwrap_err().into_bytes());
2198    /// ```
2199    #[must_use = "`self` will be dropped if the result is not used"]
2200    #[stable(feature = "rust1", since = "1.0.0")]
2201    pub fn into_bytes(self) -> Vec<u8> {
2202        self.bytes
2203    }
2204
2205    /// Fetch a `Utf8Error` to get more details about the conversion failure.
2206    ///
2207    /// The [`Utf8Error`] type provided by [`std::str`] represents an error that may
2208    /// occur when converting a slice of [`u8`]s to a [`&str`]. In this sense, it's
2209    /// an analogue to `FromUtf8Error`. See its documentation for more details
2210    /// on using it.
2211    ///
2212    /// [`std::str`]: core::str "std::str"
2213    /// [`&str`]: prim@str "&str"
2214    ///
2215    /// # Examples
2216    ///
2217    /// ```
2218    /// // some invalid bytes, in a vector
2219    /// let bytes = vec![0, 159];
2220    ///
2221    /// let error = String::from_utf8(bytes).unwrap_err().utf8_error();
2222    ///
2223    /// // the first byte is invalid here
2224    /// assert_eq!(1, error.valid_up_to());
2225    /// ```
2226    #[must_use]
2227    #[stable(feature = "rust1", since = "1.0.0")]
2228    pub fn utf8_error(&self) -> Utf8Error {
2229        self.error
2230    }
2231}
2232
2233#[stable(feature = "rust1", since = "1.0.0")]
2234impl fmt::Display for FromUtf8Error {
2235    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2236        fmt::Display::fmt(&self.error, f)
2237    }
2238}
2239
2240#[stable(feature = "rust1", since = "1.0.0")]
2241impl fmt::Display for FromUtf16Error {
2242    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2243        fmt::Display::fmt("invalid utf-16: lone surrogate found", f)
2244    }
2245}
2246
2247#[stable(feature = "rust1", since = "1.0.0")]
2248impl Error for FromUtf8Error {
2249    #[allow(deprecated)]
2250    fn description(&self) -> &str {
2251        "invalid utf-8"
2252    }
2253}
2254
2255#[stable(feature = "rust1", since = "1.0.0")]
2256impl Error for FromUtf16Error {
2257    #[allow(deprecated)]
2258    fn description(&self) -> &str {
2259        "invalid utf-16"
2260    }
2261}
2262
2263#[cfg(not(no_global_oom_handling))]
2264#[stable(feature = "rust1", since = "1.0.0")]
2265impl Clone for String {
2266    fn clone(&self) -> Self {
2267        String { vec: self.vec.clone() }
2268    }
2269
2270    /// Clones the contents of `source` into `self`.
2271    ///
2272    /// This method is preferred over simply assigning `source.clone()` to `self`,
2273    /// as it avoids reallocation if possible.
2274    fn clone_from(&mut self, source: &Self) {
2275        self.vec.clone_from(&source.vec);
2276    }
2277}
2278
2279#[cfg(not(no_global_oom_handling))]
2280#[stable(feature = "rust1", since = "1.0.0")]
2281impl FromIterator<char> for String {
2282    fn from_iter<I: IntoIterator<Item = char>>(iter: I) -> String {
2283        let mut buf = String::new();
2284        buf.extend(iter);
2285        buf
2286    }
2287}
2288
2289#[cfg(not(no_global_oom_handling))]
2290#[stable(feature = "string_from_iter_by_ref", since = "1.17.0")]
2291impl<'a> FromIterator<&'a char> for String {
2292    fn from_iter<I: IntoIterator<Item = &'a char>>(iter: I) -> String {
2293        let mut buf = String::new();
2294        buf.extend(iter);
2295        buf
2296    }
2297}
2298
2299#[cfg(not(no_global_oom_handling))]
2300#[stable(feature = "rust1", since = "1.0.0")]
2301impl<'a> FromIterator<&'a str> for String {
2302    fn from_iter<I: IntoIterator<Item = &'a str>>(iter: I) -> String {
2303        let mut buf = String::new();
2304        buf.extend(iter);
2305        buf
2306    }
2307}
2308
2309#[cfg(not(no_global_oom_handling))]
2310#[stable(feature = "extend_string", since = "1.4.0")]
2311impl FromIterator<String> for String {
2312    fn from_iter<I: IntoIterator<Item = String>>(iter: I) -> String {
2313        let mut iterator = iter.into_iter();
2314
2315        // Because we're iterating over `String`s, we can avoid at least
2316        // one allocation by getting the first string from the iterator
2317        // and appending to it all the subsequent strings.
2318        match iterator.next() {
2319            None => String::new(),
2320            Some(mut buf) => {
2321                buf.extend(iterator);
2322                buf
2323            }
2324        }
2325    }
2326}
2327
2328#[cfg(not(no_global_oom_handling))]
2329#[stable(feature = "box_str2", since = "1.45.0")]
2330impl<A: Allocator> FromIterator<Box<str, A>> for String {
2331    fn from_iter<I: IntoIterator<Item = Box<str, A>>>(iter: I) -> String {
2332        let mut buf = String::new();
2333        buf.extend(iter);
2334        buf
2335    }
2336}
2337
2338#[cfg(not(no_global_oom_handling))]
2339#[stable(feature = "herd_cows", since = "1.19.0")]
2340impl<'a> FromIterator<Cow<'a, str>> for String {
2341    fn from_iter<I: IntoIterator<Item = Cow<'a, str>>>(iter: I) -> String {
2342        let mut iterator = iter.into_iter();
2343
2344        // Because we're iterating over CoWs, we can (potentially) avoid at least
2345        // one allocation by getting the first item and appending to it all the
2346        // subsequent items.
2347        match iterator.next() {
2348            None => String::new(),
2349            Some(cow) => {
2350                let mut buf = cow.into_owned();
2351                buf.extend(iterator);
2352                buf
2353            }
2354        }
2355    }
2356}
2357
2358#[cfg(not(no_global_oom_handling))]
2359#[stable(feature = "rust1", since = "1.0.0")]
2360impl Extend<char> for String {
2361    fn extend<I: IntoIterator<Item = char>>(&mut self, iter: I) {
2362        let iterator = iter.into_iter();
2363        let (lower_bound, _) = iterator.size_hint();
2364        self.reserve(lower_bound);
2365        iterator.for_each(move |c| self.push(c));
2366    }
2367
2368    #[inline]
2369    fn extend_one(&mut self, c: char) {
2370        self.push(c);
2371    }
2372
2373    #[inline]
2374    fn extend_reserve(&mut self, additional: usize) {
2375        self.reserve(additional);
2376    }
2377}
2378
2379#[cfg(not(no_global_oom_handling))]
2380#[stable(feature = "extend_ref", since = "1.2.0")]
2381impl<'a> Extend<&'a char> for String {
2382    fn extend<I: IntoIterator<Item = &'a char>>(&mut self, iter: I) {
2383        self.extend(iter.into_iter().cloned());
2384    }
2385
2386    #[inline]
2387    fn extend_one(&mut self, &c: &'a char) {
2388        self.push(c);
2389    }
2390
2391    #[inline]
2392    fn extend_reserve(&mut self, additional: usize) {
2393        self.reserve(additional);
2394    }
2395}
2396
2397#[cfg(not(no_global_oom_handling))]
2398#[stable(feature = "rust1", since = "1.0.0")]
2399impl<'a> Extend<&'a str> for String {
2400    fn extend<I: IntoIterator<Item = &'a str>>(&mut self, iter: I) {
2401        iter.into_iter().for_each(move |s| self.push_str(s));
2402    }
2403
2404    #[inline]
2405    fn extend_one(&mut self, s: &'a str) {
2406        self.push_str(s);
2407    }
2408}
2409
2410#[cfg(not(no_global_oom_handling))]
2411#[stable(feature = "box_str2", since = "1.45.0")]
2412impl<A: Allocator> Extend<Box<str, A>> for String {
2413    fn extend<I: IntoIterator<Item = Box<str, A>>>(&mut self, iter: I) {
2414        iter.into_iter().for_each(move |s| self.push_str(&s));
2415    }
2416}
2417
2418#[cfg(not(no_global_oom_handling))]
2419#[stable(feature = "extend_string", since = "1.4.0")]
2420impl Extend<String> for String {
2421    fn extend<I: IntoIterator<Item = String>>(&mut self, iter: I) {
2422        iter.into_iter().for_each(move |s| self.push_str(&s));
2423    }
2424
2425    #[inline]
2426    fn extend_one(&mut self, s: String) {
2427        self.push_str(&s);
2428    }
2429}
2430
2431#[cfg(not(no_global_oom_handling))]
2432#[stable(feature = "herd_cows", since = "1.19.0")]
2433impl<'a> Extend<Cow<'a, str>> for String {
2434    fn extend<I: IntoIterator<Item = Cow<'a, str>>>(&mut self, iter: I) {
2435        iter.into_iter().for_each(move |s| self.push_str(&s));
2436    }
2437
2438    #[inline]
2439    fn extend_one(&mut self, s: Cow<'a, str>) {
2440        self.push_str(&s);
2441    }
2442}
2443
2444#[cfg(not(no_global_oom_handling))]
2445#[unstable(feature = "ascii_char", issue = "110998")]
2446impl Extend<core::ascii::Char> for String {
2447    fn extend<I: IntoIterator<Item = core::ascii::Char>>(&mut self, iter: I) {
2448        self.vec.extend(iter.into_iter().map(|c| c.to_u8()));
2449    }
2450
2451    #[inline]
2452    fn extend_one(&mut self, c: core::ascii::Char) {
2453        self.vec.push(c.to_u8());
2454    }
2455}
2456
2457#[cfg(not(no_global_oom_handling))]
2458#[unstable(feature = "ascii_char", issue = "110998")]
2459impl<'a> Extend<&'a core::ascii::Char> for String {
2460    fn extend<I: IntoIterator<Item = &'a core::ascii::Char>>(&mut self, iter: I) {
2461        self.extend(iter.into_iter().cloned());
2462    }
2463
2464    #[inline]
2465    fn extend_one(&mut self, c: &'a core::ascii::Char) {
2466        self.vec.push(c.to_u8());
2467    }
2468}
2469
2470/// A convenience impl that delegates to the impl for `&str`.
2471///
2472/// # Examples
2473///
2474/// ```
2475/// assert_eq!(String::from("Hello world").find("world"), Some(6));
2476/// ```
2477#[unstable(
2478    feature = "pattern",
2479    reason = "API not fully fleshed out and ready to be stabilized",
2480    issue = "27721"
2481)]
2482impl<'b> Pattern for &'b String {
2483    type Searcher<'a> = <&'b str as Pattern>::Searcher<'a>;
2484
2485    fn into_searcher(self, haystack: &str) -> <&'b str as Pattern>::Searcher<'_> {
2486        self[..].into_searcher(haystack)
2487    }
2488
2489    #[inline]
2490    fn is_contained_in(self, haystack: &str) -> bool {
2491        self[..].is_contained_in(haystack)
2492    }
2493
2494    #[inline]
2495    fn is_prefix_of(self, haystack: &str) -> bool {
2496        self[..].is_prefix_of(haystack)
2497    }
2498
2499    #[inline]
2500    fn strip_prefix_of(self, haystack: &str) -> Option<&str> {
2501        self[..].strip_prefix_of(haystack)
2502    }
2503
2504    #[inline]
2505    fn is_suffix_of<'a>(self, haystack: &'a str) -> bool
2506    where
2507        Self::Searcher<'a>: core::str::pattern::ReverseSearcher<'a>,
2508    {
2509        self[..].is_suffix_of(haystack)
2510    }
2511
2512    #[inline]
2513    fn strip_suffix_of<'a>(self, haystack: &'a str) -> Option<&'a str>
2514    where
2515        Self::Searcher<'a>: core::str::pattern::ReverseSearcher<'a>,
2516    {
2517        self[..].strip_suffix_of(haystack)
2518    }
2519
2520    #[inline]
2521    fn as_utf8_pattern(&self) -> Option<Utf8Pattern<'_>> {
2522        Some(Utf8Pattern::StringPattern(self.as_bytes()))
2523    }
2524}
2525
2526macro_rules! impl_eq {
2527    ($lhs:ty, $rhs: ty) => {
2528        #[stable(feature = "rust1", since = "1.0.0")]
2529        #[allow(unused_lifetimes)]
2530        impl<'a, 'b> PartialEq<$rhs> for $lhs {
2531            #[inline]
2532            fn eq(&self, other: &$rhs) -> bool {
2533                PartialEq::eq(&self[..], &other[..])
2534            }
2535            #[inline]
2536            fn ne(&self, other: &$rhs) -> bool {
2537                PartialEq::ne(&self[..], &other[..])
2538            }
2539        }
2540
2541        #[stable(feature = "rust1", since = "1.0.0")]
2542        #[allow(unused_lifetimes)]
2543        impl<'a, 'b> PartialEq<$lhs> for $rhs {
2544            #[inline]
2545            fn eq(&self, other: &$lhs) -> bool {
2546                PartialEq::eq(&self[..], &other[..])
2547            }
2548            #[inline]
2549            fn ne(&self, other: &$lhs) -> bool {
2550                PartialEq::ne(&self[..], &other[..])
2551            }
2552        }
2553    };
2554}
2555
2556impl_eq! { String, str }
2557impl_eq! { String, &'a str }
2558#[cfg(not(no_global_oom_handling))]
2559impl_eq! { Cow<'a, str>, str }
2560#[cfg(not(no_global_oom_handling))]
2561impl_eq! { Cow<'a, str>, &'b str }
2562#[cfg(not(no_global_oom_handling))]
2563impl_eq! { Cow<'a, str>, String }
2564
2565#[stable(feature = "rust1", since = "1.0.0")]
2566impl Default for String {
2567    /// Creates an empty `String`.
2568    #[inline]
2569    fn default() -> String {
2570        String::new()
2571    }
2572}
2573
2574#[stable(feature = "rust1", since = "1.0.0")]
2575impl fmt::Display for String {
2576    #[inline]
2577    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2578        fmt::Display::fmt(&**self, f)
2579    }
2580}
2581
2582#[stable(feature = "rust1", since = "1.0.0")]
2583impl fmt::Debug for String {
2584    #[inline]
2585    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2586        fmt::Debug::fmt(&**self, f)
2587    }
2588}
2589
2590#[stable(feature = "rust1", since = "1.0.0")]
2591impl hash::Hash for String {
2592    #[inline]
2593    fn hash<H: hash::Hasher>(&self, hasher: &mut H) {
2594        (**self).hash(hasher)
2595    }
2596}
2597
2598/// Implements the `+` operator for concatenating two strings.
2599///
2600/// This consumes the `String` on the left-hand side and re-uses its buffer (growing it if
2601/// necessary). This is done to avoid allocating a new `String` and copying the entire contents on
2602/// every operation, which would lead to *O*(*n*^2) running time when building an *n*-byte string by
2603/// repeated concatenation.
2604///
2605/// The string on the right-hand side is only borrowed; its contents are copied into the returned
2606/// `String`.
2607///
2608/// # Examples
2609///
2610/// Concatenating two `String`s takes the first by value and borrows the second:
2611///
2612/// ```
2613/// let a = String::from("hello");
2614/// let b = String::from(" world");
2615/// let c = a + &b;
2616/// // `a` is moved and can no longer be used here.
2617/// ```
2618///
2619/// If you want to keep using the first `String`, you can clone it and append to the clone instead:
2620///
2621/// ```
2622/// let a = String::from("hello");
2623/// let b = String::from(" world");
2624/// let c = a.clone() + &b;
2625/// // `a` is still valid here.
2626/// ```
2627///
2628/// Concatenating `&str` slices can be done by converting the first to a `String`:
2629///
2630/// ```
2631/// let a = "hello";
2632/// let b = " world";
2633/// let c = a.to_string() + b;
2634/// ```
2635#[cfg(not(no_global_oom_handling))]
2636#[stable(feature = "rust1", since = "1.0.0")]
2637impl Add<&str> for String {
2638    type Output = String;
2639
2640    #[inline]
2641    fn add(mut self, other: &str) -> String {
2642        self.push_str(other);
2643        self
2644    }
2645}
2646
2647/// Implements the `+=` operator for appending to a `String`.
2648///
2649/// This has the same behavior as the [`push_str`][String::push_str] method.
2650#[cfg(not(no_global_oom_handling))]
2651#[stable(feature = "stringaddassign", since = "1.12.0")]
2652impl AddAssign<&str> for String {
2653    #[inline]
2654    fn add_assign(&mut self, other: &str) {
2655        self.push_str(other);
2656    }
2657}
2658
2659#[stable(feature = "rust1", since = "1.0.0")]
2660impl<I> ops::Index<I> for String
2661where
2662    I: slice::SliceIndex<str>,
2663{
2664    type Output = I::Output;
2665
2666    #[inline]
2667    fn index(&self, index: I) -> &I::Output {
2668        index.index(self.as_str())
2669    }
2670}
2671
2672#[stable(feature = "rust1", since = "1.0.0")]
2673impl<I> ops::IndexMut<I> for String
2674where
2675    I: slice::SliceIndex<str>,
2676{
2677    #[inline]
2678    fn index_mut(&mut self, index: I) -> &mut I::Output {
2679        index.index_mut(self.as_mut_str())
2680    }
2681}
2682
2683#[stable(feature = "rust1", since = "1.0.0")]
2684impl ops::Deref for String {
2685    type Target = str;
2686
2687    #[inline]
2688    fn deref(&self) -> &str {
2689        self.as_str()
2690    }
2691}
2692
2693#[unstable(feature = "deref_pure_trait", issue = "87121")]
2694unsafe impl ops::DerefPure for String {}
2695
2696#[stable(feature = "derefmut_for_string", since = "1.3.0")]
2697impl ops::DerefMut for String {
2698    #[inline]
2699    fn deref_mut(&mut self) -> &mut str {
2700        self.as_mut_str()
2701    }
2702}
2703
2704/// A type alias for [`Infallible`].
2705///
2706/// This alias exists for backwards compatibility, and may be eventually deprecated.
2707///
2708/// [`Infallible`]: core::convert::Infallible "convert::Infallible"
2709#[stable(feature = "str_parse_error", since = "1.5.0")]
2710pub type ParseError = core::convert::Infallible;
2711
2712#[cfg(not(no_global_oom_handling))]
2713#[stable(feature = "rust1", since = "1.0.0")]
2714impl FromStr for String {
2715    type Err = core::convert::Infallible;
2716    #[inline]
2717    fn from_str(s: &str) -> Result<String, Self::Err> {
2718        Ok(String::from(s))
2719    }
2720}
2721
2722/// A trait for converting a value to a `String`.
2723///
2724/// This trait is automatically implemented for any type which implements the
2725/// [`Display`] trait. As such, `ToString` shouldn't be implemented directly:
2726/// [`Display`] should be implemented instead, and you get the `ToString`
2727/// implementation for free.
2728///
2729/// [`Display`]: fmt::Display
2730#[cfg_attr(not(test), rustc_diagnostic_item = "ToString")]
2731#[stable(feature = "rust1", since = "1.0.0")]
2732pub trait ToString {
2733    /// Converts the given value to a `String`.
2734    ///
2735    /// # Examples
2736    ///
2737    /// ```
2738    /// let i = 5;
2739    /// let five = String::from("5");
2740    ///
2741    /// assert_eq!(five, i.to_string());
2742    /// ```
2743    #[rustc_conversion_suggestion]
2744    #[stable(feature = "rust1", since = "1.0.0")]
2745    #[cfg_attr(not(test), rustc_diagnostic_item = "to_string_method")]
2746    fn to_string(&self) -> String;
2747}
2748
2749/// # Panics
2750///
2751/// In this implementation, the `to_string` method panics
2752/// if the `Display` implementation returns an error.
2753/// This indicates an incorrect `Display` implementation
2754/// since `fmt::Write for String` never returns an error itself.
2755#[cfg(not(no_global_oom_handling))]
2756#[stable(feature = "rust1", since = "1.0.0")]
2757impl<T: fmt::Display + ?Sized> ToString for T {
2758    #[inline]
2759    fn to_string(&self) -> String {
2760        <Self as SpecToString>::spec_to_string(self)
2761    }
2762}
2763
2764#[cfg(not(no_global_oom_handling))]
2765trait SpecToString {
2766    fn spec_to_string(&self) -> String;
2767}
2768
2769#[cfg(not(no_global_oom_handling))]
2770impl<T: fmt::Display + ?Sized> SpecToString for T {
2771    // A common guideline is to not inline generic functions. However,
2772    // removing `#[inline]` from this method causes non-negligible regressions.
2773    // See <https://github.com/rust-lang/rust/pull/74852>, the last attempt
2774    // to try to remove it.
2775    #[inline]
2776    default fn spec_to_string(&self) -> String {
2777        let mut buf = String::new();
2778        let mut formatter =
2779            core::fmt::Formatter::new(&mut buf, core::fmt::FormattingOptions::new());
2780        // Bypass format_args!() to avoid write_str with zero-length strs
2781        fmt::Display::fmt(self, &mut formatter)
2782            .expect("a Display implementation returned an error unexpectedly");
2783        buf
2784    }
2785}
2786
2787#[cfg(not(no_global_oom_handling))]
2788impl SpecToString for core::ascii::Char {
2789    #[inline]
2790    fn spec_to_string(&self) -> String {
2791        self.as_str().to_owned()
2792    }
2793}
2794
2795#[cfg(not(no_global_oom_handling))]
2796impl SpecToString for char {
2797    #[inline]
2798    fn spec_to_string(&self) -> String {
2799        String::from(self.encode_utf8(&mut [0; char::MAX_LEN_UTF8]))
2800    }
2801}
2802
2803#[cfg(not(no_global_oom_handling))]
2804impl SpecToString for bool {
2805    #[inline]
2806    fn spec_to_string(&self) -> String {
2807        String::from(if *self { "true" } else { "false" })
2808    }
2809}
2810
2811#[cfg(not(no_global_oom_handling))]
2812impl SpecToString for u8 {
2813    #[inline]
2814    fn spec_to_string(&self) -> String {
2815        let mut buf = String::with_capacity(3);
2816        let mut n = *self;
2817        if n >= 10 {
2818            if n >= 100 {
2819                buf.push((b'0' + n / 100) as char);
2820                n %= 100;
2821            }
2822            buf.push((b'0' + n / 10) as char);
2823            n %= 10;
2824        }
2825        buf.push((b'0' + n) as char);
2826        buf
2827    }
2828}
2829
2830#[cfg(not(no_global_oom_handling))]
2831impl SpecToString for i8 {
2832    #[inline]
2833    fn spec_to_string(&self) -> String {
2834        let mut buf = String::with_capacity(4);
2835        if self.is_negative() {
2836            buf.push('-');
2837        }
2838        let mut n = self.unsigned_abs();
2839        if n >= 10 {
2840            if n >= 100 {
2841                buf.push('1');
2842                n -= 100;
2843            }
2844            buf.push((b'0' + n / 10) as char);
2845            n %= 10;
2846        }
2847        buf.push((b'0' + n) as char);
2848        buf
2849    }
2850}
2851
2852// Generic/generated code can sometimes have multiple, nested references
2853// for strings, including `&&&str`s that would never be written
2854// by hand. This macro generates twelve layers of nested `&`-impl
2855// for primitive strings.
2856#[cfg(not(no_global_oom_handling))]
2857macro_rules! to_string_str_wrap_in_ref {
2858    {x $($x:ident)*} => {
2859        &to_string_str_wrap_in_ref! { $($x)* }
2860    };
2861    {} => { str };
2862}
2863#[cfg(not(no_global_oom_handling))]
2864macro_rules! to_string_expr_wrap_in_deref {
2865    {$self:expr ; x $($x:ident)*} => {
2866        *(to_string_expr_wrap_in_deref! { $self ; $($x)* })
2867    };
2868    {$self:expr ;} => { $self };
2869}
2870#[cfg(not(no_global_oom_handling))]
2871macro_rules! to_string_str {
2872    {$($($x:ident)*),+} => {
2873        $(
2874            impl SpecToString for to_string_str_wrap_in_ref!($($x)*) {
2875                #[inline]
2876                fn spec_to_string(&self) -> String {
2877                    String::from(to_string_expr_wrap_in_deref!(self ; $($x)*))
2878                }
2879            }
2880        )+
2881    };
2882}
2883
2884#[cfg(not(no_global_oom_handling))]
2885to_string_str! {
2886    x x x x x x x x x x x x,
2887    x x x x x x x x x x x,
2888    x x x x x x x x x x,
2889    x x x x x x x x x,
2890    x x x x x x x x,
2891    x x x x x x x,
2892    x x x x x x,
2893    x x x x x,
2894    x x x x,
2895    x x x,
2896    x x,
2897    x,
2898}
2899
2900#[cfg(not(no_global_oom_handling))]
2901impl SpecToString for Cow<'_, str> {
2902    #[inline]
2903    fn spec_to_string(&self) -> String {
2904        self[..].to_owned()
2905    }
2906}
2907
2908#[cfg(not(no_global_oom_handling))]
2909impl SpecToString for String {
2910    #[inline]
2911    fn spec_to_string(&self) -> String {
2912        self.to_owned()
2913    }
2914}
2915
2916#[cfg(not(no_global_oom_handling))]
2917impl SpecToString for fmt::Arguments<'_> {
2918    #[inline]
2919    fn spec_to_string(&self) -> String {
2920        crate::fmt::format(*self)
2921    }
2922}
2923
2924#[stable(feature = "rust1", since = "1.0.0")]
2925impl AsRef<str> for String {
2926    #[inline]
2927    fn as_ref(&self) -> &str {
2928        self
2929    }
2930}
2931
2932#[stable(feature = "string_as_mut", since = "1.43.0")]
2933impl AsMut<str> for String {
2934    #[inline]
2935    fn as_mut(&mut self) -> &mut str {
2936        self
2937    }
2938}
2939
2940#[stable(feature = "rust1", since = "1.0.0")]
2941impl AsRef<[u8]> for String {
2942    #[inline]
2943    fn as_ref(&self) -> &[u8] {
2944        self.as_bytes()
2945    }
2946}
2947
2948#[cfg(not(no_global_oom_handling))]
2949#[stable(feature = "rust1", since = "1.0.0")]
2950impl From<&str> for String {
2951    /// Converts a `&str` into a [`String`].
2952    ///
2953    /// The result is allocated on the heap.
2954    #[inline]
2955    fn from(s: &str) -> String {
2956        s.to_owned()
2957    }
2958}
2959
2960#[cfg(not(no_global_oom_handling))]
2961#[stable(feature = "from_mut_str_for_string", since = "1.44.0")]
2962impl From<&mut str> for String {
2963    /// Converts a `&mut str` into a [`String`].
2964    ///
2965    /// The result is allocated on the heap.
2966    #[inline]
2967    fn from(s: &mut str) -> String {
2968        s.to_owned()
2969    }
2970}
2971
2972#[cfg(not(no_global_oom_handling))]
2973#[stable(feature = "from_ref_string", since = "1.35.0")]
2974impl From<&String> for String {
2975    /// Converts a `&String` into a [`String`].
2976    ///
2977    /// This clones `s` and returns the clone.
2978    #[inline]
2979    fn from(s: &String) -> String {
2980        s.clone()
2981    }
2982}
2983
2984// note: test pulls in std, which causes errors here
2985#[cfg(not(test))]
2986#[stable(feature = "string_from_box", since = "1.18.0")]
2987impl From<Box<str>> for String {
2988    /// Converts the given boxed `str` slice to a [`String`].
2989    /// It is notable that the `str` slice is owned.
2990    ///
2991    /// # Examples
2992    ///
2993    /// ```
2994    /// let s1: String = String::from("hello world");
2995    /// let s2: Box<str> = s1.into_boxed_str();
2996    /// let s3: String = String::from(s2);
2997    ///
2998    /// assert_eq!("hello world", s3)
2999    /// ```
3000    fn from(s: Box<str>) -> String {
3001        s.into_string()
3002    }
3003}
3004
3005#[cfg(not(no_global_oom_handling))]
3006#[stable(feature = "box_from_str", since = "1.20.0")]
3007impl From<String> for Box<str> {
3008    /// Converts the given [`String`] to a boxed `str` slice that is owned.
3009    ///
3010    /// # Examples
3011    ///
3012    /// ```
3013    /// let s1: String = String::from("hello world");
3014    /// let s2: Box<str> = Box::from(s1);
3015    /// let s3: String = String::from(s2);
3016    ///
3017    /// assert_eq!("hello world", s3)
3018    /// ```
3019    fn from(s: String) -> Box<str> {
3020        s.into_boxed_str()
3021    }
3022}
3023
3024#[cfg(not(no_global_oom_handling))]
3025#[stable(feature = "string_from_cow_str", since = "1.14.0")]
3026impl<'a> From<Cow<'a, str>> for String {
3027    /// Converts a clone-on-write string to an owned
3028    /// instance of [`String`].
3029    ///
3030    /// This extracts the owned string,
3031    /// clones the string if it is not already owned.
3032    ///
3033    /// # Example
3034    ///
3035    /// ```
3036    /// # use std::borrow::Cow;
3037    /// // If the string is not owned...
3038    /// let cow: Cow<'_, str> = Cow::Borrowed("eggplant");
3039    /// // It will allocate on the heap and copy the string.
3040    /// let owned: String = String::from(cow);
3041    /// assert_eq!(&owned[..], "eggplant");
3042    /// ```
3043    fn from(s: Cow<'a, str>) -> String {
3044        s.into_owned()
3045    }
3046}
3047
3048#[cfg(not(no_global_oom_handling))]
3049#[stable(feature = "rust1", since = "1.0.0")]
3050impl<'a> From<&'a str> for Cow<'a, str> {
3051    /// Converts a string slice into a [`Borrowed`] variant.
3052    /// No heap allocation is performed, and the string
3053    /// is not copied.
3054    ///
3055    /// # Example
3056    ///
3057    /// ```
3058    /// # use std::borrow::Cow;
3059    /// assert_eq!(Cow::from("eggplant"), Cow::Borrowed("eggplant"));
3060    /// ```
3061    ///
3062    /// [`Borrowed`]: crate::borrow::Cow::Borrowed "borrow::Cow::Borrowed"
3063    #[inline]
3064    fn from(s: &'a str) -> Cow<'a, str> {
3065        Cow::Borrowed(s)
3066    }
3067}
3068
3069#[cfg(not(no_global_oom_handling))]
3070#[stable(feature = "rust1", since = "1.0.0")]
3071impl<'a> From<String> for Cow<'a, str> {
3072    /// Converts a [`String`] into an [`Owned`] variant.
3073    /// No heap allocation is performed, and the string
3074    /// is not copied.
3075    ///
3076    /// # Example
3077    ///
3078    /// ```
3079    /// # use std::borrow::Cow;
3080    /// let s = "eggplant".to_string();
3081    /// let s2 = "eggplant".to_string();
3082    /// assert_eq!(Cow::from(s), Cow::<'static, str>::Owned(s2));
3083    /// ```
3084    ///
3085    /// [`Owned`]: crate::borrow::Cow::Owned "borrow::Cow::Owned"
3086    #[inline]
3087    fn from(s: String) -> Cow<'a, str> {
3088        Cow::Owned(s)
3089    }
3090}
3091
3092#[cfg(not(no_global_oom_handling))]
3093#[stable(feature = "cow_from_string_ref", since = "1.28.0")]
3094impl<'a> From<&'a String> for Cow<'a, str> {
3095    /// Converts a [`String`] reference into a [`Borrowed`] variant.
3096    /// No heap allocation is performed, and the string
3097    /// is not copied.
3098    ///
3099    /// # Example
3100    ///
3101    /// ```
3102    /// # use std::borrow::Cow;
3103    /// let s = "eggplant".to_string();
3104    /// assert_eq!(Cow::from(&s), Cow::Borrowed("eggplant"));
3105    /// ```
3106    ///
3107    /// [`Borrowed`]: crate::borrow::Cow::Borrowed "borrow::Cow::Borrowed"
3108    #[inline]
3109    fn from(s: &'a String) -> Cow<'a, str> {
3110        Cow::Borrowed(s.as_str())
3111    }
3112}
3113
3114#[cfg(not(no_global_oom_handling))]
3115#[stable(feature = "cow_str_from_iter", since = "1.12.0")]
3116impl<'a> FromIterator<char> for Cow<'a, str> {
3117    fn from_iter<I: IntoIterator<Item = char>>(it: I) -> Cow<'a, str> {
3118        Cow::Owned(FromIterator::from_iter(it))
3119    }
3120}
3121
3122#[cfg(not(no_global_oom_handling))]
3123#[stable(feature = "cow_str_from_iter", since = "1.12.0")]
3124impl<'a, 'b> FromIterator<&'b str> for Cow<'a, str> {
3125    fn from_iter<I: IntoIterator<Item = &'b str>>(it: I) -> Cow<'a, str> {
3126        Cow::Owned(FromIterator::from_iter(it))
3127    }
3128}
3129
3130#[cfg(not(no_global_oom_handling))]
3131#[stable(feature = "cow_str_from_iter", since = "1.12.0")]
3132impl<'a> FromIterator<String> for Cow<'a, str> {
3133    fn from_iter<I: IntoIterator<Item = String>>(it: I) -> Cow<'a, str> {
3134        Cow::Owned(FromIterator::from_iter(it))
3135    }
3136}
3137
3138#[stable(feature = "from_string_for_vec_u8", since = "1.14.0")]
3139impl From<String> for Vec<u8> {
3140    /// Converts the given [`String`] to a vector [`Vec`] that holds values of type [`u8`].
3141    ///
3142    /// # Examples
3143    ///
3144    /// ```
3145    /// let s1 = String::from("hello world");
3146    /// let v1 = Vec::from(s1);
3147    ///
3148    /// for b in v1 {
3149    ///     println!("{b}");
3150    /// }
3151    /// ```
3152    fn from(string: String) -> Vec<u8> {
3153        string.into_bytes()
3154    }
3155}
3156
3157#[stable(feature = "try_from_vec_u8_for_string", since = "CURRENT_RUSTC_VERSION")]
3158impl TryFrom<Vec<u8>> for String {
3159    type Error = FromUtf8Error;
3160    /// Converts the given [`Vec<u8>`] into a  [`String`] if it contains valid UTF-8 data.
3161    ///
3162    /// # Examples
3163    ///
3164    /// ```
3165    /// let s1 = b"hello world".to_vec();
3166    /// let v1 = String::try_from(s1).unwrap();
3167    /// assert_eq!(v1, "hello world");
3168    ///
3169    /// ```
3170    fn try_from(bytes: Vec<u8>) -> Result<Self, Self::Error> {
3171        Self::from_utf8(bytes)
3172    }
3173}
3174
3175#[cfg(not(no_global_oom_handling))]
3176#[stable(feature = "rust1", since = "1.0.0")]
3177impl fmt::Write for String {
3178    #[inline]
3179    fn write_str(&mut self, s: &str) -> fmt::Result {
3180        self.push_str(s);
3181        Ok(())
3182    }
3183
3184    #[inline]
3185    fn write_char(&mut self, c: char) -> fmt::Result {
3186        self.push(c);
3187        Ok(())
3188    }
3189}
3190
3191/// An iterator over the [`char`]s of a string.
3192///
3193/// This struct is created by the [`into_chars`] method on [`String`].
3194/// See its documentation for more.
3195///
3196/// [`char`]: prim@char
3197/// [`into_chars`]: String::into_chars
3198#[cfg_attr(not(no_global_oom_handling), derive(Clone))]
3199#[must_use = "iterators are lazy and do nothing unless consumed"]
3200#[unstable(feature = "string_into_chars", issue = "133125")]
3201pub struct IntoChars {
3202    bytes: vec::IntoIter<u8>,
3203}
3204
3205#[unstable(feature = "string_into_chars", issue = "133125")]
3206impl fmt::Debug for IntoChars {
3207    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
3208        f.debug_tuple("IntoChars").field(&self.as_str()).finish()
3209    }
3210}
3211
3212impl IntoChars {
3213    /// Views the underlying data as a subslice of the original data.
3214    ///
3215    /// # Examples
3216    ///
3217    /// ```
3218    /// #![feature(string_into_chars)]
3219    ///
3220    /// let mut chars = String::from("abc").into_chars();
3221    ///
3222    /// assert_eq!(chars.as_str(), "abc");
3223    /// chars.next();
3224    /// assert_eq!(chars.as_str(), "bc");
3225    /// chars.next();
3226    /// chars.next();
3227    /// assert_eq!(chars.as_str(), "");
3228    /// ```
3229    #[unstable(feature = "string_into_chars", issue = "133125")]
3230    #[must_use]
3231    #[inline]
3232    pub fn as_str(&self) -> &str {
3233        // SAFETY: `bytes` is a valid UTF-8 string.
3234        unsafe { str::from_utf8_unchecked(self.bytes.as_slice()) }
3235    }
3236
3237    /// Consumes the `IntoChars`, returning the remaining string.
3238    ///
3239    /// # Examples
3240    ///
3241    /// ```
3242    /// #![feature(string_into_chars)]
3243    ///
3244    /// let chars = String::from("abc").into_chars();
3245    /// assert_eq!(chars.into_string(), "abc");
3246    ///
3247    /// let mut chars = String::from("def").into_chars();
3248    /// chars.next();
3249    /// assert_eq!(chars.into_string(), "ef");
3250    /// ```
3251    #[cfg(not(no_global_oom_handling))]
3252    #[unstable(feature = "string_into_chars", issue = "133125")]
3253    #[inline]
3254    pub fn into_string(self) -> String {
3255        // Safety: `bytes` are kept in UTF-8 form, only removing whole `char`s at a time.
3256        unsafe { String::from_utf8_unchecked(self.bytes.collect()) }
3257    }
3258
3259    #[inline]
3260    fn iter(&self) -> CharIndices<'_> {
3261        self.as_str().char_indices()
3262    }
3263}
3264
3265#[unstable(feature = "string_into_chars", issue = "133125")]
3266impl Iterator for IntoChars {
3267    type Item = char;
3268
3269    #[inline]
3270    fn next(&mut self) -> Option<char> {
3271        let mut iter = self.iter();
3272        match iter.next() {
3273            None => None,
3274            Some((_, ch)) => {
3275                let offset = iter.offset();
3276                // `offset` is a valid index.
3277                let _ = self.bytes.advance_by(offset);
3278                Some(ch)
3279            }
3280        }
3281    }
3282
3283    #[inline]
3284    fn count(self) -> usize {
3285        self.iter().count()
3286    }
3287
3288    #[inline]
3289    fn size_hint(&self) -> (usize, Option<usize>) {
3290        self.iter().size_hint()
3291    }
3292
3293    #[inline]
3294    fn last(mut self) -> Option<char> {
3295        self.next_back()
3296    }
3297}
3298
3299#[unstable(feature = "string_into_chars", issue = "133125")]
3300impl DoubleEndedIterator for IntoChars {
3301    #[inline]
3302    fn next_back(&mut self) -> Option<char> {
3303        let len = self.as_str().len();
3304        let mut iter = self.iter();
3305        match iter.next_back() {
3306            None => None,
3307            Some((idx, ch)) => {
3308                // `idx` is a valid index.
3309                let _ = self.bytes.advance_back_by(len - idx);
3310                Some(ch)
3311            }
3312        }
3313    }
3314}
3315
3316#[unstable(feature = "string_into_chars", issue = "133125")]
3317impl FusedIterator for IntoChars {}
3318
3319/// A draining iterator for `String`.
3320///
3321/// This struct is created by the [`drain`] method on [`String`]. See its
3322/// documentation for more.
3323///
3324/// [`drain`]: String::drain
3325#[stable(feature = "drain", since = "1.6.0")]
3326pub struct Drain<'a> {
3327    /// Will be used as &'a mut String in the destructor
3328    string: *mut String,
3329    /// Start of part to remove
3330    start: usize,
3331    /// End of part to remove
3332    end: usize,
3333    /// Current remaining range to remove
3334    iter: Chars<'a>,
3335}
3336
3337#[stable(feature = "collection_debug", since = "1.17.0")]
3338impl fmt::Debug for Drain<'_> {
3339    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
3340        f.debug_tuple("Drain").field(&self.as_str()).finish()
3341    }
3342}
3343
3344#[stable(feature = "drain", since = "1.6.0")]
3345unsafe impl Sync for Drain<'_> {}
3346#[stable(feature = "drain", since = "1.6.0")]
3347unsafe impl Send for Drain<'_> {}
3348
3349#[stable(feature = "drain", since = "1.6.0")]
3350impl Drop for Drain<'_> {
3351    fn drop(&mut self) {
3352        unsafe {
3353            // Use Vec::drain. "Reaffirm" the bounds checks to avoid
3354            // panic code being inserted again.
3355            let self_vec = (*self.string).as_mut_vec();
3356            if self.start <= self.end && self.end <= self_vec.len() {
3357                self_vec.drain(self.start..self.end);
3358            }
3359        }
3360    }
3361}
3362
3363impl<'a> Drain<'a> {
3364    /// Returns the remaining (sub)string of this iterator as a slice.
3365    ///
3366    /// # Examples
3367    ///
3368    /// ```
3369    /// let mut s = String::from("abc");
3370    /// let mut drain = s.drain(..);
3371    /// assert_eq!(drain.as_str(), "abc");
3372    /// let _ = drain.next().unwrap();
3373    /// assert_eq!(drain.as_str(), "bc");
3374    /// ```
3375    #[must_use]
3376    #[stable(feature = "string_drain_as_str", since = "1.55.0")]
3377    pub fn as_str(&self) -> &str {
3378        self.iter.as_str()
3379    }
3380}
3381
3382#[stable(feature = "string_drain_as_str", since = "1.55.0")]
3383impl<'a> AsRef<str> for Drain<'a> {
3384    fn as_ref(&self) -> &str {
3385        self.as_str()
3386    }
3387}
3388
3389#[stable(feature = "string_drain_as_str", since = "1.55.0")]
3390impl<'a> AsRef<[u8]> for Drain<'a> {
3391    fn as_ref(&self) -> &[u8] {
3392        self.as_str().as_bytes()
3393    }
3394}
3395
3396#[stable(feature = "drain", since = "1.6.0")]
3397impl Iterator for Drain<'_> {
3398    type Item = char;
3399
3400    #[inline]
3401    fn next(&mut self) -> Option<char> {
3402        self.iter.next()
3403    }
3404
3405    fn size_hint(&self) -> (usize, Option<usize>) {
3406        self.iter.size_hint()
3407    }
3408
3409    #[inline]
3410    fn last(mut self) -> Option<char> {
3411        self.next_back()
3412    }
3413}
3414
3415#[stable(feature = "drain", since = "1.6.0")]
3416impl DoubleEndedIterator for Drain<'_> {
3417    #[inline]
3418    fn next_back(&mut self) -> Option<char> {
3419        self.iter.next_back()
3420    }
3421}
3422
3423#[stable(feature = "fused", since = "1.26.0")]
3424impl FusedIterator for Drain<'_> {}
3425
3426#[cfg(not(no_global_oom_handling))]
3427#[stable(feature = "from_char_for_string", since = "1.46.0")]
3428impl From<char> for String {
3429    /// Allocates an owned [`String`] from a single character.
3430    ///
3431    /// # Example
3432    /// ```rust
3433    /// let c: char = 'a';
3434    /// let s: String = String::from(c);
3435    /// assert_eq!("a", &s[..]);
3436    /// ```
3437    #[inline]
3438    fn from(c: char) -> Self {
3439        c.to_string()
3440    }
3441}