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