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