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