Movatterモバイル変換


[0]ホーム

URL:


alloc/
string.rs

1//! A UTF-8–encoded, growable string.2//!3//! This module contains the [`String`] type, the [`ToString`] trait for4//! converting to strings, and several error types that may result from5//! working with [`String`]s.6//!7//! # Examples8//!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 with19//! `+`: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 of28//! 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//! ```4243#![stable(feature ="rust1", since ="1.0.0")]4445usecore::error::Error;46usecore::iter::FusedIterator;47#[cfg(not(no_global_oom_handling))]48usecore::iter::from_fn;49#[cfg(not(no_global_oom_handling))]50usecore::ops::Add;51#[cfg(not(no_global_oom_handling))]52usecore::ops::AddAssign;53#[cfg(not(no_global_oom_handling))]54usecore::ops::Bound::{Excluded, Included, Unbounded};55usecore::ops::{self, Range, RangeBounds};56usecore::str::pattern::{Pattern, Utf8Pattern};57usecore::{fmt, hash, ptr, slice};5859#[cfg(not(no_global_oom_handling))]60usecrate::alloc::Allocator;61#[cfg(not(no_global_oom_handling))]62usecrate::borrow::{Cow, ToOwned};63usecrate::boxed::Box;64usecrate::collections::TryReserveError;65usecrate::str::{self, CharIndices, Chars, Utf8Error, from_utf8_unchecked_mut};66#[cfg(not(no_global_oom_handling))]67usecrate::str::{FromStr, from_boxed_utf8_unchecked};68usecrate::vec::{self, Vec};6970/// A UTF-8–encoded, growable string.71///72/// `String` is the most common string type. It has ownership over the contents73/// 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/// # Examples77///78/// You can create a `String` from [a literal string][`&str`] with [`String::from`]:79///80/// [`String::from`]: From::from81///82/// ```83/// let hello = String::from("Hello, world!");84/// ```85///86/// You can append a [`char`] to a `String` with the [`push`] method, and87/// 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::push97/// [`push_str`]: String::push_str98///99/// If you have a vector of UTF-8 bytes, you can create a `String` from it with100/// the [`from_utf8`] method:101///102/// ```103/// // some bytes, in a vector104/// 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_utf8113///114/// # UTF-8115///116/// `String`s are always valid UTF-8. If you need a non-UTF-8 string, consider117/// [`OsString`]. It is similar, but without the UTF-8 constraint. Because UTF-8118/// is a variable width encoding, `String`s are typically smaller than an array of119/// the same `char`s:120///121/// ```122/// // `s` is ASCII which represents each `char` as one byte123/// let s = "hello";124/// assert_eq!(s.len(), 5);125///126/// // A `char` array with the same contents would be longer because127/// // every `char` is four bytes128/// let s = ['h', 'e', 'l', 'l', 'o'];129/// let size: usize = s.into_iter().map(|c| size_of_val(&c)).sum();130/// assert_eq!(size, 20);131///132/// // However, for non-ASCII strings, the difference will be smaller133/// // and sometimes they are the same134/// let s = "💖💖💖💖💖";135/// assert_eq!(s.len(), 20);136///137/// let s = ['💖', '💖', '💖', '💖', '💖'];138/// let size: usize = s.into_iter().map(|c| size_of_val(&c)).sum();139/// assert_eq!(size, 20);140/// ```141///142/// This raises interesting questions as to how `s[i]` should work.143/// What should `i` be here? Several options include byte indices and144/// `char` indices but, because of UTF-8 encoding, only byte indices145/// would provide constant time indexing. Getting the `i`th `char`, for146/// example, is available using [`chars`]:147///148/// ```149/// let s = "hello";150/// let third_character = s.chars().nth(2);151/// assert_eq!(third_character, Some('l'));152///153/// let s = "💖💖💖💖💖";154/// let third_character = s.chars().nth(2);155/// assert_eq!(third_character, Some('💖'));156/// ```157///158/// Next, what should `s[i]` return? Because indexing returns a reference159/// to underlying data it could be `&u8`, `&[u8]`, or something similar.160/// Since we're only providing one index, `&u8` makes the most sense but that161/// might not be what the user expects and can be explicitly achieved with162/// [`as_bytes()`]:163///164/// ```165/// // The first byte is 104 - the byte value of `'h'`166/// let s = "hello";167/// assert_eq!(s.as_bytes()[0], 104);168/// // or169/// assert_eq!(s.as_bytes()[0], b'h');170///171/// // The first byte is 240 which isn't obviously useful172/// let s = "💖💖💖💖💖";173/// assert_eq!(s.as_bytes()[0], 240);174/// ```175///176/// Due to these ambiguities/restrictions, indexing with a `usize` is simply177/// forbidden:178///179/// ```compile_fail,E0277180/// let s = "hello";181///182/// // The following will not compile!183/// println!("The first letter of s is {}", s[0]);184/// ```185///186/// It is more clear, however, how `&s[i..j]` should work (that is,187/// indexing with a range). It should accept byte indices (to be constant-time)188/// and return a `&str` which is UTF-8 encoded. This is also called "string slicing".189/// Note this will panic if the byte indices provided are not character190/// boundaries - see [`is_char_boundary`] for more details. See the implementations191/// for [`SliceIndex<str>`] for more details on string slicing. For a non-panicking192/// version of string slicing, see [`get`].193///194/// [`OsString`]: ../../std/ffi/struct.OsString.html "ffi::OsString"195/// [`SliceIndex<str>`]: core::slice::SliceIndex196/// [`as_bytes()`]: str::as_bytes197/// [`get`]: str::get198/// [`is_char_boundary`]: str::is_char_boundary199///200/// The [`bytes`] and [`chars`] methods return iterators over the bytes and201/// codepoints of the string, respectively. To iterate over codepoints along202/// with byte indices, use [`char_indices`].203///204/// [`bytes`]: str::bytes205/// [`chars`]: str::chars206/// [`char_indices`]: str::char_indices207///208/// # Deref209///210/// `String` implements <code>[Deref]<Target = [str]></code>, and so inherits all of [`str`]'s211/// methods. In addition, this means that you can pass a `String` to a212/// function which takes a [`&str`] by using an ampersand (`&`):213///214/// ```215/// fn takes_str(s: &str) { }216///217/// let s = String::from("Hello");218///219/// takes_str(&s);220/// ```221///222/// This will create a [`&str`] from the `String` and pass it in. This223/// conversion is very inexpensive, and so generally, functions will accept224/// [`&str`]s as arguments unless they need a `String` for some specific225/// reason.226///227/// In certain cases Rust doesn't have enough information to make this228/// conversion, known as [`Deref`] coercion. In the following example a string229/// slice [`&'a str`][`&str`] implements the trait `TraitExample`, and the function230/// `example_func` takes anything that implements the trait. In this case Rust231/// would need to make two implicit conversions, which Rust doesn't have the232/// means to do. For that reason, the following example will not compile.233///234/// ```compile_fail,E0277235/// trait TraitExample {}236///237/// impl<'a> TraitExample for &'a str {}238///239/// fn example_func<A: TraitExample>(example_arg: A) {}240///241/// let example_string = String::from("example_string");242/// example_func(&example_string);243/// ```244///245/// There are two options that would work instead. The first would be to246/// change the line `example_func(&example_string);` to247/// `example_func(example_string.as_str());`, using the method [`as_str()`]248/// to explicitly extract the string slice containing the string. The second249/// way changes `example_func(&example_string);` to250/// `example_func(&*example_string);`. In this case we are dereferencing a251/// `String` to a [`str`], then referencing the [`str`] back to252/// [`&str`]. The second way is more idiomatic, however both work to do the253/// conversion explicitly rather than relying on the implicit conversion.254///255/// # Representation256///257/// A `String` is made up of three components: a pointer to some bytes, a258/// length, and a capacity. The pointer points to the internal buffer which `String`259/// uses to store its data. The length is the number of bytes currently stored260/// in the buffer, and the capacity is the size of the buffer in bytes. As such,261/// the length will always be less than or equal to the capacity.262///263/// This buffer is always stored on the heap.264///265/// You can look at these with the [`as_ptr`], [`len`], and [`capacity`]266/// methods:267///268/// ```269/// let story = String::from("Once upon a time...");270///271/// // Deconstruct the String into parts.272/// let (ptr, len, capacity) = story.into_raw_parts();273///274/// // story has nineteen bytes275/// assert_eq!(19, len);276///277/// // We can re-build a String out of ptr, len, and capacity. This is all278/// // unsafe because we are responsible for making sure the components are279/// // valid:280/// let s = unsafe { String::from_raw_parts(ptr, len, capacity) } ;281///282/// assert_eq!(String::from("Once upon a time..."), s);283/// ```284///285/// [`as_ptr`]: str::as_ptr286/// [`len`]: String::len287/// [`capacity`]: String::capacity288///289/// If a `String` has enough capacity, adding elements to it will not290/// re-allocate. For example, consider this program:291///292/// ```293/// let mut s = String::new();294///295/// println!("{}", s.capacity());296///297/// for _ in 0..5 {298///     s.push_str("hello");299///     println!("{}", s.capacity());300/// }301/// ```302///303/// This will output the following:304///305/// ```text306/// 0307/// 8308/// 16309/// 16310/// 32311/// 32312/// ```313///314/// At first, we have no memory allocated at all, but as we append to the315/// string, it increases its capacity appropriately. If we instead use the316/// [`with_capacity`] method to allocate the correct capacity initially:317///318/// ```319/// let mut s = String::with_capacity(25);320///321/// println!("{}", s.capacity());322///323/// for _ in 0..5 {324///     s.push_str("hello");325///     println!("{}", s.capacity());326/// }327/// ```328///329/// [`with_capacity`]: String::with_capacity330///331/// We end up with a different output:332///333/// ```text334/// 25335/// 25336/// 25337/// 25338/// 25339/// 25340/// ```341///342/// Here, there's no need to allocate more memory inside the loop.343///344/// [str]: prim@str "str"345/// [`str`]: prim@str "str"346/// [`&str`]: prim@str "&str"347/// [Deref]: core::ops::Deref "ops::Deref"348/// [`Deref`]: core::ops::Deref "ops::Deref"349/// [`as_str()`]: String::as_str350#[derive(PartialEq, PartialOrd, Eq, Ord)]351#[stable(feature ="rust1", since ="1.0.0")]352#[lang ="String"]353pub structString {354    vec: Vec<u8>,355}356357/// A possible error value when converting a `String` from a UTF-8 byte vector.358///359/// This type is the error type for the [`from_utf8`] method on [`String`]. It360/// is designed in such a way to carefully avoid reallocations: the361/// [`into_bytes`] method will give back the byte vector that was used in the362/// conversion attempt.363///364/// [`from_utf8`]: String::from_utf8365/// [`into_bytes`]: FromUtf8Error::into_bytes366///367/// The [`Utf8Error`] type provided by [`std::str`] represents an error that may368/// occur when converting a slice of [`u8`]s to a [`&str`]. In this sense, it's369/// an analogue to `FromUtf8Error`, and you can get one from a `FromUtf8Error`370/// through the [`utf8_error`] method.371///372/// [`Utf8Error`]: str::Utf8Error "std::str::Utf8Error"373/// [`std::str`]: core::str "std::str"374/// [`&str`]: prim@str "&str"375/// [`utf8_error`]: FromUtf8Error::utf8_error376///377/// # Examples378///379/// ```380/// // some invalid bytes, in a vector381/// let bytes = vec![0, 159];382///383/// let value = String::from_utf8(bytes);384///385/// assert!(value.is_err());386/// assert_eq!(vec![0, 159], value.unwrap_err().into_bytes());387/// ```388#[stable(feature ="rust1", since ="1.0.0")]389#[cfg_attr(not(no_global_oom_handling), derive(Clone))]390#[derive(Debug, PartialEq, Eq)]391pub structFromUtf8Error {392    bytes: Vec<u8>,393    error: Utf8Error,394}395396/// A possible error value when converting a `String` from a UTF-16 byte slice.397///398/// This type is the error type for the [`from_utf16`] method on [`String`].399///400/// [`from_utf16`]: String::from_utf16401///402/// # Examples403///404/// ```405/// // 𝄞mu<invalid>ic406/// let v = &[0xD834, 0xDD1E, 0x006d, 0x0075,407///           0xD800, 0x0069, 0x0063];408///409/// assert!(String::from_utf16(v).is_err());410/// ```411#[stable(feature ="rust1", since ="1.0.0")]412#[derive(Debug)]413pub structFromUtf16Error(());414415implString {416/// Creates a new empty `String`.417    ///418    /// Given that the `String` is empty, this will not allocate any initial419    /// buffer. While that means that this initial operation is very420    /// inexpensive, it may cause excessive allocation later when you add421    /// data. If you have an idea of how much data the `String` will hold,422    /// consider the [`with_capacity`] method to prevent excessive423    /// re-allocation.424    ///425    /// [`with_capacity`]: String::with_capacity426    ///427    /// # Examples428    ///429    /// ```430    /// let s = String::new();431    /// ```432#[inline]433    #[rustc_const_stable(feature ="const_string_new", since ="1.39.0")]434    #[rustc_diagnostic_item ="string_new"]435    #[stable(feature ="rust1", since ="1.0.0")]436    #[must_use]437pub const fnnew() -> String {438        String { vec: Vec::new() }439    }440441/// Creates a new empty `String` with at least the specified capacity.442    ///443    /// `String`s have an internal buffer to hold their data. The capacity is444    /// the length of that buffer, and can be queried with the [`capacity`]445    /// method. This method creates an empty `String`, but one with an initial446    /// buffer that can hold at least `capacity` bytes. This is useful when you447    /// may be appending a bunch of data to the `String`, reducing the number of448    /// reallocations it needs to do.449    ///450    /// [`capacity`]: String::capacity451    ///452    /// If the given capacity is `0`, no allocation will occur, and this method453    /// is identical to the [`new`] method.454    ///455    /// [`new`]: String::new456    ///457    /// # Examples458    ///459    /// ```460    /// let mut s = String::with_capacity(10);461    ///462    /// // The String contains no chars, even though it has capacity for more463    /// assert_eq!(s.len(), 0);464    ///465    /// // These are all done without reallocating...466    /// let cap = s.capacity();467    /// for _ in 0..10 {468    ///     s.push('a');469    /// }470    ///471    /// assert_eq!(s.capacity(), cap);472    ///473    /// // ...but this may make the string reallocate474    /// s.push('a');475    /// ```476#[cfg(not(no_global_oom_handling))]477    #[inline]478    #[stable(feature ="rust1", since ="1.0.0")]479    #[must_use]480pub fnwith_capacity(capacity: usize) -> String {481        String { vec: Vec::with_capacity(capacity) }482    }483484/// Creates a new empty `String` with at least the specified capacity.485    ///486    /// # Errors487    ///488    /// Returns [`Err`] if the capacity exceeds `isize::MAX` bytes,489    /// or if the memory allocator reports failure.490    ///491#[inline]492    #[unstable(feature ="try_with_capacity", issue ="91913")]493pub fntry_with_capacity(capacity: usize) ->Result<String, TryReserveError> {494Ok(String { vec: Vec::try_with_capacity(capacity)?})495    }496497/// Converts a vector of bytes to a `String`.498    ///499    /// A string ([`String`]) is made of bytes ([`u8`]), and a vector of bytes500    /// ([`Vec<u8>`]) is made of bytes, so this function converts between the501    /// two. Not all byte slices are valid `String`s, however: `String`502    /// requires that it is valid UTF-8. `from_utf8()` checks to ensure that503    /// the bytes are valid UTF-8, and then does the conversion.504    ///505    /// If you are sure that the byte slice is valid UTF-8, and you don't want506    /// to incur the overhead of the validity check, there is an unsafe version507    /// of this function, [`from_utf8_unchecked`], which has the same behavior508    /// but skips the check.509    ///510    /// This method will take care to not copy the vector, for efficiency's511    /// sake.512    ///513    /// If you need a [`&str`] instead of a `String`, consider514    /// [`str::from_utf8`].515    ///516    /// The inverse of this method is [`into_bytes`].517    ///518    /// # Errors519    ///520    /// Returns [`Err`] if the slice is not UTF-8 with a description as to why the521    /// provided bytes are not UTF-8. The vector you moved in is also included.522    ///523    /// # Examples524    ///525    /// Basic usage:526    ///527    /// ```528    /// // some bytes, in a vector529    /// let sparkle_heart = vec![240, 159, 146, 150];530    ///531    /// // We know these bytes are valid, so we'll use `unwrap()`.532    /// let sparkle_heart = String::from_utf8(sparkle_heart).unwrap();533    ///534    /// assert_eq!("💖", sparkle_heart);535    /// ```536    ///537    /// Incorrect bytes:538    ///539    /// ```540    /// // some invalid bytes, in a vector541    /// let sparkle_heart = vec![0, 159, 146, 150];542    ///543    /// assert!(String::from_utf8(sparkle_heart).is_err());544    /// ```545    ///546    /// See the docs for [`FromUtf8Error`] for more details on what you can do547    /// with this error.548    ///549    /// [`from_utf8_unchecked`]: String::from_utf8_unchecked550    /// [`Vec<u8>`]: crate::vec::Vec "Vec"551    /// [`&str`]: prim@str "&str"552    /// [`into_bytes`]: String::into_bytes553#[inline]554    #[stable(feature ="rust1", since ="1.0.0")]555    #[rustc_diagnostic_item ="string_from_utf8"]556pub fnfrom_utf8(vec: Vec<u8>) ->Result<String, FromUtf8Error> {557matchstr::from_utf8(&vec) {558Ok(..) =>Ok(String { vec }),559Err(e) =>Err(FromUtf8Error { bytes: vec, error: e }),560        }561    }562563/// Converts a slice of bytes to a string, including invalid characters.564    ///565    /// Strings are made of bytes ([`u8`]), and a slice of bytes566    /// ([`&[u8]`][byteslice]) is made of bytes, so this function converts567    /// between the two. Not all byte slices are valid strings, however: strings568    /// are required to be valid UTF-8. During this conversion,569    /// `from_utf8_lossy()` will replace any invalid UTF-8 sequences with570    /// [`U+FFFD REPLACEMENT CHARACTER`][U+FFFD], which looks like this: �571    ///572    /// [byteslice]: prim@slice573    /// [U+FFFD]: core::char::REPLACEMENT_CHARACTER574    ///575    /// If you are sure that the byte slice is valid UTF-8, and you don't want576    /// to incur the overhead of the conversion, there is an unsafe version577    /// of this function, [`from_utf8_unchecked`], which has the same behavior578    /// but skips the checks.579    ///580    /// [`from_utf8_unchecked`]: String::from_utf8_unchecked581    ///582    /// This function returns a [`Cow<'a, str>`]. If our byte slice is invalid583    /// UTF-8, then we need to insert the replacement characters, which will584    /// change the size of the string, and hence, require a `String`. But if585    /// it's already valid UTF-8, we don't need a new allocation. This return586    /// type allows us to handle both cases.587    ///588    /// [`Cow<'a, str>`]: crate::borrow::Cow "borrow::Cow"589    ///590    /// # Examples591    ///592    /// Basic usage:593    ///594    /// ```595    /// // some bytes, in a vector596    /// let sparkle_heart = vec![240, 159, 146, 150];597    ///598    /// let sparkle_heart = String::from_utf8_lossy(&sparkle_heart);599    ///600    /// assert_eq!("💖", sparkle_heart);601    /// ```602    ///603    /// Incorrect bytes:604    ///605    /// ```606    /// // some invalid bytes607    /// let input = b"Hello \xF0\x90\x80World";608    /// let output = String::from_utf8_lossy(input);609    ///610    /// assert_eq!("Hello �World", output);611    /// ```612#[must_use]613    #[cfg(not(no_global_oom_handling))]614    #[stable(feature ="rust1", since ="1.0.0")]615pub fnfrom_utf8_lossy(v:&[u8]) -> Cow<'_, str> {616letmutiter = v.utf8_chunks();617618letfirst_valid =if letSome(chunk) = iter.next() {619letvalid = chunk.valid();620ifchunk.invalid().is_empty() {621debug_assert_eq!(valid.len(), v.len());622returnCow::Borrowed(valid);623            }624            valid625        }else{626returnCow::Borrowed("");627        };628629constREPLACEMENT:&str ="\u{FFFD}";630631letmutres = String::with_capacity(v.len());632        res.push_str(first_valid);633        res.push_str(REPLACEMENT);634635forchunkiniter {636            res.push_str(chunk.valid());637if!chunk.invalid().is_empty() {638                res.push_str(REPLACEMENT);639            }640        }641642        Cow::Owned(res)643    }644645/// Converts a [`Vec<u8>`] to a `String`, substituting invalid UTF-8646    /// sequences with replacement characters.647    ///648    /// See [`from_utf8_lossy`] for more details.649    ///650    /// [`from_utf8_lossy`]: String::from_utf8_lossy651    ///652    /// Note that this function does not guarantee reuse of the original `Vec`653    /// allocation.654    ///655    /// # Examples656    ///657    /// Basic usage:658    ///659    /// ```660    /// #![feature(string_from_utf8_lossy_owned)]661    /// // some bytes, in a vector662    /// let sparkle_heart = vec![240, 159, 146, 150];663    ///664    /// let sparkle_heart = String::from_utf8_lossy_owned(sparkle_heart);665    ///666    /// assert_eq!(String::from("💖"), sparkle_heart);667    /// ```668    ///669    /// Incorrect bytes:670    ///671    /// ```672    /// #![feature(string_from_utf8_lossy_owned)]673    /// // some invalid bytes674    /// let input: Vec<u8> = b"Hello \xF0\x90\x80World".into();675    /// let output = String::from_utf8_lossy_owned(input);676    ///677    /// assert_eq!(String::from("Hello �World"), output);678    /// ```679#[must_use]680    #[cfg(not(no_global_oom_handling))]681    #[unstable(feature ="string_from_utf8_lossy_owned", issue ="129436")]682pub fnfrom_utf8_lossy_owned(v: Vec<u8>) -> String {683if letCow::Owned(string) = String::from_utf8_lossy(&v) {684            string685        }else{686// SAFETY: `String::from_utf8_lossy`'s contract ensures that if687            // it returns a `Cow::Borrowed`, it is a valid UTF-8 string.688            // Otherwise, it returns a new allocation of an owned `String`, with689            // replacement characters for invalid sequences, which is returned690            // above.691unsafe{ String::from_utf8_unchecked(v) }692        }693    }694695/// Decode a native endian UTF-16–encoded vector `v` into a `String`,696    /// returning [`Err`] if `v` contains any invalid data.697    ///698    /// # Examples699    ///700    /// ```701    /// // 𝄞music702    /// let v = &[0xD834, 0xDD1E, 0x006d, 0x0075,703    ///           0x0073, 0x0069, 0x0063];704    /// assert_eq!(String::from("𝄞music"),705    ///            String::from_utf16(v).unwrap());706    ///707    /// // 𝄞mu<invalid>ic708    /// let v = &[0xD834, 0xDD1E, 0x006d, 0x0075,709    ///           0xD800, 0x0069, 0x0063];710    /// assert!(String::from_utf16(v).is_err());711    /// ```712#[cfg(not(no_global_oom_handling))]713    #[stable(feature ="rust1", since ="1.0.0")]714pub fnfrom_utf16(v:&[u16]) ->Result<String, FromUtf16Error> {715// This isn't done via collect::<Result<_, _>>() for performance reasons.716        // FIXME: the function can be simplified again when #48994 is closed.717letmutret = String::with_capacity(v.len());718forcinchar::decode_utf16(v.iter().cloned()) {719if letOk(c) = c {720                ret.push(c);721            }else{722returnErr(FromUtf16Error(()));723            }724        }725Ok(ret)726    }727728/// Decode a native endian UTF-16–encoded slice `v` into a `String`,729    /// replacing invalid data with [the replacement character (`U+FFFD`)][U+FFFD].730    ///731    /// Unlike [`from_utf8_lossy`] which returns a [`Cow<'a, str>`],732    /// `from_utf16_lossy` returns a `String` since the UTF-16 to UTF-8733    /// conversion requires a memory allocation.734    ///735    /// [`from_utf8_lossy`]: String::from_utf8_lossy736    /// [`Cow<'a, str>`]: crate::borrow::Cow "borrow::Cow"737    /// [U+FFFD]: core::char::REPLACEMENT_CHARACTER738    ///739    /// # Examples740    ///741    /// ```742    /// // 𝄞mus<invalid>ic<invalid>743    /// let v = &[0xD834, 0xDD1E, 0x006d, 0x0075,744    ///           0x0073, 0xDD1E, 0x0069, 0x0063,745    ///           0xD834];746    ///747    /// assert_eq!(String::from("𝄞mus\u{FFFD}ic\u{FFFD}"),748    ///            String::from_utf16_lossy(v));749    /// ```750#[cfg(not(no_global_oom_handling))]751    #[must_use]752    #[inline]753    #[stable(feature ="rust1", since ="1.0.0")]754pub fnfrom_utf16_lossy(v:&[u16]) -> String {755        char::decode_utf16(v.iter().cloned())756            .map(|r| r.unwrap_or(char::REPLACEMENT_CHARACTER))757            .collect()758    }759760/// Decode a UTF-16LE–encoded vector `v` into a `String`,761    /// returning [`Err`] if `v` contains any invalid data.762    ///763    /// # Examples764    ///765    /// Basic usage:766    ///767    /// ```768    /// #![feature(str_from_utf16_endian)]769    /// // 𝄞music770    /// let v = &[0x34, 0xD8, 0x1E, 0xDD, 0x6d, 0x00, 0x75, 0x00,771    ///           0x73, 0x00, 0x69, 0x00, 0x63, 0x00];772    /// assert_eq!(String::from("𝄞music"),773    ///            String::from_utf16le(v).unwrap());774    ///775    /// // 𝄞mu<invalid>ic776    /// let v = &[0x34, 0xD8, 0x1E, 0xDD, 0x6d, 0x00, 0x75, 0x00,777    ///           0x00, 0xD8, 0x69, 0x00, 0x63, 0x00];778    /// assert!(String::from_utf16le(v).is_err());779    /// ```780#[cfg(not(no_global_oom_handling))]781    #[unstable(feature ="str_from_utf16_endian", issue ="116258")]782pub fnfrom_utf16le(v:&[u8]) ->Result<String, FromUtf16Error> {783let(chunks, []) = v.as_chunks::<2>()else{784returnErr(FromUtf16Error(()));785        };786match(cfg!(target_endian ="little"),unsafe{ v.align_to::<u16>() }) {787            (true, ([], v, [])) =>Self::from_utf16(v),788_=> char::decode_utf16(chunks.iter().copied().map(u16::from_le_bytes))789                .collect::<Result<_,_>>()790                .map_err(|_| FromUtf16Error(())),791        }792    }793794/// Decode a UTF-16LE–encoded slice `v` into a `String`, replacing795    /// invalid data with [the replacement character (`U+FFFD`)][U+FFFD].796    ///797    /// Unlike [`from_utf8_lossy`] which returns a [`Cow<'a, str>`],798    /// `from_utf16le_lossy` returns a `String` since the UTF-16 to UTF-8799    /// conversion requires a memory allocation.800    ///801    /// [`from_utf8_lossy`]: String::from_utf8_lossy802    /// [`Cow<'a, str>`]: crate::borrow::Cow "borrow::Cow"803    /// [U+FFFD]: core::char::REPLACEMENT_CHARACTER804    ///805    /// # Examples806    ///807    /// Basic usage:808    ///809    /// ```810    /// #![feature(str_from_utf16_endian)]811    /// // 𝄞mus<invalid>ic<invalid>812    /// let v = &[0x34, 0xD8, 0x1E, 0xDD, 0x6d, 0x00, 0x75, 0x00,813    ///           0x73, 0x00, 0x1E, 0xDD, 0x69, 0x00, 0x63, 0x00,814    ///           0x34, 0xD8];815    ///816    /// assert_eq!(String::from("𝄞mus\u{FFFD}ic\u{FFFD}"),817    ///            String::from_utf16le_lossy(v));818    /// ```819#[cfg(not(no_global_oom_handling))]820    #[unstable(feature ="str_from_utf16_endian", issue ="116258")]821pub fnfrom_utf16le_lossy(v:&[u8]) -> String {822match(cfg!(target_endian ="little"),unsafe{ v.align_to::<u16>() }) {823            (true, ([], v, [])) =>Self::from_utf16_lossy(v),824            (true, ([], v, [_remainder])) =>Self::from_utf16_lossy(v) +"\u{FFFD}",825_=> {826let(chunks, remainder) = v.as_chunks::<2>();827letstring = char::decode_utf16(chunks.iter().copied().map(u16::from_le_bytes))828                    .map(|r| r.unwrap_or(char::REPLACEMENT_CHARACTER))829                    .collect();830ifremainder.is_empty() { string }else{ string +"\u{FFFD}"}831            }832        }833    }834835/// Decode a UTF-16BE–encoded vector `v` into a `String`,836    /// returning [`Err`] if `v` contains any invalid data.837    ///838    /// # Examples839    ///840    /// Basic usage:841    ///842    /// ```843    /// #![feature(str_from_utf16_endian)]844    /// // 𝄞music845    /// let v = &[0xD8, 0x34, 0xDD, 0x1E, 0x00, 0x6d, 0x00, 0x75,846    ///           0x00, 0x73, 0x00, 0x69, 0x00, 0x63];847    /// assert_eq!(String::from("𝄞music"),848    ///            String::from_utf16be(v).unwrap());849    ///850    /// // 𝄞mu<invalid>ic851    /// let v = &[0xD8, 0x34, 0xDD, 0x1E, 0x00, 0x6d, 0x00, 0x75,852    ///           0xD8, 0x00, 0x00, 0x69, 0x00, 0x63];853    /// assert!(String::from_utf16be(v).is_err());854    /// ```855#[cfg(not(no_global_oom_handling))]856    #[unstable(feature ="str_from_utf16_endian", issue ="116258")]857pub fnfrom_utf16be(v:&[u8]) ->Result<String, FromUtf16Error> {858let(chunks, []) = v.as_chunks::<2>()else{859returnErr(FromUtf16Error(()));860        };861match(cfg!(target_endian ="big"),unsafe{ v.align_to::<u16>() }) {862            (true, ([], v, [])) =>Self::from_utf16(v),863_=> char::decode_utf16(chunks.iter().copied().map(u16::from_be_bytes))864                .collect::<Result<_,_>>()865                .map_err(|_| FromUtf16Error(())),866        }867    }868869/// Decode a UTF-16BE–encoded slice `v` into a `String`, replacing870    /// invalid data with [the replacement character (`U+FFFD`)][U+FFFD].871    ///872    /// Unlike [`from_utf8_lossy`] which returns a [`Cow<'a, str>`],873    /// `from_utf16le_lossy` returns a `String` since the UTF-16 to UTF-8874    /// conversion requires a memory allocation.875    ///876    /// [`from_utf8_lossy`]: String::from_utf8_lossy877    /// [`Cow<'a, str>`]: crate::borrow::Cow "borrow::Cow"878    /// [U+FFFD]: core::char::REPLACEMENT_CHARACTER879    ///880    /// # Examples881    ///882    /// Basic usage:883    ///884    /// ```885    /// #![feature(str_from_utf16_endian)]886    /// // 𝄞mus<invalid>ic<invalid>887    /// let v = &[0xD8, 0x34, 0xDD, 0x1E, 0x00, 0x6d, 0x00, 0x75,888    ///           0x00, 0x73, 0xDD, 0x1E, 0x00, 0x69, 0x00, 0x63,889    ///           0xD8, 0x34];890    ///891    /// assert_eq!(String::from("𝄞mus\u{FFFD}ic\u{FFFD}"),892    ///            String::from_utf16be_lossy(v));893    /// ```894#[cfg(not(no_global_oom_handling))]895    #[unstable(feature ="str_from_utf16_endian", issue ="116258")]896pub fnfrom_utf16be_lossy(v:&[u8]) -> String {897match(cfg!(target_endian ="big"),unsafe{ v.align_to::<u16>() }) {898            (true, ([], v, [])) =>Self::from_utf16_lossy(v),899            (true, ([], v, [_remainder])) =>Self::from_utf16_lossy(v) +"\u{FFFD}",900_=> {901let(chunks, remainder) = v.as_chunks::<2>();902letstring = char::decode_utf16(chunks.iter().copied().map(u16::from_be_bytes))903                    .map(|r| r.unwrap_or(char::REPLACEMENT_CHARACTER))904                    .collect();905ifremainder.is_empty() { string }else{ string +"\u{FFFD}"}906            }907        }908    }909910/// Decomposes a `String` into its raw components: `(pointer, length, capacity)`.911    ///912    /// Returns the raw pointer to the underlying data, the length of913    /// the string (in bytes), and the allocated capacity of the data914    /// (in bytes). These are the same arguments in the same order as915    /// the arguments to [`from_raw_parts`].916    ///917    /// After calling this function, the caller is responsible for the918    /// memory previously managed by the `String`. The only way to do919    /// this is to convert the raw pointer, length, and capacity back920    /// into a `String` with the [`from_raw_parts`] function, allowing921    /// the destructor to perform the cleanup.922    ///923    /// [`from_raw_parts`]: String::from_raw_parts924    ///925    /// # Examples926    ///927    /// ```928    /// let s = String::from("hello");929    ///930    /// let (ptr, len, cap) = s.into_raw_parts();931    ///932    /// let rebuilt = unsafe { String::from_raw_parts(ptr, len, cap) };933    /// assert_eq!(rebuilt, "hello");934    /// ```935#[must_use ="losing the pointer will leak memory"]936    #[stable(feature ="vec_into_raw_parts", since ="CURRENT_RUSTC_VERSION")]937pub fninto_raw_parts(self) -> (*mutu8, usize, usize) {938self.vec.into_raw_parts()939    }940941/// Creates a new `String` from a pointer, a length and a capacity.942    ///943    /// # Safety944    ///945    /// This is highly unsafe, due to the number of invariants that aren't946    /// checked:947    ///948    /// * all safety requirements for [`Vec::<u8>::from_raw_parts`].949    /// * all safety requirements for [`String::from_utf8_unchecked`].950    ///951    /// Violating these may cause problems like corrupting the allocator's952    /// internal data structures. For example, it is normally **not** safe to953    /// build a `String` from a pointer to a C `char` array containing UTF-8954    /// _unless_ you are certain that array was originally allocated by the955    /// Rust standard library's allocator.956    ///957    /// The ownership of `buf` is effectively transferred to the958    /// `String` which may then deallocate, reallocate or change the959    /// contents of memory pointed to by the pointer at will. Ensure960    /// that nothing else uses the pointer after calling this961    /// function.962    ///963    /// # Examples964    ///965    /// ```966    /// unsafe {967    ///     let s = String::from("hello");968    ///969    ///     // Deconstruct the String into parts.970    ///     let (ptr, len, capacity) = s.into_raw_parts();971    ///972    ///     let s = String::from_raw_parts(ptr, len, capacity);973    ///974    ///     assert_eq!(String::from("hello"), s);975    /// }976    /// ```977#[inline]978    #[stable(feature ="rust1", since ="1.0.0")]979pub unsafe fnfrom_raw_parts(buf:*mutu8, length: usize, capacity: usize) -> String {980unsafe{ String { vec: Vec::from_raw_parts(buf, length, capacity) } }981    }982983/// Converts a vector of bytes to a `String` without checking that the984    /// string contains valid UTF-8.985    ///986    /// See the safe version, [`from_utf8`], for more details.987    ///988    /// [`from_utf8`]: String::from_utf8989    ///990    /// # Safety991    ///992    /// This function is unsafe because it does not check that the bytes passed993    /// to it are valid UTF-8. If this constraint is violated, it may cause994    /// memory unsafety issues with future users of the `String`, as the rest of995    /// the standard library assumes that `String`s are valid UTF-8.996    ///997    /// # Examples998    ///999    /// ```1000    /// // some bytes, in a vector1001    /// let sparkle_heart = vec![240, 159, 146, 150];1002    ///1003    /// let sparkle_heart = unsafe {1004    ///     String::from_utf8_unchecked(sparkle_heart)1005    /// };1006    ///1007    /// assert_eq!("💖", sparkle_heart);1008    /// ```1009#[inline]1010    #[must_use]1011    #[stable(feature ="rust1", since ="1.0.0")]1012pub unsafe fnfrom_utf8_unchecked(bytes: Vec<u8>) -> String {1013        String { vec: bytes }1014    }10151016/// Converts a `String` into a byte vector.1017    ///1018    /// This consumes the `String`, so we do not need to copy its contents.1019    ///1020    /// # Examples1021    ///1022    /// ```1023    /// let s = String::from("hello");1024    /// let bytes = s.into_bytes();1025    ///1026    /// assert_eq!(&[104, 101, 108, 108, 111][..], &bytes[..]);1027    /// ```1028#[inline]1029    #[must_use ="`self` will be dropped if the result is not used"]1030    #[stable(feature ="rust1", since ="1.0.0")]1031    #[rustc_const_stable(feature ="const_vec_string_slice", since ="1.87.0")]1032    #[rustc_allow_const_fn_unstable(const_precise_live_drops)]1033pub const fninto_bytes(self) -> Vec<u8> {1034self.vec1035    }10361037/// Extracts a string slice containing the entire `String`.1038    ///1039    /// # Examples1040    ///1041    /// ```1042    /// let s = String::from("foo");1043    ///1044    /// assert_eq!("foo", s.as_str());1045    /// ```1046#[inline]1047    #[must_use]1048    #[stable(feature ="string_as_str", since ="1.7.0")]1049    #[rustc_diagnostic_item ="string_as_str"]1050    #[rustc_const_stable(feature ="const_vec_string_slice", since ="1.87.0")]1051pub const fnas_str(&self) ->&str {1052// SAFETY: String contents are stipulated to be valid UTF-8, invalid contents are an error1053        // at construction.1054unsafe{ str::from_utf8_unchecked(self.vec.as_slice()) }1055    }10561057/// Converts a `String` into a mutable string slice.1058    ///1059    /// # Examples1060    ///1061    /// ```1062    /// let mut s = String::from("foobar");1063    /// let s_mut_str = s.as_mut_str();1064    ///1065    /// s_mut_str.make_ascii_uppercase();1066    ///1067    /// assert_eq!("FOOBAR", s_mut_str);1068    /// ```1069#[inline]1070    #[must_use]1071    #[stable(feature ="string_as_str", since ="1.7.0")]1072    #[rustc_diagnostic_item ="string_as_mut_str"]1073    #[rustc_const_stable(feature ="const_vec_string_slice", since ="1.87.0")]1074pub const fnas_mut_str(&mutself) ->&mutstr {1075// SAFETY: String contents are stipulated to be valid UTF-8, invalid contents are an error1076        // at construction.1077unsafe{ str::from_utf8_unchecked_mut(self.vec.as_mut_slice()) }1078    }10791080/// Appends a given string slice onto the end of this `String`.1081    ///1082    /// # Examples1083    ///1084    /// ```1085    /// let mut s = String::from("foo");1086    ///1087    /// s.push_str("bar");1088    ///1089    /// assert_eq!("foobar", s);1090    /// ```1091#[cfg(not(no_global_oom_handling))]1092    #[inline]1093    #[stable(feature ="rust1", since ="1.0.0")]1094    #[rustc_confusables("append","push")]1095    #[rustc_diagnostic_item ="string_push_str"]1096pub fnpush_str(&mutself, string:&str) {1097self.vec.extend_from_slice(string.as_bytes())1098    }10991100/// Copies elements from `src` range to the end of the string.1101    ///1102    /// # Panics1103    ///1104    /// Panics if the range has `start_bound > end_bound`, or, if the range is1105    /// bounded on either end and does not lie on a [`char`] boundary.1106    ///1107    /// # Examples1108    ///1109    /// ```1110    /// let mut string = String::from("abcde");1111    ///1112    /// string.extend_from_within(2..);1113    /// assert_eq!(string, "abcdecde");1114    ///1115    /// string.extend_from_within(..2);1116    /// assert_eq!(string, "abcdecdeab");1117    ///1118    /// string.extend_from_within(4..8);1119    /// assert_eq!(string, "abcdecdeabecde");1120    /// ```1121#[cfg(not(no_global_oom_handling))]1122    #[stable(feature ="string_extend_from_within", since ="1.87.0")]1123    #[track_caller]1124pub fnextend_from_within<R>(&mutself, src: R)1125where1126R: RangeBounds<usize>,1127    {1128letsrc @ Range { start, end } = slice::range(src, ..self.len());11291130assert!(self.is_char_boundary(start));1131assert!(self.is_char_boundary(end));11321133self.vec.extend_from_within(src);1134    }11351136/// Returns this `String`'s capacity, in bytes.1137    ///1138    /// # Examples1139    ///1140    /// ```1141    /// let s = String::with_capacity(10);1142    ///1143    /// assert!(s.capacity() >= 10);1144    /// ```1145#[inline]1146    #[must_use]1147    #[stable(feature ="rust1", since ="1.0.0")]1148    #[rustc_const_stable(feature ="const_vec_string_slice", since ="1.87.0")]1149pub const fncapacity(&self) -> usize {1150self.vec.capacity()1151    }11521153/// Reserves capacity for at least `additional` bytes more than the1154    /// current length. The allocator may reserve more space to speculatively1155    /// avoid frequent allocations. After calling `reserve`,1156    /// capacity will be greater than or equal to `self.len() + additional`.1157    /// Does nothing if capacity is already sufficient.1158    ///1159    /// # Panics1160    ///1161    /// Panics if the new capacity overflows [`usize`].1162    ///1163    /// # Examples1164    ///1165    /// Basic usage:1166    ///1167    /// ```1168    /// let mut s = String::new();1169    ///1170    /// s.reserve(10);1171    ///1172    /// assert!(s.capacity() >= 10);1173    /// ```1174    ///1175    /// This might not actually increase the capacity:1176    ///1177    /// ```1178    /// let mut s = String::with_capacity(10);1179    /// s.push('a');1180    /// s.push('b');1181    ///1182    /// // s now has a length of 2 and a capacity of at least 101183    /// let capacity = s.capacity();1184    /// assert_eq!(2, s.len());1185    /// assert!(capacity >= 10);1186    ///1187    /// // Since we already have at least an extra 8 capacity, calling this...1188    /// s.reserve(8);1189    ///1190    /// // ... doesn't actually increase.1191    /// assert_eq!(capacity, s.capacity());1192    /// ```1193#[cfg(not(no_global_oom_handling))]1194    #[inline]1195    #[stable(feature ="rust1", since ="1.0.0")]1196pub fnreserve(&mutself, additional: usize) {1197self.vec.reserve(additional)1198    }11991200/// Reserves the minimum capacity for at least `additional` bytes more than1201    /// the current length. Unlike [`reserve`], this will not1202    /// deliberately over-allocate to speculatively avoid frequent allocations.1203    /// After calling `reserve_exact`, capacity will be greater than or equal to1204    /// `self.len() + additional`. Does nothing if the capacity is already1205    /// sufficient.1206    ///1207    /// [`reserve`]: String::reserve1208    ///1209    /// # Panics1210    ///1211    /// Panics if the new capacity overflows [`usize`].1212    ///1213    /// # Examples1214    ///1215    /// Basic usage:1216    ///1217    /// ```1218    /// let mut s = String::new();1219    ///1220    /// s.reserve_exact(10);1221    ///1222    /// assert!(s.capacity() >= 10);1223    /// ```1224    ///1225    /// This might not actually increase the capacity:1226    ///1227    /// ```1228    /// let mut s = String::with_capacity(10);1229    /// s.push('a');1230    /// s.push('b');1231    ///1232    /// // s now has a length of 2 and a capacity of at least 101233    /// let capacity = s.capacity();1234    /// assert_eq!(2, s.len());1235    /// assert!(capacity >= 10);1236    ///1237    /// // Since we already have at least an extra 8 capacity, calling this...1238    /// s.reserve_exact(8);1239    ///1240    /// // ... doesn't actually increase.1241    /// assert_eq!(capacity, s.capacity());1242    /// ```1243#[cfg(not(no_global_oom_handling))]1244    #[inline]1245    #[stable(feature ="rust1", since ="1.0.0")]1246pub fnreserve_exact(&mutself, additional: usize) {1247self.vec.reserve_exact(additional)1248    }12491250/// Tries to reserve capacity for at least `additional` bytes more than the1251    /// current length. The allocator may reserve more space to speculatively1252    /// avoid frequent allocations. After calling `try_reserve`, capacity will be1253    /// greater than or equal to `self.len() + additional` if it returns1254    /// `Ok(())`. Does nothing if capacity is already sufficient. This method1255    /// preserves the contents even if an error occurs.1256    ///1257    /// # Errors1258    ///1259    /// If the capacity overflows, or the allocator reports a failure, then an error1260    /// is returned.1261    ///1262    /// # Examples1263    ///1264    /// ```1265    /// use std::collections::TryReserveError;1266    ///1267    /// fn process_data(data: &str) -> Result<String, TryReserveError> {1268    ///     let mut output = String::new();1269    ///1270    ///     // Pre-reserve the memory, exiting if we can't1271    ///     output.try_reserve(data.len())?;1272    ///1273    ///     // Now we know this can't OOM in the middle of our complex work1274    ///     output.push_str(data);1275    ///1276    ///     Ok(output)1277    /// }1278    /// # process_data("rust").expect("why is the test harness OOMing on 4 bytes?");1279    /// ```1280#[stable(feature ="try_reserve", since ="1.57.0")]1281pub fntry_reserve(&mutself, additional: usize) ->Result<(), TryReserveError> {1282self.vec.try_reserve(additional)1283    }12841285/// Tries to reserve the minimum capacity for at least `additional` bytes1286    /// more than the current length. Unlike [`try_reserve`], this will not1287    /// deliberately over-allocate to speculatively avoid frequent allocations.1288    /// After calling `try_reserve_exact`, capacity will be greater than or1289    /// equal to `self.len() + additional` if it returns `Ok(())`.1290    /// Does nothing if the capacity is already sufficient.1291    ///1292    /// Note that the allocator may give the collection more space than it1293    /// requests. Therefore, capacity can not be relied upon to be precisely1294    /// minimal. Prefer [`try_reserve`] if future insertions are expected.1295    ///1296    /// [`try_reserve`]: String::try_reserve1297    ///1298    /// # Errors1299    ///1300    /// If the capacity overflows, or the allocator reports a failure, then an error1301    /// is returned.1302    ///1303    /// # Examples1304    ///1305    /// ```1306    /// use std::collections::TryReserveError;1307    ///1308    /// fn process_data(data: &str) -> Result<String, TryReserveError> {1309    ///     let mut output = String::new();1310    ///1311    ///     // Pre-reserve the memory, exiting if we can't1312    ///     output.try_reserve_exact(data.len())?;1313    ///1314    ///     // Now we know this can't OOM in the middle of our complex work1315    ///     output.push_str(data);1316    ///1317    ///     Ok(output)1318    /// }1319    /// # process_data("rust").expect("why is the test harness OOMing on 4 bytes?");1320    /// ```1321#[stable(feature ="try_reserve", since ="1.57.0")]1322pub fntry_reserve_exact(&mutself, additional: usize) ->Result<(), TryReserveError> {1323self.vec.try_reserve_exact(additional)1324    }13251326/// Shrinks the capacity of this `String` to match its length.1327    ///1328    /// # Examples1329    ///1330    /// ```1331    /// let mut s = String::from("foo");1332    ///1333    /// s.reserve(100);1334    /// assert!(s.capacity() >= 100);1335    ///1336    /// s.shrink_to_fit();1337    /// assert_eq!(3, s.capacity());1338    /// ```1339#[cfg(not(no_global_oom_handling))]1340    #[inline]1341    #[stable(feature ="rust1", since ="1.0.0")]1342pub fnshrink_to_fit(&mutself) {1343self.vec.shrink_to_fit()1344    }13451346/// Shrinks the capacity of this `String` with a lower bound.1347    ///1348    /// The capacity will remain at least as large as both the length1349    /// and the supplied value.1350    ///1351    /// If the current capacity is less than the lower limit, this is a no-op.1352    ///1353    /// # Examples1354    ///1355    /// ```1356    /// let mut s = String::from("foo");1357    ///1358    /// s.reserve(100);1359    /// assert!(s.capacity() >= 100);1360    ///1361    /// s.shrink_to(10);1362    /// assert!(s.capacity() >= 10);1363    /// s.shrink_to(0);1364    /// assert!(s.capacity() >= 3);1365    /// ```1366#[cfg(not(no_global_oom_handling))]1367    #[inline]1368    #[stable(feature ="shrink_to", since ="1.56.0")]1369pub fnshrink_to(&mutself, min_capacity: usize) {1370self.vec.shrink_to(min_capacity)1371    }13721373/// Appends the given [`char`] to the end of this `String`.1374    ///1375    /// # Examples1376    ///1377    /// ```1378    /// let mut s = String::from("abc");1379    ///1380    /// s.push('1');1381    /// s.push('2');1382    /// s.push('3');1383    ///1384    /// assert_eq!("abc123", s);1385    /// ```1386#[cfg(not(no_global_oom_handling))]1387    #[inline]1388    #[stable(feature ="rust1", since ="1.0.0")]1389pub fnpush(&mutself, ch: char) {1390letlen =self.len();1391letch_len = ch.len_utf8();1392self.reserve(ch_len);13931394// SAFETY: Just reserved capacity for at least the length needed to encode `ch`.1395unsafe{1396            core::char::encode_utf8_raw_unchecked(chasu32,self.vec.as_mut_ptr().add(self.len()));1397self.vec.set_len(len + ch_len);1398        }1399    }14001401/// Returns a byte slice of this `String`'s contents.1402    ///1403    /// The inverse of this method is [`from_utf8`].1404    ///1405    /// [`from_utf8`]: String::from_utf81406    ///1407    /// # Examples1408    ///1409    /// ```1410    /// let s = String::from("hello");1411    ///1412    /// assert_eq!(&[104, 101, 108, 108, 111], s.as_bytes());1413    /// ```1414#[inline]1415    #[must_use]1416    #[stable(feature ="rust1", since ="1.0.0")]1417    #[rustc_const_stable(feature ="const_vec_string_slice", since ="1.87.0")]1418pub const fnas_bytes(&self) ->&[u8] {1419self.vec.as_slice()1420    }14211422/// Shortens this `String` to the specified length.1423    ///1424    /// If `new_len` is greater than or equal to the string's current length, this has no1425    /// effect.1426    ///1427    /// Note that this method has no effect on the allocated capacity1428    /// of the string1429    ///1430    /// # Panics1431    ///1432    /// Panics if `new_len` does not lie on a [`char`] boundary.1433    ///1434    /// # Examples1435    ///1436    /// ```1437    /// let mut s = String::from("hello");1438    ///1439    /// s.truncate(2);1440    ///1441    /// assert_eq!("he", s);1442    /// ```1443#[inline]1444    #[stable(feature ="rust1", since ="1.0.0")]1445    #[track_caller]1446pub fntruncate(&mutself, new_len: usize) {1447ifnew_len <=self.len() {1448assert!(self.is_char_boundary(new_len));1449self.vec.truncate(new_len)1450        }1451    }14521453/// Removes the last character from the string buffer and returns it.1454    ///1455    /// Returns [`None`] if this `String` is empty.1456    ///1457    /// # Examples1458    ///1459    /// ```1460    /// let mut s = String::from("abč");1461    ///1462    /// assert_eq!(s.pop(), Some('č'));1463    /// assert_eq!(s.pop(), Some('b'));1464    /// assert_eq!(s.pop(), Some('a'));1465    ///1466    /// assert_eq!(s.pop(), None);1467    /// ```1468#[inline]1469    #[stable(feature ="rust1", since ="1.0.0")]1470pub fnpop(&mutself) ->Option<char> {1471letch =self.chars().rev().next()?;1472letnewlen =self.len() - ch.len_utf8();1473unsafe{1474self.vec.set_len(newlen);1475        }1476Some(ch)1477    }14781479/// Removes a [`char`] from this `String` at byte position `idx` and returns it.1480    ///1481    /// Copies all bytes after the removed char to new positions.1482    ///1483    /// Note that calling this in a loop can result in quadratic behavior.1484    ///1485    /// # Panics1486    ///1487    /// Panics if `idx` is larger than or equal to the `String`'s length,1488    /// or if it does not lie on a [`char`] boundary.1489    ///1490    /// # Examples1491    ///1492    /// ```1493    /// let mut s = String::from("abç");1494    ///1495    /// assert_eq!(s.remove(0), 'a');1496    /// assert_eq!(s.remove(1), 'ç');1497    /// assert_eq!(s.remove(0), 'b');1498    /// ```1499#[inline]1500    #[stable(feature ="rust1", since ="1.0.0")]1501    #[track_caller]1502    #[rustc_confusables("delete","take")]1503pub fnremove(&mutself, idx: usize) -> char {1504letch =matchself[idx..].chars().next() {1505Some(ch) => ch,1506None=>panic!("cannot remove a char from the end of a string"),1507        };15081509letnext = idx + ch.len_utf8();1510letlen =self.len();1511unsafe{1512            ptr::copy(self.vec.as_ptr().add(next),self.vec.as_mut_ptr().add(idx), len - next);1513self.vec.set_len(len - (next - idx));1514        }1515        ch1516    }15171518/// Remove all matches of pattern `pat` in the `String`.1519    ///1520    /// # Examples1521    ///1522    /// ```1523    /// #![feature(string_remove_matches)]1524    /// let mut s = String::from("Trees are not green, the sky is not blue.");1525    /// s.remove_matches("not ");1526    /// assert_eq!("Trees are green, the sky is blue.", s);1527    /// ```1528    ///1529    /// Matches will be detected and removed iteratively, so in cases where1530    /// patterns overlap, only the first pattern will be removed:1531    ///1532    /// ```1533    /// #![feature(string_remove_matches)]1534    /// let mut s = String::from("banana");1535    /// s.remove_matches("ana");1536    /// assert_eq!("bna", s);1537    /// ```1538#[cfg(not(no_global_oom_handling))]1539    #[unstable(feature ="string_remove_matches", reason ="new API", issue ="72826")]1540pub fnremove_matches<P: Pattern>(&mutself, pat: P) {1541usecore::str::pattern::Searcher;15421543letrejections = {1544letmutsearcher = pat.into_searcher(self);1545// Per Searcher::next:1546            //1547            // A Match result needs to contain the whole matched pattern,1548            // however Reject results may be split up into arbitrary many1549            // adjacent fragments. Both ranges may have zero length.1550            //1551            // In practice the implementation of Searcher::next_match tends to1552            // be more efficient, so we use it here and do some work to invert1553            // matches into rejections since that's what we want to copy below.1554letmutfront =0;1555letrejections: Vec<_> = from_fn(|| {1556let(start, end) = searcher.next_match()?;1557letprev_front = front;1558                front = end;1559Some((prev_front, start))1560            })1561            .collect();1562            rejections.into_iter().chain(core::iter::once((front,self.len())))1563        };15641565letmutlen =0;1566letptr =self.vec.as_mut_ptr();15671568for(start, end)inrejections {1569letcount = end - start;1570ifstart != len {1571// SAFETY: per Searcher::next:1572                //1573                // The stream of Match and Reject values up to a Done will1574                // contain index ranges that are adjacent, non-overlapping,1575                // covering the whole haystack, and laying on utf81576                // boundaries.1577unsafe{1578                    ptr::copy(ptr.add(start), ptr.add(len), count);1579                }1580            }1581            len += count;1582        }15831584unsafe{1585self.vec.set_len(len);1586        }1587    }15881589/// Retains only the characters specified by the predicate.1590    ///1591    /// In other words, remove all characters `c` such that `f(c)` returns `false`.1592    /// This method operates in place, visiting each character exactly once in the1593    /// original order, and preserves the order of the retained characters.1594    ///1595    /// # Examples1596    ///1597    /// ```1598    /// let mut s = String::from("f_o_ob_ar");1599    ///1600    /// s.retain(|c| c != '_');1601    ///1602    /// assert_eq!(s, "foobar");1603    /// ```1604    ///1605    /// Because the elements are visited exactly once in the original order,1606    /// external state may be used to decide which elements to keep.1607    ///1608    /// ```1609    /// let mut s = String::from("abcde");1610    /// let keep = [false, true, true, false, true];1611    /// let mut iter = keep.iter();1612    /// s.retain(|_| *iter.next().unwrap());1613    /// assert_eq!(s, "bce");1614    /// ```1615#[inline]1616    #[stable(feature ="string_retain", since ="1.26.0")]1617pub fnretain<F>(&mutself,mutf: F)1618where1619F: FnMut(char) -> bool,1620    {1621structSetLenOnDrop<'a> {1622            s:&'amutString,1623            idx: usize,1624            del_bytes: usize,1625        }16261627impl<'a> DropforSetLenOnDrop<'a> {1628fndrop(&mutself) {1629letnew_len =self.idx -self.del_bytes;1630debug_assert!(new_len <=self.s.len());1631unsafe{self.s.vec.set_len(new_len) };1632            }1633        }16341635letlen =self.len();1636letmutguard = SetLenOnDrop { s:self, idx:0, del_bytes:0};16371638whileguard.idx < len {1639letch =1640// SAFETY: `guard.idx` is positive-or-zero and less that len so the `get_unchecked`1641                // is in bound. `self` is valid UTF-8 like string and the returned slice starts at1642                // a unicode code point so the `Chars` always return one character.1643unsafe{ guard.s.get_unchecked(guard.idx..len).chars().next().unwrap_unchecked() };1644letch_len = ch.len_utf8();16451646if!f(ch) {1647                guard.del_bytes += ch_len;1648            }else ifguard.del_bytes >0{1649// SAFETY: `guard.idx` is in bound and `guard.del_bytes` represent the number of1650                // bytes that are erased from the string so the resulting `guard.idx -1651                // guard.del_bytes` always represent a valid unicode code point.1652                //1653                // `guard.del_bytes` >= `ch.len_utf8()`, so taking a slice with `ch.len_utf8()` len1654                // is safe.1655ch.encode_utf8(unsafe{1656crate::slice::from_raw_parts_mut(1657                        guard.s.as_mut_ptr().add(guard.idx - guard.del_bytes),1658                        ch.len_utf8(),1659                    )1660                });1661            }16621663// Point idx to the next char1664guard.idx += ch_len;1665        }16661667        drop(guard);1668    }16691670/// Inserts a character into this `String` at byte position `idx`.1671    ///1672    /// Reallocates if `self.capacity()` is insufficient, which may involve copying all1673    /// `self.capacity()` bytes. Makes space for the insertion by copying all bytes of1674    /// `&self[idx..]` to new positions.1675    ///1676    /// Note that calling this in a loop can result in quadratic behavior.1677    ///1678    /// # Panics1679    ///1680    /// Panics if `idx` is larger than the `String`'s length, or if it does not1681    /// lie on a [`char`] boundary.1682    ///1683    /// # Examples1684    ///1685    /// ```1686    /// let mut s = String::with_capacity(3);1687    ///1688    /// s.insert(0, 'f');1689    /// s.insert(1, 'o');1690    /// s.insert(2, 'o');1691    ///1692    /// assert_eq!("foo", s);1693    /// ```1694#[cfg(not(no_global_oom_handling))]1695    #[inline]1696    #[track_caller]1697    #[stable(feature ="rust1", since ="1.0.0")]1698    #[rustc_confusables("set")]1699pub fninsert(&mutself, idx: usize, ch: char) {1700assert!(self.is_char_boundary(idx));17011702letlen =self.len();1703letch_len = ch.len_utf8();1704self.reserve(ch_len);17051706// SAFETY: Move the bytes starting from `idx` to their new location `ch_len`1707        // bytes ahead. This is safe because sufficient capacity was reserved, and `idx`1708        // is a char boundary.1709unsafe{1710            ptr::copy(1711self.vec.as_ptr().add(idx),1712self.vec.as_mut_ptr().add(idx + ch_len),1713                len - idx,1714            );1715        }17161717// SAFETY: Encode the character into the vacated region if `idx != len`,1718        // or into the uninitialized spare capacity otherwise.1719unsafe{1720            core::char::encode_utf8_raw_unchecked(chasu32,self.vec.as_mut_ptr().add(idx));1721        }17221723// SAFETY: Update the length to include the newly added bytes.1724unsafe{1725self.vec.set_len(len + ch_len);1726        }1727    }17281729/// Inserts a string slice into this `String` at byte position `idx`.1730    ///1731    /// Reallocates if `self.capacity()` is insufficient, which may involve copying all1732    /// `self.capacity()` bytes. Makes space for the insertion by copying all bytes of1733    /// `&self[idx..]` to new positions.1734    ///1735    /// Note that calling this in a loop can result in quadratic behavior.1736    ///1737    /// # Panics1738    ///1739    /// Panics if `idx` is larger than the `String`'s length, or if it does not1740    /// lie on a [`char`] boundary.1741    ///1742    /// # Examples1743    ///1744    /// ```1745    /// let mut s = String::from("bar");1746    ///1747    /// s.insert_str(0, "foo");1748    ///1749    /// assert_eq!("foobar", s);1750    /// ```1751#[cfg(not(no_global_oom_handling))]1752    #[inline]1753    #[track_caller]1754    #[stable(feature ="insert_str", since ="1.16.0")]1755    #[rustc_diagnostic_item ="string_insert_str"]1756pub fninsert_str(&mutself, idx: usize, string:&str) {1757assert!(self.is_char_boundary(idx));17581759letlen =self.len();1760letamt = string.len();1761self.reserve(amt);17621763// SAFETY: Move the bytes starting from `idx` to their new location `amt` bytes1764        // ahead. This is safe because sufficient capacity was just reserved, and `idx`1765        // is a char boundary.1766unsafe{1767            ptr::copy(self.vec.as_ptr().add(idx),self.vec.as_mut_ptr().add(idx + amt), len - idx);1768        }17691770// SAFETY: Copy the new string slice into the vacated region if `idx != len`,1771        // or into the uninitialized spare capacity otherwise. The borrow checker1772        // ensures that the source and destination do not overlap.1773unsafe{1774            ptr::copy_nonoverlapping(string.as_ptr(),self.vec.as_mut_ptr().add(idx), amt);1775        }17761777// SAFETY: Update the length to include the newly added bytes.1778unsafe{1779self.vec.set_len(len + amt);1780        }1781    }17821783/// Returns a mutable reference to the contents of this `String`.1784    ///1785    /// # Safety1786    ///1787    /// This function is unsafe because the returned `&mut Vec` allows writing1788    /// bytes which are not valid UTF-8. If this constraint is violated, using1789    /// the original `String` after dropping the `&mut Vec` may violate memory1790    /// safety, as the rest of the standard library assumes that `String`s are1791    /// valid UTF-8.1792    ///1793    /// # Examples1794    ///1795    /// ```1796    /// let mut s = String::from("hello");1797    ///1798    /// unsafe {1799    ///     let vec = s.as_mut_vec();1800    ///     assert_eq!(&[104, 101, 108, 108, 111][..], &vec[..]);1801    ///1802    ///     vec.reverse();1803    /// }1804    /// assert_eq!(s, "olleh");1805    /// ```1806#[inline]1807    #[stable(feature ="rust1", since ="1.0.0")]1808    #[rustc_const_stable(feature ="const_vec_string_slice", since ="1.87.0")]1809pub const unsafe fnas_mut_vec(&mutself) ->&mutVec<u8> {1810&mutself.vec1811    }18121813/// Returns the length of this `String`, in bytes, not [`char`]s or1814    /// graphemes. In other words, it might not be what a human considers the1815    /// length of the string.1816    ///1817    /// # Examples1818    ///1819    /// ```1820    /// let a = String::from("foo");1821    /// assert_eq!(a.len(), 3);1822    ///1823    /// let fancy_f = String::from("ƒoo");1824    /// assert_eq!(fancy_f.len(), 4);1825    /// assert_eq!(fancy_f.chars().count(), 3);1826    /// ```1827#[inline]1828    #[must_use]1829    #[stable(feature ="rust1", since ="1.0.0")]1830    #[rustc_const_stable(feature ="const_vec_string_slice", since ="1.87.0")]1831    #[rustc_confusables("length","size")]1832    #[rustc_no_implicit_autorefs]1833pub const fnlen(&self) -> usize {1834self.vec.len()1835    }18361837/// Returns `true` if this `String` has a length of zero, and `false` otherwise.1838    ///1839    /// # Examples1840    ///1841    /// ```1842    /// let mut v = String::new();1843    /// assert!(v.is_empty());1844    ///1845    /// v.push('a');1846    /// assert!(!v.is_empty());1847    /// ```1848#[inline]1849    #[must_use]1850    #[stable(feature ="rust1", since ="1.0.0")]1851    #[rustc_const_stable(feature ="const_vec_string_slice", since ="1.87.0")]1852    #[rustc_no_implicit_autorefs]1853pub const fnis_empty(&self) -> bool {1854self.len() ==01855}18561857/// Splits the string into two at the given byte index.1858    ///1859    /// Returns a newly allocated `String`. `self` contains bytes `[0, at)`, and1860    /// the returned `String` contains bytes `[at, len)`. `at` must be on the1861    /// boundary of a UTF-8 code point.1862    ///1863    /// Note that the capacity of `self` does not change.1864    ///1865    /// # Panics1866    ///1867    /// Panics if `at` is not on a `UTF-8` code point boundary, or if it is beyond the last1868    /// code point of the string.1869    ///1870    /// # Examples1871    ///1872    /// ```1873    /// # fn main() {1874    /// let mut hello = String::from("Hello, World!");1875    /// let world = hello.split_off(7);1876    /// assert_eq!(hello, "Hello, ");1877    /// assert_eq!(world, "World!");1878    /// # }1879    /// ```1880#[cfg(not(no_global_oom_handling))]1881    #[inline]1882    #[track_caller]1883    #[stable(feature ="string_split_off", since ="1.16.0")]1884    #[must_use ="use `.truncate()` if you don't need the other half"]1885pub fnsplit_off(&mutself, at: usize) -> String {1886assert!(self.is_char_boundary(at));1887letother =self.vec.split_off(at);1888unsafe{ String::from_utf8_unchecked(other) }1889    }18901891/// Truncates this `String`, removing all contents.1892    ///1893    /// While this means the `String` will have a length of zero, it does not1894    /// touch its capacity.1895    ///1896    /// # Examples1897    ///1898    /// ```1899    /// let mut s = String::from("foo");1900    ///1901    /// s.clear();1902    ///1903    /// assert!(s.is_empty());1904    /// assert_eq!(0, s.len());1905    /// assert_eq!(3, s.capacity());1906    /// ```1907#[inline]1908    #[stable(feature ="rust1", since ="1.0.0")]1909pub fnclear(&mutself) {1910self.vec.clear()1911    }19121913/// Removes the specified range from the string in bulk, returning all1914    /// removed characters as an iterator.1915    ///1916    /// The returned iterator keeps a mutable borrow on the string to optimize1917    /// its implementation.1918    ///1919    /// # Panics1920    ///1921    /// Panics if the range has `start_bound > end_bound`, or, if the range is1922    /// bounded on either end and does not lie on a [`char`] boundary.1923    ///1924    /// # Leaking1925    ///1926    /// If the returned iterator goes out of scope without being dropped (due to1927    /// [`core::mem::forget`], for example), the string may still contain a copy1928    /// of any drained characters, or may have lost characters arbitrarily,1929    /// including characters outside the range.1930    ///1931    /// # Examples1932    ///1933    /// ```1934    /// let mut s = String::from("α is alpha, β is beta");1935    /// let beta_offset = s.find('β').unwrap_or(s.len());1936    ///1937    /// // Remove the range up until the β from the string1938    /// let t: String = s.drain(..beta_offset).collect();1939    /// assert_eq!(t, "α is alpha, ");1940    /// assert_eq!(s, "β is beta");1941    ///1942    /// // A full range clears the string, like `clear()` does1943    /// s.drain(..);1944    /// assert_eq!(s, "");1945    /// ```1946#[stable(feature ="drain", since ="1.6.0")]1947    #[track_caller]1948pub fndrain<R>(&mutself, range: R) -> Drain<'_>1949where1950R: RangeBounds<usize>,1951    {1952// Memory safety1953        //1954        // The String version of Drain does not have the memory safety issues1955        // of the vector version. The data is just plain bytes.1956        // Because the range removal happens in Drop, if the Drain iterator is leaked,1957        // the removal will not happen.1958letRange { start, end } = slice::range(range, ..self.len());1959assert!(self.is_char_boundary(start));1960assert!(self.is_char_boundary(end));19611962// Take out two simultaneous borrows. The &mut String won't be accessed1963        // until iteration is over, in Drop.1964letself_ptr =selfas*mut_;1965// SAFETY: `slice::range` and `is_char_boundary` do the appropriate bounds checks.1966letchars_iter =unsafe{self.get_unchecked(start..end) }.chars();19671968        Drain { start, end, iter: chars_iter, string: self_ptr }1969    }19701971/// Converts a `String` into an iterator over the [`char`]s of the string.1972    ///1973    /// As a string consists of valid UTF-8, we can iterate through a string1974    /// by [`char`]. This method returns such an iterator.1975    ///1976    /// It's important to remember that [`char`] represents a Unicode Scalar1977    /// Value, and might not match your idea of what a 'character' is. Iteration1978    /// over grapheme clusters may be what you actually want. That functionality1979    /// is not provided by Rust's standard library, check crates.io instead.1980    ///1981    /// # Examples1982    ///1983    /// Basic usage:1984    ///1985    /// ```1986    /// #![feature(string_into_chars)]1987    ///1988    /// let word = String::from("goodbye");1989    ///1990    /// let mut chars = word.into_chars();1991    ///1992    /// assert_eq!(Some('g'), chars.next());1993    /// assert_eq!(Some('o'), chars.next());1994    /// assert_eq!(Some('o'), chars.next());1995    /// assert_eq!(Some('d'), chars.next());1996    /// assert_eq!(Some('b'), chars.next());1997    /// assert_eq!(Some('y'), chars.next());1998    /// assert_eq!(Some('e'), chars.next());1999    ///2000    /// assert_eq!(None, chars.next());2001    /// ```2002    ///2003    /// Remember, [`char`]s might not match your intuition about characters:2004    ///2005    /// ```2006    /// #![feature(string_into_chars)]2007    ///2008    /// let y = String::from("y̆");2009    ///2010    /// let mut chars = y.into_chars();2011    ///2012    /// assert_eq!(Some('y'), chars.next()); // not 'y̆'2013    /// assert_eq!(Some('\u{0306}'), chars.next());2014    ///2015    /// assert_eq!(None, chars.next());2016    /// ```2017    ///2018    /// [`char`]: prim@char2019#[inline]2020    #[must_use ="`self` will be dropped if the result is not used"]2021    #[unstable(feature ="string_into_chars", issue ="133125")]2022pub fninto_chars(self) -> IntoChars {2023        IntoChars { bytes:self.into_bytes().into_iter() }2024    }20252026/// Removes the specified range in the string,2027    /// and replaces it with the given string.2028    /// The given string doesn't need to be the same length as the range.2029    ///2030    /// # Panics2031    ///2032    /// Panics if the range has `start_bound > end_bound`, or, if the range is2033    /// bounded on either end and does not lie on a [`char`] boundary.2034    ///2035    /// # Examples2036    ///2037    /// ```2038    /// let mut s = String::from("α is alpha, β is beta");2039    /// let beta_offset = s.find('β').unwrap_or(s.len());2040    ///2041    /// // Replace the range up until the β from the string2042    /// s.replace_range(..beta_offset, "Α is capital alpha; ");2043    /// assert_eq!(s, "Α is capital alpha; β is beta");2044    /// ```2045#[cfg(not(no_global_oom_handling))]2046    #[stable(feature ="splice", since ="1.27.0")]2047    #[track_caller]2048pub fnreplace_range<R>(&mutself, range: R, replace_with:&str)2049where2050R: RangeBounds<usize>,2051    {2052// Memory safety2053        //2054        // Replace_range does not have the memory safety issues of a vector Splice.2055        // of the vector version. The data is just plain bytes.20562057        // WARNING: Inlining this variable would be unsound (#81138)2058letstart = range.start_bound();2059matchstart {2060            Included(&n) =>assert!(self.is_char_boundary(n)),2061            Excluded(&n) =>assert!(self.is_char_boundary(n +1)),2062            Unbounded => {}2063        };2064// WARNING: Inlining this variable would be unsound (#81138)2065letend = range.end_bound();2066matchend {2067            Included(&n) =>assert!(self.is_char_boundary(n +1)),2068            Excluded(&n) =>assert!(self.is_char_boundary(n)),2069            Unbounded => {}2070        };20712072// Using `range` again would be unsound (#81138)2073        // We assume the bounds reported by `range` remain the same, but2074        // an adversarial implementation could change between calls2075unsafe{self.as_mut_vec() }.splice((start, end), replace_with.bytes());2076    }20772078/// Replaces the leftmost occurrence of a pattern with another string, in-place.2079    ///2080    /// This method can be preferred over [`string = string.replacen(..., 1);`][replacen],2081    /// as it can use the `String`'s existing capacity to prevent a reallocation if2082    /// sufficient space is available.2083    ///2084    /// # Examples2085    ///2086    /// Basic usage:2087    ///2088    /// ```2089    /// #![feature(string_replace_in_place)]2090    ///2091    /// let mut s = String::from("Test Results: ❌❌❌");2092    ///2093    /// // Replace the leftmost ❌ with a ✅2094    /// s.replace_first('❌', "✅");2095    /// assert_eq!(s, "Test Results: ✅❌❌");2096    /// ```2097    ///2098    /// [replacen]: ../../std/primitive.str.html#method.replacen2099#[cfg(not(no_global_oom_handling))]2100    #[unstable(feature ="string_replace_in_place", issue ="147949")]2101pub fnreplace_first<P: Pattern>(&mutself, from: P, to:&str) {2102letrange =matchself.match_indices(from).next() {2103Some((start, match_str)) => start..start + match_str.len(),2104None=>return,2105        };21062107self.replace_range(range, to);2108    }21092110/// Replaces the rightmost occurrence of a pattern with another string, in-place.2111    ///2112    /// # Examples2113    ///2114    /// Basic usage:2115    ///2116    /// ```2117    /// #![feature(string_replace_in_place)]2118    ///2119    /// let mut s = String::from("Test Results: ❌❌❌");2120    ///2121    /// // Replace the rightmost ❌ with a ✅2122    /// s.replace_last('❌', "✅");2123    /// assert_eq!(s, "Test Results: ❌❌✅");2124    /// ```2125#[cfg(not(no_global_oom_handling))]2126    #[unstable(feature ="string_replace_in_place", issue ="147949")]2127pub fnreplace_last<P: Pattern>(&mutself, from: P, to:&str)2128where2129        for<'a> P::Searcher<'a>: core::str::pattern::ReverseSearcher<'a>,2130    {2131letrange =matchself.rmatch_indices(from).next() {2132Some((start, match_str)) => start..start + match_str.len(),2133None=>return,2134        };21352136self.replace_range(range, to);2137    }21382139/// Converts this `String` into a <code>[Box]<[str]></code>.2140    ///2141    /// Before doing the conversion, this method discards excess capacity like [`shrink_to_fit`].2142    /// Note that this call may reallocate and copy the bytes of the string.2143    ///2144    /// [`shrink_to_fit`]: String::shrink_to_fit2145    /// [str]: prim@str "str"2146    ///2147    /// # Examples2148    ///2149    /// ```2150    /// let s = String::from("hello");2151    ///2152    /// let b = s.into_boxed_str();2153    /// ```2154#[cfg(not(no_global_oom_handling))]2155    #[stable(feature ="box_str", since ="1.4.0")]2156    #[must_use ="`self` will be dropped if the result is not used"]2157    #[inline]2158pub fninto_boxed_str(self) -> Box<str> {2159letslice =self.vec.into_boxed_slice();2160unsafe{ from_boxed_utf8_unchecked(slice) }2161    }21622163/// Consumes and leaks the `String`, returning a mutable reference to the contents,2164    /// `&'a mut str`.2165    ///2166    /// The caller has free choice over the returned lifetime, including `'static`. Indeed,2167    /// this function is ideally used for data that lives for the remainder of the program's life,2168    /// as dropping the returned reference will cause a memory leak.2169    ///2170    /// It does not reallocate or shrink the `String`, so the leaked allocation may include unused2171    /// capacity that is not part of the returned slice. If you want to discard excess capacity,2172    /// call [`into_boxed_str`], and then [`Box::leak`] instead. However, keep in mind that2173    /// trimming the capacity may result in a reallocation and copy.2174    ///2175    /// [`into_boxed_str`]: Self::into_boxed_str2176    ///2177    /// # Examples2178    ///2179    /// ```2180    /// let x = String::from("bucket");2181    /// let static_ref: &'static mut str = x.leak();2182    /// assert_eq!(static_ref, "bucket");2183    /// # // FIXME(https://github.com/rust-lang/miri/issues/3670):2184    /// # // use -Zmiri-disable-leak-check instead of unleaking in tests meant to leak.2185    /// # drop(unsafe { Box::from_raw(static_ref) });2186    /// ```2187#[stable(feature ="string_leak", since ="1.72.0")]2188    #[inline]2189pub fnleak<'a>(self) ->&'amutstr {2190letslice =self.vec.leak();2191unsafe{ from_utf8_unchecked_mut(slice) }2192    }2193}21942195implFromUtf8Error {2196/// Returns a slice of [`u8`]s bytes that were attempted to convert to a `String`.2197    ///2198    /// # Examples2199    ///2200    /// ```2201    /// // some invalid bytes, in a vector2202    /// let bytes = vec![0, 159];2203    ///2204    /// let value = String::from_utf8(bytes);2205    ///2206    /// assert_eq!(&[0, 159], value.unwrap_err().as_bytes());2207    /// ```2208#[must_use]2209    #[stable(feature ="from_utf8_error_as_bytes", since ="1.26.0")]2210pub fnas_bytes(&self) ->&[u8] {2211&self.bytes[..]2212    }22132214/// Converts the bytes into a `String` lossily, substituting invalid UTF-82215    /// sequences with replacement characters.2216    ///2217    /// See [`String::from_utf8_lossy`] for more details on replacement of2218    /// invalid sequences, and [`String::from_utf8_lossy_owned`] for the2219    /// `String` function which corresponds to this function.2220    ///2221    /// # Examples2222    ///2223    /// ```2224    /// #![feature(string_from_utf8_lossy_owned)]2225    /// // some invalid bytes2226    /// let input: Vec<u8> = b"Hello \xF0\x90\x80World".into();2227    /// let output = String::from_utf8(input).unwrap_or_else(|e| e.into_utf8_lossy());2228    ///2229    /// assert_eq!(String::from("Hello �World"), output);2230    /// ```2231#[must_use]2232    #[cfg(not(no_global_oom_handling))]2233    #[unstable(feature ="string_from_utf8_lossy_owned", issue ="129436")]2234pub fninto_utf8_lossy(self) -> String {2235constREPLACEMENT:&str ="\u{FFFD}";22362237letmutres = {2238letmutv = Vec::with_capacity(self.bytes.len());22392240// `Utf8Error::valid_up_to` returns the maximum index of validated2241            // UTF-8 bytes. Copy the valid bytes into the output buffer.2242v.extend_from_slice(&self.bytes[..self.error.valid_up_to()]);22432244// SAFETY: This is safe because the only bytes present in the buffer2245            // were validated as UTF-8 by the call to `String::from_utf8` which2246            // produced this `FromUtf8Error`.2247unsafe{ String::from_utf8_unchecked(v) }2248        };22492250letiter =self.bytes[self.error.valid_up_to()..].utf8_chunks();22512252forchunkiniter {2253            res.push_str(chunk.valid());2254if!chunk.invalid().is_empty() {2255                res.push_str(REPLACEMENT);2256            }2257        }22582259        res2260    }22612262/// Returns the bytes that were attempted to convert to a `String`.2263    ///2264    /// This method is carefully constructed to avoid allocation. It will2265    /// consume the error, moving out the bytes, so that a copy of the bytes2266    /// does not need to be made.2267    ///2268    /// # Examples2269    ///2270    /// ```2271    /// // some invalid bytes, in a vector2272    /// let bytes = vec![0, 159];2273    ///2274    /// let value = String::from_utf8(bytes);2275    ///2276    /// assert_eq!(vec![0, 159], value.unwrap_err().into_bytes());2277    /// ```2278#[must_use ="`self` will be dropped if the result is not used"]2279    #[stable(feature ="rust1", since ="1.0.0")]2280pub fninto_bytes(self) -> Vec<u8> {2281self.bytes2282    }22832284/// Fetch a `Utf8Error` to get more details about the conversion failure.2285    ///2286    /// The [`Utf8Error`] type provided by [`std::str`] represents an error that may2287    /// occur when converting a slice of [`u8`]s to a [`&str`]. In this sense, it's2288    /// an analogue to `FromUtf8Error`. See its documentation for more details2289    /// on using it.2290    ///2291    /// [`std::str`]: core::str "std::str"2292    /// [`&str`]: prim@str "&str"2293    ///2294    /// # Examples2295    ///2296    /// ```2297    /// // some invalid bytes, in a vector2298    /// let bytes = vec![0, 159];2299    ///2300    /// let error = String::from_utf8(bytes).unwrap_err().utf8_error();2301    ///2302    /// // the first byte is invalid here2303    /// assert_eq!(1, error.valid_up_to());2304    /// ```2305#[must_use]2306    #[stable(feature ="rust1", since ="1.0.0")]2307pub fnutf8_error(&self) -> Utf8Error {2308self.error2309    }2310}23112312#[stable(feature ="rust1", since ="1.0.0")]2313implfmt::DisplayforFromUtf8Error {2314fnfmt(&self, f:&mutfmt::Formatter<'_>) -> fmt::Result {2315        fmt::Display::fmt(&self.error, f)2316    }2317}23182319#[stable(feature ="rust1", since ="1.0.0")]2320implfmt::DisplayforFromUtf16Error {2321fnfmt(&self, f:&mutfmt::Formatter<'_>) -> fmt::Result {2322        fmt::Display::fmt("invalid utf-16: lone surrogate found", f)2323    }2324}23252326#[stable(feature ="rust1", since ="1.0.0")]2327implErrorforFromUtf8Error {}23282329#[stable(feature ="rust1", since ="1.0.0")]2330implErrorforFromUtf16Error {}23312332#[cfg(not(no_global_oom_handling))]2333#[stable(feature ="rust1", since ="1.0.0")]2334implCloneforString {2335fnclone(&self) ->Self{2336        String { vec:self.vec.clone() }2337    }23382339/// Clones the contents of `source` into `self`.2340    ///2341    /// This method is preferred over simply assigning `source.clone()` to `self`,2342    /// as it avoids reallocation if possible.2343fnclone_from(&mutself, source:&Self) {2344self.vec.clone_from(&source.vec);2345    }2346}23472348#[cfg(not(no_global_oom_handling))]2349#[stable(feature ="rust1", since ="1.0.0")]2350implFromIterator<char>forString {2351fnfrom_iter<I: IntoIterator<Item = char>>(iter: I) -> String {2352letmutbuf = String::new();2353        buf.extend(iter);2354        buf2355    }2356}23572358#[cfg(not(no_global_oom_handling))]2359#[stable(feature ="string_from_iter_by_ref", since ="1.17.0")]2360impl<'a> FromIterator<&'achar>forString {2361fnfrom_iter<I: IntoIterator<Item =&'achar>>(iter: I) -> String {2362letmutbuf = String::new();2363        buf.extend(iter);2364        buf2365    }2366}23672368#[cfg(not(no_global_oom_handling))]2369#[stable(feature ="rust1", since ="1.0.0")]2370impl<'a> FromIterator<&'astr>forString {2371fnfrom_iter<I: IntoIterator<Item =&'astr>>(iter: I) -> String {2372letmutbuf = String::new();2373        buf.extend(iter);2374        buf2375    }2376}23772378#[cfg(not(no_global_oom_handling))]2379#[stable(feature ="extend_string", since ="1.4.0")]2380implFromIterator<String>forString {2381fnfrom_iter<I: IntoIterator<Item = String>>(iter: I) -> String {2382letmutiterator = iter.into_iter();23832384// Because we're iterating over `String`s, we can avoid at least2385        // one allocation by getting the first string from the iterator2386        // and appending to it all the subsequent strings.2387matchiterator.next() {2388None=> String::new(),2389Some(mutbuf) => {2390                buf.extend(iterator);2391                buf2392            }2393        }2394    }2395}23962397#[cfg(not(no_global_oom_handling))]2398#[stable(feature ="box_str2", since ="1.45.0")]2399impl<A: Allocator> FromIterator<Box<str, A>>forString {2400fnfrom_iter<I: IntoIterator<Item = Box<str, A>>>(iter: I) -> String {2401letmutbuf = String::new();2402        buf.extend(iter);2403        buf2404    }2405}24062407#[cfg(not(no_global_oom_handling))]2408#[stable(feature ="herd_cows", since ="1.19.0")]2409impl<'a> FromIterator<Cow<'a, str>>forString {2410fnfrom_iter<I: IntoIterator<Item = Cow<'a, str>>>(iter: I) -> String {2411letmutiterator = iter.into_iter();24122413// Because we're iterating over CoWs, we can (potentially) avoid at least2414        // one allocation by getting the first item and appending to it all the2415        // subsequent items.2416matchiterator.next() {2417None=> String::new(),2418Some(cow) => {2419letmutbuf = cow.into_owned();2420                buf.extend(iterator);2421                buf2422            }2423        }2424    }2425}24262427#[cfg(not(no_global_oom_handling))]2428#[unstable(feature ="ascii_char", issue ="110998")]2429implFromIterator<core::ascii::Char>forString {2430fnfrom_iter<T: IntoIterator<Item = core::ascii::Char>>(iter: T) ->Self{2431letbuf = iter.into_iter().map(core::ascii::Char::to_u8).collect();2432// SAFETY: `buf` is guaranteed to be valid UTF-8 because the `core::ascii::Char` type2433        // only contains ASCII values (0x00-0x7F), which are valid UTF-8.2434unsafe{ String::from_utf8_unchecked(buf) }2435    }2436}24372438#[cfg(not(no_global_oom_handling))]2439#[unstable(feature ="ascii_char", issue ="110998")]2440impl<'a> FromIterator<&'acore::ascii::Char>forString {2441fnfrom_iter<T: IntoIterator<Item =&'acore::ascii::Char>>(iter: T) ->Self{2442letbuf = iter.into_iter().copied().map(core::ascii::Char::to_u8).collect();2443// SAFETY: `buf` is guaranteed to be valid UTF-8 because the `core::ascii::Char` type2444        // only contains ASCII values (0x00-0x7F), which are valid UTF-8.2445unsafe{ String::from_utf8_unchecked(buf) }2446    }2447}24482449#[cfg(not(no_global_oom_handling))]2450#[stable(feature ="rust1", since ="1.0.0")]2451implExtend<char>forString {2452fnextend<I: IntoIterator<Item = char>>(&mutself, iter: I) {2453letiterator = iter.into_iter();2454let(lower_bound,_) = iterator.size_hint();2455self.reserve(lower_bound);2456        iterator.for_each(move|c|self.push(c));2457    }24582459#[inline]2460fnextend_one(&mutself, c: char) {2461self.push(c);2462    }24632464#[inline]2465fnextend_reserve(&mutself, additional: usize) {2466self.reserve(additional);2467    }2468}24692470#[cfg(not(no_global_oom_handling))]2471#[stable(feature ="extend_ref", since ="1.2.0")]2472impl<'a> Extend<&'achar>forString {2473fnextend<I: IntoIterator<Item =&'achar>>(&mutself, iter: I) {2474self.extend(iter.into_iter().cloned());2475    }24762477#[inline]2478fnextend_one(&mutself,&c:&'achar) {2479self.push(c);2480    }24812482#[inline]2483fnextend_reserve(&mutself, additional: usize) {2484self.reserve(additional);2485    }2486}24872488#[cfg(not(no_global_oom_handling))]2489#[stable(feature ="rust1", since ="1.0.0")]2490impl<'a> Extend<&'astr>forString {2491fnextend<I: IntoIterator<Item =&'astr>>(&mutself, iter: I) {2492        iter.into_iter().for_each(move|s|self.push_str(s));2493    }24942495#[inline]2496fnextend_one(&mutself, s:&'astr) {2497self.push_str(s);2498    }2499}25002501#[cfg(not(no_global_oom_handling))]2502#[stable(feature ="box_str2", since ="1.45.0")]2503impl<A: Allocator> Extend<Box<str, A>>forString {2504fnextend<I: IntoIterator<Item = Box<str, A>>>(&mutself, iter: I) {2505        iter.into_iter().for_each(move|s|self.push_str(&s));2506    }2507}25082509#[cfg(not(no_global_oom_handling))]2510#[stable(feature ="extend_string", since ="1.4.0")]2511implExtend<String>forString {2512fnextend<I: IntoIterator<Item = String>>(&mutself, iter: I) {2513        iter.into_iter().for_each(move|s|self.push_str(&s));2514    }25152516#[inline]2517fnextend_one(&mutself, s: String) {2518self.push_str(&s);2519    }2520}25212522#[cfg(not(no_global_oom_handling))]2523#[stable(feature ="herd_cows", since ="1.19.0")]2524impl<'a> Extend<Cow<'a, str>>forString {2525fnextend<I: IntoIterator<Item = Cow<'a, str>>>(&mutself, iter: I) {2526        iter.into_iter().for_each(move|s|self.push_str(&s));2527    }25282529#[inline]2530fnextend_one(&mutself, s: Cow<'a, str>) {2531self.push_str(&s);2532    }2533}25342535#[cfg(not(no_global_oom_handling))]2536#[unstable(feature ="ascii_char", issue ="110998")]2537implExtend<core::ascii::Char>forString {2538#[inline]2539fnextend<I: IntoIterator<Item = core::ascii::Char>>(&mutself, iter: I) {2540self.vec.extend(iter.into_iter().map(|c| c.to_u8()));2541    }25422543#[inline]2544fnextend_one(&mutself, c: core::ascii::Char) {2545self.vec.push(c.to_u8());2546    }2547}25482549#[cfg(not(no_global_oom_handling))]2550#[unstable(feature ="ascii_char", issue ="110998")]2551impl<'a> Extend<&'acore::ascii::Char>forString {2552#[inline]2553fnextend<I: IntoIterator<Item =&'acore::ascii::Char>>(&mutself, iter: I) {2554self.extend(iter.into_iter().cloned());2555    }25562557#[inline]2558fnextend_one(&mutself, c:&'acore::ascii::Char) {2559self.vec.push(c.to_u8());2560    }2561}25622563/// A convenience impl that delegates to the impl for `&str`.2564///2565/// # Examples2566///2567/// ```2568/// assert_eq!(String::from("Hello world").find("world"), Some(6));2569/// ```2570#[unstable(2571    feature ="pattern",2572    reason ="API not fully fleshed out and ready to be stabilized",2573    issue ="27721"2574)]2575impl<'b> Patternfor&'bString {2576typeSearcher<'a> = <&'bstrasPattern>::Searcher<'a>;25772578fninto_searcher(self, haystack:&str) -> <&'bstrasPattern>::Searcher<'_> {2579self[..].into_searcher(haystack)2580    }25812582#[inline]2583fnis_contained_in(self, haystack:&str) -> bool {2584self[..].is_contained_in(haystack)2585    }25862587#[inline]2588fnis_prefix_of(self, haystack:&str) -> bool {2589self[..].is_prefix_of(haystack)2590    }25912592#[inline]2593fnstrip_prefix_of(self, haystack:&str) ->Option<&str> {2594self[..].strip_prefix_of(haystack)2595    }25962597#[inline]2598fnis_suffix_of<'a>(self, haystack:&'astr) -> bool2599where2600Self::Searcher<'a>: core::str::pattern::ReverseSearcher<'a>,2601    {2602self[..].is_suffix_of(haystack)2603    }26042605#[inline]2606fnstrip_suffix_of<'a>(self, haystack:&'astr) ->Option<&'astr>2607where2608Self::Searcher<'a>: core::str::pattern::ReverseSearcher<'a>,2609    {2610self[..].strip_suffix_of(haystack)2611    }26122613#[inline]2614fnas_utf8_pattern(&self) ->Option<Utf8Pattern<'_>> {2615Some(Utf8Pattern::StringPattern(self.as_bytes()))2616    }2617}26182619macro_rules! impl_eq {2620    ($lhs:ty,$rhs: ty) => {2621#[stable(feature ="rust1", since ="1.0.0")]2622        #[allow(unused_lifetimes)]2623impl<'a,'b> PartialEq<$rhs>for$lhs{2624#[inline]2625fneq(&self, other:&$rhs) -> bool {2626                PartialEq::eq(&self[..],&other[..])2627            }2628#[inline]2629fnne(&self, other:&$rhs) -> bool {2630                PartialEq::ne(&self[..],&other[..])2631            }2632        }26332634#[stable(feature ="rust1", since ="1.0.0")]2635        #[allow(unused_lifetimes)]2636impl<'a,'b> PartialEq<$lhs>for$rhs{2637#[inline]2638fneq(&self, other:&$lhs) -> bool {2639                PartialEq::eq(&self[..],&other[..])2640            }2641#[inline]2642fnne(&self, other:&$lhs) -> bool {2643                PartialEq::ne(&self[..],&other[..])2644            }2645        }2646    };2647}26482649impl_eq! { String, str }2650impl_eq! { String,&'astr }2651#[cfg(not(no_global_oom_handling))]2652impl_eq! { Cow<'a, str>, str }2653#[cfg(not(no_global_oom_handling))]2654impl_eq! { Cow<'a, str>,&'bstr }2655#[cfg(not(no_global_oom_handling))]2656impl_eq! { Cow<'a, str>, String }26572658#[stable(feature ="rust1", since ="1.0.0")]2659#[rustc_const_unstable(feature ="const_default", issue ="143894")]2660impl constDefaultforString {2661/// Creates an empty `String`.2662#[inline]2663fndefault() -> String {2664        String::new()2665    }2666}26672668#[stable(feature ="rust1", since ="1.0.0")]2669implfmt::DisplayforString {2670#[inline]2671fnfmt(&self, f:&mutfmt::Formatter<'_>) -> fmt::Result {2672        fmt::Display::fmt(&**self, f)2673    }2674}26752676#[stable(feature ="rust1", since ="1.0.0")]2677implfmt::DebugforString {2678#[inline]2679fnfmt(&self, f:&mutfmt::Formatter<'_>) -> fmt::Result {2680        fmt::Debug::fmt(&**self, f)2681    }2682}26832684#[stable(feature ="rust1", since ="1.0.0")]2685implhash::HashforString {2686#[inline]2687fnhash<H: hash::Hasher>(&self, hasher:&mutH) {2688        (**self).hash(hasher)2689    }2690}26912692/// Implements the `+` operator for concatenating two strings.2693///2694/// This consumes the `String` on the left-hand side and re-uses its buffer (growing it if2695/// necessary). This is done to avoid allocating a new `String` and copying the entire contents on2696/// every operation, which would lead to *O*(*n*^2) running time when building an *n*-byte string by2697/// repeated concatenation.2698///2699/// The string on the right-hand side is only borrowed; its contents are copied into the returned2700/// `String`.2701///2702/// # Examples2703///2704/// Concatenating two `String`s takes the first by value and borrows the second:2705///2706/// ```2707/// let a = String::from("hello");2708/// let b = String::from(" world");2709/// let c = a + &b;2710/// // `a` is moved and can no longer be used here.2711/// ```2712///2713/// If you want to keep using the first `String`, you can clone it and append to the clone instead:2714///2715/// ```2716/// let a = String::from("hello");2717/// let b = String::from(" world");2718/// let c = a.clone() + &b;2719/// // `a` is still valid here.2720/// ```2721///2722/// Concatenating `&str` slices can be done by converting the first to a `String`:2723///2724/// ```2725/// let a = "hello";2726/// let b = " world";2727/// let c = a.to_string() + b;2728/// ```2729#[cfg(not(no_global_oom_handling))]2730#[stable(feature ="rust1", since ="1.0.0")]2731implAdd<&str>forString {2732typeOutput = String;27332734#[inline]2735fnadd(mutself, other:&str) -> String {2736self.push_str(other);2737self2738}2739}27402741/// Implements the `+=` operator for appending to a `String`.2742///2743/// This has the same behavior as the [`push_str`][String::push_str] method.2744#[cfg(not(no_global_oom_handling))]2745#[stable(feature ="stringaddassign", since ="1.12.0")]2746implAddAssign<&str>forString {2747#[inline]2748fnadd_assign(&mutself, other:&str) {2749self.push_str(other);2750    }2751}27522753#[stable(feature ="rust1", since ="1.0.0")]2754impl<I> ops::Index<I>forString2755where2756I: slice::SliceIndex<str>,2757{2758typeOutput = I::Output;27592760#[inline]2761fnindex(&self, index: I) ->&I::Output {2762        index.index(self.as_str())2763    }2764}27652766#[stable(feature ="rust1", since ="1.0.0")]2767impl<I> ops::IndexMut<I>forString2768where2769I: slice::SliceIndex<str>,2770{2771#[inline]2772fnindex_mut(&mutself, index: I) ->&mutI::Output {2773        index.index_mut(self.as_mut_str())2774    }2775}27762777#[stable(feature ="rust1", since ="1.0.0")]2778implops::DerefforString {2779typeTarget = str;27802781#[inline]2782fnderef(&self) ->&str {2783self.as_str()2784    }2785}27862787#[unstable(feature ="deref_pure_trait", issue ="87121")]2788unsafe implops::DerefPureforString {}27892790#[stable(feature ="derefmut_for_string", since ="1.3.0")]2791implops::DerefMutforString {2792#[inline]2793fnderef_mut(&mutself) ->&mutstr {2794self.as_mut_str()2795    }2796}27972798/// A type alias for [`Infallible`].2799///2800/// This alias exists for backwards compatibility, and may be eventually deprecated.2801///2802/// [`Infallible`]: core::convert::Infallible "convert::Infallible"2803#[stable(feature ="str_parse_error", since ="1.5.0")]2804pub typeParseError = core::convert::Infallible;28052806#[cfg(not(no_global_oom_handling))]2807#[stable(feature ="rust1", since ="1.0.0")]2808implFromStrforString {2809typeErr= core::convert::Infallible;2810#[inline]2811fnfrom_str(s:&str) ->Result<String,Self::Err> {2812Ok(String::from(s))2813    }2814}28152816/// A trait for converting a value to a `String`.2817///2818/// This trait is automatically implemented for any type which implements the2819/// [`Display`] trait. As such, `ToString` shouldn't be implemented directly:2820/// [`Display`] should be implemented instead, and you get the `ToString`2821/// implementation for free.2822///2823/// [`Display`]: fmt::Display2824#[rustc_diagnostic_item ="ToString"]2825#[stable(feature ="rust1", since ="1.0.0")]2826pub traitToString {2827/// Converts the given value to a `String`.2828    ///2829    /// # Examples2830    ///2831    /// ```2832    /// let i = 5;2833    /// let five = String::from("5");2834    ///2835    /// assert_eq!(five, i.to_string());2836    /// ```2837#[rustc_conversion_suggestion]2838    #[stable(feature ="rust1", since ="1.0.0")]2839    #[rustc_diagnostic_item ="to_string_method"]2840fnto_string(&self) -> String;2841}28422843/// # Panics2844///2845/// In this implementation, the `to_string` method panics2846/// if the `Display` implementation returns an error.2847/// This indicates an incorrect `Display` implementation2848/// since `fmt::Write for String` never returns an error itself.2849#[cfg(not(no_global_oom_handling))]2850#[stable(feature ="rust1", since ="1.0.0")]2851impl<T: fmt::Display +?Sized> ToStringforT {2852#[inline]2853fnto_string(&self) -> String {2854        <SelfasSpecToString>::spec_to_string(self)2855    }2856}28572858#[cfg(not(no_global_oom_handling))]2859traitSpecToString {2860fnspec_to_string(&self) -> String;2861}28622863#[cfg(not(no_global_oom_handling))]2864impl<T: fmt::Display +?Sized> SpecToStringforT {2865// A common guideline is to not inline generic functions. However,2866    // removing `#[inline]` from this method causes non-negligible regressions.2867    // See <https://github.com/rust-lang/rust/pull/74852>, the last attempt2868    // to try to remove it.2869#[inline]2870defaultfnspec_to_string(&self) -> String {2871letmutbuf = String::new();2872letmutformatter =2873            core::fmt::Formatter::new(&mutbuf, core::fmt::FormattingOptions::new());2874// Bypass format_args!() to avoid write_str with zero-length strs2875fmt::Display::fmt(self,&mutformatter)2876            .expect("a Display implementation returned an error unexpectedly");2877        buf2878    }2879}28802881#[cfg(not(no_global_oom_handling))]2882implSpecToStringforcore::ascii::Char {2883#[inline]2884fnspec_to_string(&self) -> String {2885self.as_str().to_owned()2886    }2887}28882889#[cfg(not(no_global_oom_handling))]2890implSpecToStringforchar {2891#[inline]2892fnspec_to_string(&self) -> String {2893        String::from(self.encode_utf8(&mut[0; char::MAX_LEN_UTF8]))2894    }2895}28962897#[cfg(not(no_global_oom_handling))]2898implSpecToStringforbool {2899#[inline]2900fnspec_to_string(&self) -> String {2901        String::from(if*self{"true"}else{"false"})2902    }2903}29042905macro_rules! impl_to_string {2906    ($($signed:ident,$unsigned:ident,)*) => {2907        $(2908#[cfg(not(no_global_oom_handling))]2909        #[cfg(not(feature ="optimize_for_size"))]2910implSpecToStringfor$signed{2911#[inline]2912fnspec_to_string(&self) -> String {2913constSIZE: usize =$signed::MAX.ilog10()asusize +1;2914letmutbuf = [core::mem::MaybeUninit::<u8>::uninit(); SIZE];2915// Only difference between signed and unsigned are these 8 lines.2916letmutout;2917if*self<0{2918                    out = String::with_capacity(SIZE +1);2919                    out.push('-');2920                }else{2921                    out = String::with_capacity(SIZE);2922                }29232924// SAFETY: `buf` is always big enough to contain all the digits.2925unsafe{ out.push_str(self.unsigned_abs()._fmt(&mutbuf)); }2926                out2927            }2928        }2929#[cfg(not(no_global_oom_handling))]2930        #[cfg(not(feature ="optimize_for_size"))]2931implSpecToStringfor$unsigned{2932#[inline]2933fnspec_to_string(&self) -> String {2934constSIZE: usize =$unsigned::MAX.ilog10()asusize +1;2935letmutbuf = [core::mem::MaybeUninit::<u8>::uninit(); SIZE];29362937// SAFETY: `buf` is always big enough to contain all the digits.2938unsafe{self._fmt(&mutbuf).to_string() }2939            }2940        }2941        )*2942    }2943}29442945impl_to_string! {2946    i8, u8,2947    i16, u16,2948    i32, u32,2949    i64, u64,2950    isize, usize,2951    i128, u128,2952}29532954#[cfg(not(no_global_oom_handling))]2955#[cfg(feature ="optimize_for_size")]2956implSpecToStringforu8 {2957#[inline]2958fnspec_to_string(&self) -> String {2959letmutbuf = String::with_capacity(3);2960letmutn =*self;2961ifn >=10{2962ifn >=100{2963                buf.push((b'0'+ n /100)aschar);2964                n %=100;2965            }2966            buf.push((b'0'+ n /10)aschar);2967            n %=10;2968        }2969        buf.push((b'0'+ n)aschar);2970        buf2971    }2972}29732974#[cfg(not(no_global_oom_handling))]2975#[cfg(feature ="optimize_for_size")]2976implSpecToStringfori8 {2977#[inline]2978fnspec_to_string(&self) -> String {2979letmutbuf = String::with_capacity(4);2980ifself.is_negative() {2981            buf.push('-');2982        }2983letmutn =self.unsigned_abs();2984ifn >=10{2985ifn >=100{2986                buf.push('1');2987                n -=100;2988            }2989            buf.push((b'0'+ n /10)aschar);2990            n %=10;2991        }2992        buf.push((b'0'+ n)aschar);2993        buf2994    }2995}29962997#[cfg(not(no_global_oom_handling))]2998macro_rules! to_string_str {2999    {$($type:ty,)*} => {3000        $(3001implSpecToStringfor$type{3002#[inline]3003fnspec_to_string(&self) -> String {3004lets:&str =self;3005                    String::from(s)3006                }3007            }3008        )*3009    };3010}30113012#[cfg(not(no_global_oom_handling))]3013to_string_str! {3014    Cow<'_, str>,3015    String,3016// Generic/generated code can sometimes have multiple, nested references3017    // for strings, including `&&&str`s that would never be written3018    // by hand.3019&&&&&&&&&&&&str,3020    &&&&&&&&&&&str,3021    &&&&&&&&&&str,3022    &&&&&&&&&str,3023    &&&&&&&&str,3024    &&&&&&&str,3025    &&&&&&str,3026    &&&&&str,3027    &&&&str,3028    &&&str,3029    &&str,3030&str,3031    str,3032}30333034#[cfg(not(no_global_oom_handling))]3035implSpecToStringforfmt::Arguments<'_> {3036#[inline]3037fnspec_to_string(&self) -> String {3038crate::fmt::format(*self)3039    }3040}30413042#[stable(feature ="rust1", since ="1.0.0")]3043implAsRef<str>forString {3044#[inline]3045fnas_ref(&self) ->&str {3046self3047}3048}30493050#[stable(feature ="string_as_mut", since ="1.43.0")]3051implAsMut<str>forString {3052#[inline]3053fnas_mut(&mutself) ->&mutstr {3054self3055}3056}30573058#[stable(feature ="rust1", since ="1.0.0")]3059implAsRef<[u8]>forString {3060#[inline]3061fnas_ref(&self) ->&[u8] {3062self.as_bytes()3063    }3064}30653066#[cfg(not(no_global_oom_handling))]3067#[stable(feature ="rust1", since ="1.0.0")]3068implFrom<&str>forString {3069/// Converts a `&str` into a [`String`].3070    ///3071    /// The result is allocated on the heap.3072#[inline]3073fnfrom(s:&str) -> String {3074        s.to_owned()3075    }3076}30773078#[cfg(not(no_global_oom_handling))]3079#[stable(feature ="from_mut_str_for_string", since ="1.44.0")]3080implFrom<&mutstr>forString {3081/// Converts a `&mut str` into a [`String`].3082    ///3083    /// The result is allocated on the heap.3084#[inline]3085fnfrom(s:&mutstr) -> String {3086        s.to_owned()3087    }3088}30893090#[cfg(not(no_global_oom_handling))]3091#[stable(feature ="from_ref_string", since ="1.35.0")]3092implFrom<&String>forString {3093/// Converts a `&String` into a [`String`].3094    ///3095    /// This clones `s` and returns the clone.3096#[inline]3097fnfrom(s:&String) -> String {3098        s.clone()3099    }3100}31013102// note: test pulls in std, which causes errors here3103#[stable(feature ="string_from_box", since ="1.18.0")]3104implFrom<Box<str>>forString {3105/// Converts the given boxed `str` slice to a [`String`].3106    /// It is notable that the `str` slice is owned.3107    ///3108    /// # Examples3109    ///3110    /// ```3111    /// let s1: String = String::from("hello world");3112    /// let s2: Box<str> = s1.into_boxed_str();3113    /// let s3: String = String::from(s2);3114    ///3115    /// assert_eq!("hello world", s3)3116    /// ```3117fnfrom(s: Box<str>) -> String {3118        s.into_string()3119    }3120}31213122#[cfg(not(no_global_oom_handling))]3123#[stable(feature ="box_from_str", since ="1.20.0")]3124implFrom<String>forBox<str> {3125/// Converts the given [`String`] to a boxed `str` slice that is owned.3126    ///3127    /// # Examples3128    ///3129    /// ```3130    /// let s1: String = String::from("hello world");3131    /// let s2: Box<str> = Box::from(s1);3132    /// let s3: String = String::from(s2);3133    ///3134    /// assert_eq!("hello world", s3)3135    /// ```3136fnfrom(s: String) -> Box<str> {3137        s.into_boxed_str()3138    }3139}31403141#[cfg(not(no_global_oom_handling))]3142#[stable(feature ="string_from_cow_str", since ="1.14.0")]3143impl<'a> From<Cow<'a, str>>forString {3144/// Converts a clone-on-write string to an owned3145    /// instance of [`String`].3146    ///3147    /// This extracts the owned string,3148    /// clones the string if it is not already owned.3149    ///3150    /// # Example3151    ///3152    /// ```3153    /// # use std::borrow::Cow;3154    /// // If the string is not owned...3155    /// let cow: Cow<'_, str> = Cow::Borrowed("eggplant");3156    /// // It will allocate on the heap and copy the string.3157    /// let owned: String = String::from(cow);3158    /// assert_eq!(&owned[..], "eggplant");3159    /// ```3160fnfrom(s: Cow<'a, str>) -> String {3161        s.into_owned()3162    }3163}31643165#[cfg(not(no_global_oom_handling))]3166#[stable(feature ="rust1", since ="1.0.0")]3167impl<'a> From<&'astr>forCow<'a, str> {3168/// Converts a string slice into a [`Borrowed`] variant.3169    /// No heap allocation is performed, and the string3170    /// is not copied.3171    ///3172    /// # Example3173    ///3174    /// ```3175    /// # use std::borrow::Cow;3176    /// assert_eq!(Cow::from("eggplant"), Cow::Borrowed("eggplant"));3177    /// ```3178    ///3179    /// [`Borrowed`]: crate::borrow::Cow::Borrowed "borrow::Cow::Borrowed"3180#[inline]3181fnfrom(s:&'astr) -> Cow<'a, str> {3182        Cow::Borrowed(s)3183    }3184}31853186#[cfg(not(no_global_oom_handling))]3187#[stable(feature ="rust1", since ="1.0.0")]3188impl<'a> From<String>forCow<'a, str> {3189/// Converts a [`String`] into an [`Owned`] variant.3190    /// No heap allocation is performed, and the string3191    /// is not copied.3192    ///3193    /// # Example3194    ///3195    /// ```3196    /// # use std::borrow::Cow;3197    /// let s = "eggplant".to_string();3198    /// let s2 = "eggplant".to_string();3199    /// assert_eq!(Cow::from(s), Cow::<'static, str>::Owned(s2));3200    /// ```3201    ///3202    /// [`Owned`]: crate::borrow::Cow::Owned "borrow::Cow::Owned"3203#[inline]3204fnfrom(s: String) -> Cow<'a, str> {3205        Cow::Owned(s)3206    }3207}32083209#[cfg(not(no_global_oom_handling))]3210#[stable(feature ="cow_from_string_ref", since ="1.28.0")]3211impl<'a> From<&'aString>forCow<'a, str> {3212/// Converts a [`String`] reference into a [`Borrowed`] variant.3213    /// No heap allocation is performed, and the string3214    /// is not copied.3215    ///3216    /// # Example3217    ///3218    /// ```3219    /// # use std::borrow::Cow;3220    /// let s = "eggplant".to_string();3221    /// assert_eq!(Cow::from(&s), Cow::Borrowed("eggplant"));3222    /// ```3223    ///3224    /// [`Borrowed`]: crate::borrow::Cow::Borrowed "borrow::Cow::Borrowed"3225#[inline]3226fnfrom(s:&'aString) -> Cow<'a, str> {3227        Cow::Borrowed(s.as_str())3228    }3229}32303231#[cfg(not(no_global_oom_handling))]3232#[stable(feature ="cow_str_from_iter", since ="1.12.0")]3233impl<'a> FromIterator<char>forCow<'a, str> {3234fnfrom_iter<I: IntoIterator<Item = char>>(it: I) -> Cow<'a, str> {3235        Cow::Owned(FromIterator::from_iter(it))3236    }3237}32383239#[cfg(not(no_global_oom_handling))]3240#[stable(feature ="cow_str_from_iter", since ="1.12.0")]3241impl<'a,'b> FromIterator<&'bstr>forCow<'a, str> {3242fnfrom_iter<I: IntoIterator<Item =&'bstr>>(it: I) -> Cow<'a, str> {3243        Cow::Owned(FromIterator::from_iter(it))3244    }3245}32463247#[cfg(not(no_global_oom_handling))]3248#[stable(feature ="cow_str_from_iter", since ="1.12.0")]3249impl<'a> FromIterator<String>forCow<'a, str> {3250fnfrom_iter<I: IntoIterator<Item = String>>(it: I) -> Cow<'a, str> {3251        Cow::Owned(FromIterator::from_iter(it))3252    }3253}32543255#[cfg(not(no_global_oom_handling))]3256#[unstable(feature ="ascii_char", issue ="110998")]3257impl<'a> FromIterator<core::ascii::Char>forCow<'a, str> {3258fnfrom_iter<T: IntoIterator<Item = core::ascii::Char>>(it: T) ->Self{3259        Cow::Owned(FromIterator::from_iter(it))3260    }3261}32623263#[stable(feature ="from_string_for_vec_u8", since ="1.14.0")]3264implFrom<String>forVec<u8> {3265/// Converts the given [`String`] to a vector [`Vec`] that holds values of type [`u8`].3266    ///3267    /// # Examples3268    ///3269    /// ```3270    /// let s1 = String::from("hello world");3271    /// let v1 = Vec::from(s1);3272    ///3273    /// for b in v1 {3274    ///     println!("{b}");3275    /// }3276    /// ```3277fnfrom(string: String) -> Vec<u8> {3278        string.into_bytes()3279    }3280}32813282#[stable(feature ="try_from_vec_u8_for_string", since ="1.87.0")]3283implTryFrom<Vec<u8>>forString {3284typeError = FromUtf8Error;3285/// Converts the given [`Vec<u8>`] into a  [`String`] if it contains valid UTF-8 data.3286    ///3287    /// # Examples3288    ///3289    /// ```3290    /// let s1 = b"hello world".to_vec();3291    /// let v1 = String::try_from(s1).unwrap();3292    /// assert_eq!(v1, "hello world");3293    ///3294    /// ```3295fntry_from(bytes: Vec<u8>) ->Result<Self,Self::Error> {3296Self::from_utf8(bytes)3297    }3298}32993300#[cfg(not(no_global_oom_handling))]3301#[stable(feature ="rust1", since ="1.0.0")]3302implfmt::WriteforString {3303#[inline]3304fnwrite_str(&mutself, s:&str) -> fmt::Result {3305self.push_str(s);3306Ok(())3307    }33083309#[inline]3310fnwrite_char(&mutself, c: char) -> fmt::Result {3311self.push(c);3312Ok(())3313    }3314}33153316/// An iterator over the [`char`]s of a string.3317///3318/// This struct is created by the [`into_chars`] method on [`String`].3319/// See its documentation for more.3320///3321/// [`char`]: prim@char3322/// [`into_chars`]: String::into_chars3323#[cfg_attr(not(no_global_oom_handling), derive(Clone))]3324#[must_use ="iterators are lazy and do nothing unless consumed"]3325#[unstable(feature ="string_into_chars", issue ="133125")]3326pub structIntoChars {3327    bytes: vec::IntoIter<u8>,3328}33293330#[unstable(feature ="string_into_chars", issue ="133125")]3331implfmt::DebugforIntoChars {3332fnfmt(&self, f:&mutfmt::Formatter<'_>) -> fmt::Result {3333        f.debug_tuple("IntoChars").field(&self.as_str()).finish()3334    }3335}33363337implIntoChars {3338/// Views the underlying data as a subslice of the original data.3339    ///3340    /// # Examples3341    ///3342    /// ```3343    /// #![feature(string_into_chars)]3344    ///3345    /// let mut chars = String::from("abc").into_chars();3346    ///3347    /// assert_eq!(chars.as_str(), "abc");3348    /// chars.next();3349    /// assert_eq!(chars.as_str(), "bc");3350    /// chars.next();3351    /// chars.next();3352    /// assert_eq!(chars.as_str(), "");3353    /// ```3354#[unstable(feature ="string_into_chars", issue ="133125")]3355    #[must_use]3356    #[inline]3357pub fnas_str(&self) ->&str {3358// SAFETY: `bytes` is a valid UTF-8 string.3359unsafe{ str::from_utf8_unchecked(self.bytes.as_slice()) }3360    }33613362/// Consumes the `IntoChars`, returning the remaining string.3363    ///3364    /// # Examples3365    ///3366    /// ```3367    /// #![feature(string_into_chars)]3368    ///3369    /// let chars = String::from("abc").into_chars();3370    /// assert_eq!(chars.into_string(), "abc");3371    ///3372    /// let mut chars = String::from("def").into_chars();3373    /// chars.next();3374    /// assert_eq!(chars.into_string(), "ef");3375    /// ```3376#[cfg(not(no_global_oom_handling))]3377    #[unstable(feature ="string_into_chars", issue ="133125")]3378    #[inline]3379pub fninto_string(self) -> String {3380// Safety: `bytes` are kept in UTF-8 form, only removing whole `char`s at a time.3381unsafe{ String::from_utf8_unchecked(self.bytes.collect()) }3382    }33833384#[inline]3385fniter(&self) -> CharIndices<'_> {3386self.as_str().char_indices()3387    }3388}33893390#[unstable(feature ="string_into_chars", issue ="133125")]3391implIteratorforIntoChars {3392typeItem = char;33933394#[inline]3395fnnext(&mutself) ->Option<char> {3396letmutiter =self.iter();3397matchiter.next() {3398None=>None,3399Some((_, ch)) => {3400letoffset = iter.offset();3401// `offset` is a valid index.3402let _=self.bytes.advance_by(offset);3403Some(ch)3404            }3405        }3406    }34073408#[inline]3409fncount(self) -> usize {3410self.iter().count()3411    }34123413#[inline]3414fnsize_hint(&self) -> (usize,Option<usize>) {3415self.iter().size_hint()3416    }34173418#[inline]3419fnlast(mutself) ->Option<char> {3420self.next_back()3421    }3422}34233424#[unstable(feature ="string_into_chars", issue ="133125")]3425implDoubleEndedIteratorforIntoChars {3426#[inline]3427fnnext_back(&mutself) ->Option<char> {3428letlen =self.as_str().len();3429letmutiter =self.iter();3430matchiter.next_back() {3431None=>None,3432Some((idx, ch)) => {3433// `idx` is a valid index.3434let _=self.bytes.advance_back_by(len - idx);3435Some(ch)3436            }3437        }3438    }3439}34403441#[unstable(feature ="string_into_chars", issue ="133125")]3442implFusedIteratorforIntoChars {}34433444/// A draining iterator for `String`.3445///3446/// This struct is created by the [`drain`] method on [`String`]. See its3447/// documentation for more.3448///3449/// [`drain`]: String::drain3450#[stable(feature ="drain", since ="1.6.0")]3451pub structDrain<'a> {3452/// Will be used as &'a mut String in the destructor3453string:*mutString,3454/// Start of part to remove3455start: usize,3456/// End of part to remove3457end: usize,3458/// Current remaining range to remove3459iter: Chars<'a>,3460}34613462#[stable(feature ="collection_debug", since ="1.17.0")]3463implfmt::DebugforDrain<'_> {3464fnfmt(&self, f:&mutfmt::Formatter<'_>) -> fmt::Result {3465        f.debug_tuple("Drain").field(&self.as_str()).finish()3466    }3467}34683469#[stable(feature ="drain", since ="1.6.0")]3470unsafe implSyncforDrain<'_> {}3471#[stable(feature ="drain", since ="1.6.0")]3472unsafe implSendforDrain<'_> {}34733474#[stable(feature ="drain", since ="1.6.0")]3475implDropforDrain<'_> {3476fndrop(&mutself) {3477unsafe{3478// Use Vec::drain. "Reaffirm" the bounds checks to avoid3479            // panic code being inserted again.3480letself_vec = (*self.string).as_mut_vec();3481ifself.start <=self.end &&self.end <= self_vec.len() {3482                self_vec.drain(self.start..self.end);3483            }3484        }3485    }3486}34873488impl<'a> Drain<'a> {3489/// Returns the remaining (sub)string of this iterator as a slice.3490    ///3491    /// # Examples3492    ///3493    /// ```3494    /// let mut s = String::from("abc");3495    /// let mut drain = s.drain(..);3496    /// assert_eq!(drain.as_str(), "abc");3497    /// let _ = drain.next().unwrap();3498    /// assert_eq!(drain.as_str(), "bc");3499    /// ```3500#[must_use]3501    #[stable(feature ="string_drain_as_str", since ="1.55.0")]3502pub fnas_str(&self) ->&str {3503self.iter.as_str()3504    }3505}35063507#[stable(feature ="string_drain_as_str", since ="1.55.0")]3508impl<'a> AsRef<str>forDrain<'a> {3509fnas_ref(&self) ->&str {3510self.as_str()3511    }3512}35133514#[stable(feature ="string_drain_as_str", since ="1.55.0")]3515impl<'a> AsRef<[u8]>forDrain<'a> {3516fnas_ref(&self) ->&[u8] {3517self.as_str().as_bytes()3518    }3519}35203521#[stable(feature ="drain", since ="1.6.0")]3522implIteratorforDrain<'_> {3523typeItem = char;35243525#[inline]3526fnnext(&mutself) ->Option<char> {3527self.iter.next()3528    }35293530fnsize_hint(&self) -> (usize,Option<usize>) {3531self.iter.size_hint()3532    }35333534#[inline]3535fnlast(mutself) ->Option<char> {3536self.next_back()3537    }3538}35393540#[stable(feature ="drain", since ="1.6.0")]3541implDoubleEndedIteratorforDrain<'_> {3542#[inline]3543fnnext_back(&mutself) ->Option<char> {3544self.iter.next_back()3545    }3546}35473548#[stable(feature ="fused", since ="1.26.0")]3549implFusedIteratorforDrain<'_> {}35503551#[cfg(not(no_global_oom_handling))]3552#[stable(feature ="from_char_for_string", since ="1.46.0")]3553implFrom<char>forString {3554/// Allocates an owned [`String`] from a single character.3555    ///3556    /// # Example3557    /// ```rust3558    /// let c: char = 'a';3559    /// let s: String = String::from(c);3560    /// assert_eq!("a", &s[..]);3561    /// ```3562#[inline]3563fnfrom(c: char) ->Self{3564        c.to_string()3565    }3566}

[8]ページ先頭

©2009-2025 Movatter.jp