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