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