]> git.lizzy.rs Git - rust.git/blob - src/liballoc/string.rs
Merge branch 'master' into E0688
[rust.git] / src / liballoc / string.rs
1 //! A UTF-8 encoded, growable string.
2 //!
3 //! This module contains the [`String`] type, a trait for converting
4 //! [`ToString`]s, and several error types that may result from working with
5 //! [`String`]s.
6 //!
7 //! [`ToString`]: trait.ToString.html
8 //!
9 //! # Examples
10 //!
11 //! There are multiple ways to create a new [`String`] from a string literal:
12 //!
13 //! ```
14 //! let s = "Hello".to_string();
15 //!
16 //! let s = String::from("world");
17 //! let s: String = "also this".into();
18 //! ```
19 //!
20 //! You can create a new [`String`] from an existing one by concatenating with
21 //! `+`:
22 //!
23 //! [`String`]: struct.String.html
24 //!
25 //! ```
26 //! let s = "Hello".to_string();
27 //!
28 //! let message = s + " world!";
29 //! ```
30 //!
31 //! If you have a vector of valid UTF-8 bytes, you can make a [`String`] out of
32 //! it. You can do the reverse too.
33 //!
34 //! ```
35 //! let sparkle_heart = vec![240, 159, 146, 150];
36 //!
37 //! // We know these bytes are valid, so we'll use `unwrap()`.
38 //! let sparkle_heart = String::from_utf8(sparkle_heart).unwrap();
39 //!
40 //! assert_eq!("💖", sparkle_heart);
41 //!
42 //! let bytes = sparkle_heart.into_bytes();
43 //!
44 //! assert_eq!(bytes, [240, 159, 146, 150]);
45 //! ```
46
47 #![stable(feature = "rust1", since = "1.0.0")]
48
49 use core::char::{decode_utf16, REPLACEMENT_CHARACTER};
50 use core::fmt;
51 use core::hash;
52 use core::iter::{FromIterator, FusedIterator};
53 use core::ops::Bound::{Excluded, Included, Unbounded};
54 use core::ops::{self, Add, AddAssign, Index, IndexMut, RangeBounds};
55 use core::ptr;
56 use core::str::{lossy, pattern::Pattern};
57
58 use crate::borrow::{Cow, ToOwned};
59 use crate::boxed::Box;
60 use crate::collections::TryReserveError;
61 use crate::str::{self, from_boxed_utf8_unchecked, Chars, FromStr, Utf8Error};
62 use crate::vec::Vec;
63
64 /// A UTF-8 encoded, growable string.
65 ///
66 /// The `String` type is the most common string type that has ownership over the
67 /// contents of the string. It has a close relationship with its borrowed
68 /// counterpart, the primitive [`str`].
69 ///
70 /// [`str`]: ../../std/primitive.str.html
71 ///
72 /// # Examples
73 ///
74 /// You can create a `String` from a literal string with [`String::from`]:
75 ///
76 /// ```
77 /// let hello = String::from("Hello, world!");
78 /// ```
79 ///
80 /// You can append a [`char`] to a `String` with the [`push`] method, and
81 /// append a [`&str`] with the [`push_str`] method:
82 ///
83 /// ```
84 /// let mut hello = String::from("Hello, ");
85 ///
86 /// hello.push('w');
87 /// hello.push_str("orld!");
88 /// ```
89 ///
90 /// [`String::from`]: #method.from
91 /// [`char`]: ../../std/primitive.char.html
92 /// [`push`]: #method.push
93 /// [`push_str`]: #method.push_str
94 ///
95 /// If you have a vector of UTF-8 bytes, you can create a `String` from it with
96 /// the [`from_utf8`] method:
97 ///
98 /// ```
99 /// // some bytes, in a vector
100 /// let sparkle_heart = vec![240, 159, 146, 150];
101 ///
102 /// // We know these bytes are valid, so we'll use `unwrap()`.
103 /// let sparkle_heart = String::from_utf8(sparkle_heart).unwrap();
104 ///
105 /// assert_eq!("💖", sparkle_heart);
106 /// ```
107 ///
108 /// [`from_utf8`]: #method.from_utf8
109 ///
110 /// # UTF-8
111 ///
112 /// `String`s are always valid UTF-8. This has a few implications, the first of
113 /// which is that if you need a non-UTF-8 string, consider [`OsString`]. It is
114 /// similar, but without the UTF-8 constraint. The second implication is that
115 /// you cannot index into a `String`:
116 ///
117 /// ```compile_fail,E0277
118 /// let s = "hello";
119 ///
120 /// println!("The first letter of s is {}", s[0]); // ERROR!!!
121 /// ```
122 ///
123 /// [`OsString`]: ../../std/ffi/struct.OsString.html
124 ///
125 /// Indexing is intended to be a constant-time operation, but UTF-8 encoding
126 /// does not allow us to do this. Furthermore, it's not clear what sort of
127 /// thing the index should return: a byte, a codepoint, or a grapheme cluster.
128 /// The [`bytes`] and [`chars`] methods return iterators over the first
129 /// two, respectively.
130 ///
131 /// [`bytes`]: #method.bytes
132 /// [`chars`]: #method.chars
133 ///
134 /// # Deref
135 ///
136 /// `String`s implement [`Deref`]`<Target=str>`, and so inherit all of [`str`]'s
137 /// methods. In addition, this means that you can pass a `String` to a
138 /// function which takes a [`&str`] by using an ampersand (`&`):
139 ///
140 /// ```
141 /// fn takes_str(s: &str) { }
142 ///
143 /// let s = String::from("Hello");
144 ///
145 /// takes_str(&s);
146 /// ```
147 ///
148 /// This will create a [`&str`] from the `String` and pass it in. This
149 /// conversion is very inexpensive, and so generally, functions will accept
150 /// [`&str`]s as arguments unless they need a `String` for some specific
151 /// reason.
152 ///
153 /// In certain cases Rust doesn't have enough information to make this
154 /// conversion, known as [`Deref`] coercion. In the following example a string
155 /// slice [`&'a str`][`&str`] implements the trait `TraitExample`, and the function
156 /// `example_func` takes anything that implements the trait. In this case Rust
157 /// would need to make two implicit conversions, which Rust doesn't have the
158 /// means to do. For that reason, the following example will not compile.
159 ///
160 /// ```compile_fail,E0277
161 /// trait TraitExample {}
162 ///
163 /// impl<'a> TraitExample for &'a str {}
164 ///
165 /// fn example_func<A: TraitExample>(example_arg: A) {}
166 ///
167 /// let example_string = String::from("example_string");
168 /// example_func(&example_string);
169 /// ```
170 ///
171 /// There are two options that would work instead. The first would be to
172 /// change the line `example_func(&example_string);` to
173 /// `example_func(example_string.as_str());`, using the method [`as_str()`]
174 /// to explicitly extract the string slice containing the string. The second
175 /// way changes `example_func(&example_string);` to
176 /// `example_func(&*example_string);`. In this case we are dereferencing a
177 /// `String` to a [`str`][`&str`], then referencing the [`str`][`&str`] back to
178 /// [`&str`]. The second way is more idiomatic, however both work to do the
179 /// conversion explicitly rather than relying on the implicit conversion.
180 ///
181 /// # Representation
182 ///
183 /// A `String` is made up of three components: a pointer to some bytes, a
184 /// length, and a capacity. The pointer points to an internal buffer `String`
185 /// uses to store its data. The length is the number of bytes currently stored
186 /// in the buffer, and the capacity is the size of the buffer in bytes. As such,
187 /// the length will always be less than or equal to the capacity.
188 ///
189 /// This buffer is always stored on the heap.
190 ///
191 /// You can look at these with the [`as_ptr`], [`len`], and [`capacity`]
192 /// methods:
193 ///
194 /// ```
195 /// use std::mem;
196 ///
197 /// let story = String::from("Once upon a time...");
198 ///
199 // FIXME Update this when vec_into_raw_parts is stabilized
200 /// // Prevent automatically dropping the String's data
201 /// let mut story = mem::ManuallyDrop::new(story);
202 ///
203 /// let ptr = story.as_mut_ptr();
204 /// let len = story.len();
205 /// let capacity = story.capacity();
206 ///
207 /// // story has nineteen bytes
208 /// assert_eq!(19, len);
209 ///
210 /// // We can re-build a String out of ptr, len, and capacity. This is all
211 /// // unsafe because we are responsible for making sure the components are
212 /// // valid:
213 /// let s = unsafe { String::from_raw_parts(ptr, len, capacity) } ;
214 ///
215 /// assert_eq!(String::from("Once upon a time..."), s);
216 /// ```
217 ///
218 /// [`as_ptr`]: #method.as_ptr
219 /// [`len`]: #method.len
220 /// [`capacity`]: #method.capacity
221 ///
222 /// If a `String` has enough capacity, adding elements to it will not
223 /// re-allocate. For example, consider this program:
224 ///
225 /// ```
226 /// let mut s = String::new();
227 ///
228 /// println!("{}", s.capacity());
229 ///
230 /// for _ in 0..5 {
231 ///     s.push_str("hello");
232 ///     println!("{}", s.capacity());
233 /// }
234 /// ```
235 ///
236 /// This will output the following:
237 ///
238 /// ```text
239 /// 0
240 /// 5
241 /// 10
242 /// 20
243 /// 20
244 /// 40
245 /// ```
246 ///
247 /// At first, we have no memory allocated at all, but as we append to the
248 /// string, it increases its capacity appropriately. If we instead use the
249 /// [`with_capacity`] method to allocate the correct capacity initially:
250 ///
251 /// ```
252 /// let mut s = String::with_capacity(25);
253 ///
254 /// println!("{}", s.capacity());
255 ///
256 /// for _ in 0..5 {
257 ///     s.push_str("hello");
258 ///     println!("{}", s.capacity());
259 /// }
260 /// ```
261 ///
262 /// [`with_capacity`]: #method.with_capacity
263 ///
264 /// We end up with a different output:
265 ///
266 /// ```text
267 /// 25
268 /// 25
269 /// 25
270 /// 25
271 /// 25
272 /// 25
273 /// ```
274 ///
275 /// Here, there's no need to allocate more memory inside the loop.
276 ///
277 /// [`&str`]: ../../std/primitive.str.html
278 /// [`Deref`]: ../../std/ops/trait.Deref.html
279 /// [`as_str()`]: struct.String.html#method.as_str
280 #[derive(PartialOrd, Eq, Ord)]
281 #[cfg_attr(not(test), rustc_diagnostic_item = "string_type")]
282 #[stable(feature = "rust1", since = "1.0.0")]
283 pub struct String {
284     vec: Vec<u8>,
285 }
286
287 /// A possible error value when converting a `String` from a UTF-8 byte vector.
288 ///
289 /// This type is the error type for the [`from_utf8`] method on [`String`]. It
290 /// is designed in such a way to carefully avoid reallocations: the
291 /// [`into_bytes`] method will give back the byte vector that was used in the
292 /// conversion attempt.
293 ///
294 /// [`from_utf8`]: struct.String.html#method.from_utf8
295 /// [`String`]: struct.String.html
296 /// [`into_bytes`]: struct.FromUtf8Error.html#method.into_bytes
297 ///
298 /// The [`Utf8Error`] type provided by [`std::str`] represents an error that may
299 /// occur when converting a slice of [`u8`]s to a [`&str`]. In this sense, it's
300 /// an analogue to `FromUtf8Error`, and you can get one from a `FromUtf8Error`
301 /// through the [`utf8_error`] method.
302 ///
303 /// [`Utf8Error`]: ../../std/str/struct.Utf8Error.html
304 /// [`std::str`]: ../../std/str/index.html
305 /// [`u8`]: ../../std/primitive.u8.html
306 /// [`&str`]: ../../std/primitive.str.html
307 /// [`utf8_error`]: #method.utf8_error
308 ///
309 /// # Examples
310 ///
311 /// Basic usage:
312 ///
313 /// ```
314 /// // some invalid bytes, in a vector
315 /// let bytes = vec![0, 159];
316 ///
317 /// let value = String::from_utf8(bytes);
318 ///
319 /// assert!(value.is_err());
320 /// assert_eq!(vec![0, 159], value.unwrap_err().into_bytes());
321 /// ```
322 #[stable(feature = "rust1", since = "1.0.0")]
323 #[derive(Debug, Clone, PartialEq, Eq)]
324 pub struct FromUtf8Error {
325     bytes: Vec<u8>,
326     error: Utf8Error,
327 }
328
329 /// A possible error value when converting a `String` from a UTF-16 byte slice.
330 ///
331 /// This type is the error type for the [`from_utf16`] method on [`String`].
332 ///
333 /// [`from_utf16`]: struct.String.html#method.from_utf16
334 /// [`String`]: struct.String.html
335 ///
336 /// # Examples
337 ///
338 /// Basic usage:
339 ///
340 /// ```
341 /// // 𝄞mu<invalid>ic
342 /// let v = &[0xD834, 0xDD1E, 0x006d, 0x0075,
343 ///           0xD800, 0x0069, 0x0063];
344 ///
345 /// assert!(String::from_utf16(v).is_err());
346 /// ```
347 #[stable(feature = "rust1", since = "1.0.0")]
348 #[derive(Debug)]
349 pub struct FromUtf16Error(());
350
351 impl String {
352     /// Creates a new empty `String`.
353     ///
354     /// Given that the `String` is empty, this will not allocate any initial
355     /// buffer. While that means that this initial operation is very
356     /// inexpensive, it may cause excessive allocation later when you add
357     /// data. If you have an idea of how much data the `String` will hold,
358     /// consider the [`with_capacity`] method to prevent excessive
359     /// re-allocation.
360     ///
361     /// [`with_capacity`]: #method.with_capacity
362     ///
363     /// # Examples
364     ///
365     /// Basic usage:
366     ///
367     /// ```
368     /// let s = String::new();
369     /// ```
370     #[inline]
371     #[rustc_const_stable(feature = "const_string_new", since = "1.32.0")]
372     #[stable(feature = "rust1", since = "1.0.0")]
373     pub const fn new() -> String {
374         String { vec: Vec::new() }
375     }
376
377     /// Creates a new empty `String` with a particular capacity.
378     ///
379     /// `String`s have an internal buffer to hold their data. The capacity is
380     /// the length of that buffer, and can be queried with the [`capacity`]
381     /// method. This method creates an empty `String`, but one with an initial
382     /// buffer that can hold `capacity` bytes. This is useful when you may be
383     /// appending a bunch of data to the `String`, reducing the number of
384     /// reallocations it needs to do.
385     ///
386     /// [`capacity`]: #method.capacity
387     ///
388     /// If the given capacity is `0`, no allocation will occur, and this method
389     /// is identical to the [`new`] method.
390     ///
391     /// [`new`]: #method.new
392     ///
393     /// # Examples
394     ///
395     /// Basic usage:
396     ///
397     /// ```
398     /// let mut s = String::with_capacity(10);
399     ///
400     /// // The String contains no chars, even though it has capacity for more
401     /// assert_eq!(s.len(), 0);
402     ///
403     /// // These are all done without reallocating...
404     /// let cap = s.capacity();
405     /// for _ in 0..10 {
406     ///     s.push('a');
407     /// }
408     ///
409     /// assert_eq!(s.capacity(), cap);
410     ///
411     /// // ...but this may make the string reallocate
412     /// s.push('a');
413     /// ```
414     #[inline]
415     #[stable(feature = "rust1", since = "1.0.0")]
416     pub fn with_capacity(capacity: usize) -> String {
417         String { vec: Vec::with_capacity(capacity) }
418     }
419
420     // HACK(japaric): with cfg(test) the inherent `[T]::to_vec` method, which is
421     // required for this method definition, is not available. Since we don't
422     // require this method for testing purposes, I'll just stub it
423     // NB see the slice::hack module in slice.rs for more information
424     #[inline]
425     #[cfg(test)]
426     pub fn from_str(_: &str) -> String {
427         panic!("not available with cfg(test)");
428     }
429
430     /// Converts a vector of bytes to a `String`.
431     ///
432     /// A string ([`String`]) is made of bytes ([`u8`]), and a vector of bytes
433     /// ([`Vec<u8>`]) is made of bytes, so this function converts between the
434     /// two. Not all byte slices are valid `String`s, however: `String`
435     /// requires that it is valid UTF-8. `from_utf8()` checks to ensure that
436     /// the bytes are valid UTF-8, and then does the conversion.
437     ///
438     /// If you are sure that the byte slice is valid UTF-8, and you don't want
439     /// to incur the overhead of the validity check, there is an unsafe version
440     /// of this function, [`from_utf8_unchecked`], which has the same behavior
441     /// but skips the check.
442     ///
443     /// This method will take care to not copy the vector, for efficiency's
444     /// sake.
445     ///
446     /// If you need a [`&str`] instead of a `String`, consider
447     /// [`str::from_utf8`].
448     ///
449     /// The inverse of this method is [`into_bytes`].
450     ///
451     /// # Errors
452     ///
453     /// Returns [`Err`] if the slice is not UTF-8 with a description as to why the
454     /// provided bytes are not UTF-8. The vector you moved in is also included.
455     ///
456     /// # Examples
457     ///
458     /// Basic usage:
459     ///
460     /// ```
461     /// // some bytes, in a vector
462     /// let sparkle_heart = vec![240, 159, 146, 150];
463     ///
464     /// // We know these bytes are valid, so we'll use `unwrap()`.
465     /// let sparkle_heart = String::from_utf8(sparkle_heart).unwrap();
466     ///
467     /// assert_eq!("💖", sparkle_heart);
468     /// ```
469     ///
470     /// Incorrect bytes:
471     ///
472     /// ```
473     /// // some invalid bytes, in a vector
474     /// let sparkle_heart = vec![0, 159, 146, 150];
475     ///
476     /// assert!(String::from_utf8(sparkle_heart).is_err());
477     /// ```
478     ///
479     /// See the docs for [`FromUtf8Error`] for more details on what you can do
480     /// with this error.
481     ///
482     /// [`from_utf8_unchecked`]: struct.String.html#method.from_utf8_unchecked
483     /// [`String`]: struct.String.html
484     /// [`u8`]: ../../std/primitive.u8.html
485     /// [`Vec<u8>`]: ../../std/vec/struct.Vec.html
486     /// [`&str`]: ../../std/primitive.str.html
487     /// [`str::from_utf8`]: ../../std/str/fn.from_utf8.html
488     /// [`into_bytes`]: struct.String.html#method.into_bytes
489     /// [`FromUtf8Error`]: struct.FromUtf8Error.html
490     /// [`Err`]: ../../std/result/enum.Result.html#variant.Err
491     #[inline]
492     #[stable(feature = "rust1", since = "1.0.0")]
493     pub fn from_utf8(vec: Vec<u8>) -> Result<String, FromUtf8Error> {
494         match str::from_utf8(&vec) {
495             Ok(..) => Ok(String { vec }),
496             Err(e) => Err(FromUtf8Error { bytes: vec, error: e }),
497         }
498     }
499
500     /// Converts a slice of bytes to a string, including invalid characters.
501     ///
502     /// Strings are made of bytes ([`u8`]), and a slice of bytes
503     /// ([`&[u8]`][byteslice]) is made of bytes, so this function converts
504     /// between the two. Not all byte slices are valid strings, however: strings
505     /// are required to be valid UTF-8. During this conversion,
506     /// `from_utf8_lossy()` will replace any invalid UTF-8 sequences with
507     /// [`U+FFFD REPLACEMENT CHARACTER`][U+FFFD], which looks like this: �
508     ///
509     /// [`u8`]: ../../std/primitive.u8.html
510     /// [byteslice]: ../../std/primitive.slice.html
511     /// [U+FFFD]: ../char/constant.REPLACEMENT_CHARACTER.html
512     ///
513     /// If you are sure that the byte slice is valid UTF-8, and you don't want
514     /// to incur the overhead of the conversion, there is an unsafe version
515     /// of this function, [`from_utf8_unchecked`], which has the same behavior
516     /// but skips the checks.
517     ///
518     /// [`from_utf8_unchecked`]: struct.String.html#method.from_utf8_unchecked
519     ///
520     /// This function returns a [`Cow<'a, str>`]. If our byte slice is invalid
521     /// UTF-8, then we need to insert the replacement characters, which will
522     /// change the size of the string, and hence, require a `String`. But if
523     /// it's already valid UTF-8, we don't need a new allocation. This return
524     /// type allows us to handle both cases.
525     ///
526     /// [`Cow<'a, str>`]: ../../std/borrow/enum.Cow.html
527     ///
528     /// # Examples
529     ///
530     /// Basic usage:
531     ///
532     /// ```
533     /// // some bytes, in a vector
534     /// let sparkle_heart = vec![240, 159, 146, 150];
535     ///
536     /// let sparkle_heart = String::from_utf8_lossy(&sparkle_heart);
537     ///
538     /// assert_eq!("💖", sparkle_heart);
539     /// ```
540     ///
541     /// Incorrect bytes:
542     ///
543     /// ```
544     /// // some invalid bytes
545     /// let input = b"Hello \xF0\x90\x80World";
546     /// let output = String::from_utf8_lossy(input);
547     ///
548     /// assert_eq!("Hello �World", output);
549     /// ```
550     #[stable(feature = "rust1", since = "1.0.0")]
551     pub fn from_utf8_lossy(v: &[u8]) -> Cow<'_, str> {
552         let mut iter = lossy::Utf8Lossy::from_bytes(v).chunks();
553
554         let (first_valid, first_broken) = if let Some(chunk) = iter.next() {
555             let lossy::Utf8LossyChunk { valid, broken } = chunk;
556             if valid.len() == v.len() {
557                 debug_assert!(broken.is_empty());
558                 return Cow::Borrowed(valid);
559             }
560             (valid, broken)
561         } else {
562             return Cow::Borrowed("");
563         };
564
565         const REPLACEMENT: &str = "\u{FFFD}";
566
567         let mut res = String::with_capacity(v.len());
568         res.push_str(first_valid);
569         if !first_broken.is_empty() {
570             res.push_str(REPLACEMENT);
571         }
572
573         for lossy::Utf8LossyChunk { valid, broken } in iter {
574             res.push_str(valid);
575             if !broken.is_empty() {
576                 res.push_str(REPLACEMENT);
577             }
578         }
579
580         Cow::Owned(res)
581     }
582
583     /// Decode a UTF-16 encoded vector `v` into a `String`, returning [`Err`]
584     /// if `v` contains any invalid data.
585     ///
586     /// [`Err`]: ../../std/result/enum.Result.html#variant.Err
587     ///
588     /// # Examples
589     ///
590     /// Basic usage:
591     ///
592     /// ```
593     /// // 𝄞music
594     /// let v = &[0xD834, 0xDD1E, 0x006d, 0x0075,
595     ///           0x0073, 0x0069, 0x0063];
596     /// assert_eq!(String::from("𝄞music"),
597     ///            String::from_utf16(v).unwrap());
598     ///
599     /// // 𝄞mu<invalid>ic
600     /// let v = &[0xD834, 0xDD1E, 0x006d, 0x0075,
601     ///           0xD800, 0x0069, 0x0063];
602     /// assert!(String::from_utf16(v).is_err());
603     /// ```
604     #[stable(feature = "rust1", since = "1.0.0")]
605     pub fn from_utf16(v: &[u16]) -> Result<String, FromUtf16Error> {
606         // This isn't done via collect::<Result<_, _>>() for performance reasons.
607         // FIXME: the function can be simplified again when #48994 is closed.
608         let mut ret = String::with_capacity(v.len());
609         for c in decode_utf16(v.iter().cloned()) {
610             if let Ok(c) = c {
611                 ret.push(c);
612             } else {
613                 return Err(FromUtf16Error(()));
614             }
615         }
616         Ok(ret)
617     }
618
619     /// Decode a UTF-16 encoded slice `v` into a `String`, replacing
620     /// invalid data with [the replacement character (`U+FFFD`)][U+FFFD].
621     ///
622     /// Unlike [`from_utf8_lossy`] which returns a [`Cow<'a, str>`],
623     /// `from_utf16_lossy` returns a `String` since the UTF-16 to UTF-8
624     /// conversion requires a memory allocation.
625     ///
626     /// [`from_utf8_lossy`]: #method.from_utf8_lossy
627     /// [`Cow<'a, str>`]: ../borrow/enum.Cow.html
628     /// [U+FFFD]: ../char/constant.REPLACEMENT_CHARACTER.html
629     ///
630     /// # Examples
631     ///
632     /// Basic usage:
633     ///
634     /// ```
635     /// // 𝄞mus<invalid>ic<invalid>
636     /// let v = &[0xD834, 0xDD1E, 0x006d, 0x0075,
637     ///           0x0073, 0xDD1E, 0x0069, 0x0063,
638     ///           0xD834];
639     ///
640     /// assert_eq!(String::from("𝄞mus\u{FFFD}ic\u{FFFD}"),
641     ///            String::from_utf16_lossy(v));
642     /// ```
643     #[inline]
644     #[stable(feature = "rust1", since = "1.0.0")]
645     pub fn from_utf16_lossy(v: &[u16]) -> String {
646         decode_utf16(v.iter().cloned()).map(|r| r.unwrap_or(REPLACEMENT_CHARACTER)).collect()
647     }
648
649     /// Decomposes a `String` into its raw components.
650     ///
651     /// Returns the raw pointer to the underlying data, the length of
652     /// the string (in bytes), and the allocated capacity of the data
653     /// (in bytes). These are the same arguments in the same order as
654     /// the arguments to [`from_raw_parts`].
655     ///
656     /// After calling this function, the caller is responsible for the
657     /// memory previously managed by the `String`. The only way to do
658     /// this is to convert the raw pointer, length, and capacity back
659     /// into a `String` with the [`from_raw_parts`] function, allowing
660     /// the destructor to perform the cleanup.
661     ///
662     /// [`from_raw_parts`]: #method.from_raw_parts
663     ///
664     /// # Examples
665     ///
666     /// ```
667     /// #![feature(vec_into_raw_parts)]
668     /// let s = String::from("hello");
669     ///
670     /// let (ptr, len, cap) = s.into_raw_parts();
671     ///
672     /// let rebuilt = unsafe { String::from_raw_parts(ptr, len, cap) };
673     /// assert_eq!(rebuilt, "hello");
674     /// ```
675     #[unstable(feature = "vec_into_raw_parts", reason = "new API", issue = "65816")]
676     pub fn into_raw_parts(self) -> (*mut u8, usize, usize) {
677         self.vec.into_raw_parts()
678     }
679
680     /// Creates a new `String` from a length, capacity, and pointer.
681     ///
682     /// # Safety
683     ///
684     /// This is highly unsafe, due to the number of invariants that aren't
685     /// checked:
686     ///
687     /// * The memory at `ptr` needs to have been previously allocated by the
688     ///   same allocator the standard library uses, with a required alignment of exactly 1.
689     /// * `length` needs to be less than or equal to `capacity`.
690     /// * `capacity` needs to be the correct value.
691     ///
692     /// Violating these may cause problems like corrupting the allocator's
693     /// internal data structures.
694     ///
695     /// The ownership of `ptr` is effectively transferred to the
696     /// `String` which may then deallocate, reallocate or change the
697     /// contents of memory pointed to by the pointer at will. Ensure
698     /// that nothing else uses the pointer after calling this
699     /// function.
700     ///
701     /// # Examples
702     ///
703     /// Basic usage:
704     ///
705     /// ```
706     /// use std::mem;
707     ///
708     /// unsafe {
709     ///     let s = String::from("hello");
710     ///
711     // FIXME Update this when vec_into_raw_parts is stabilized
712     ///     // Prevent automatically dropping the String's data
713     ///     let mut s = mem::ManuallyDrop::new(s);
714     ///
715     ///     let ptr = s.as_mut_ptr();
716     ///     let len = s.len();
717     ///     let capacity = s.capacity();
718     ///
719     ///     let s = String::from_raw_parts(ptr, len, capacity);
720     ///
721     ///     assert_eq!(String::from("hello"), s);
722     /// }
723     /// ```
724     #[inline]
725     #[stable(feature = "rust1", since = "1.0.0")]
726     pub unsafe fn from_raw_parts(buf: *mut u8, length: usize, capacity: usize) -> String {
727         unsafe { String { vec: Vec::from_raw_parts(buf, length, capacity) } }
728     }
729
730     /// Converts a vector of bytes to a `String` without checking that the
731     /// string contains valid UTF-8.
732     ///
733     /// See the safe version, [`from_utf8`], for more details.
734     ///
735     /// [`from_utf8`]: struct.String.html#method.from_utf8
736     ///
737     /// # Safety
738     ///
739     /// This function is unsafe because it does not check that the bytes passed
740     /// to it are valid UTF-8. If this constraint is violated, it may cause
741     /// memory unsafety issues with future users of the `String`, as the rest of
742     /// the standard library assumes that `String`s are valid UTF-8.
743     ///
744     /// # Examples
745     ///
746     /// Basic usage:
747     ///
748     /// ```
749     /// // some bytes, in a vector
750     /// let sparkle_heart = vec![240, 159, 146, 150];
751     ///
752     /// let sparkle_heart = unsafe {
753     ///     String::from_utf8_unchecked(sparkle_heart)
754     /// };
755     ///
756     /// assert_eq!("💖", sparkle_heart);
757     /// ```
758     #[inline]
759     #[stable(feature = "rust1", since = "1.0.0")]
760     pub unsafe fn from_utf8_unchecked(bytes: Vec<u8>) -> String {
761         String { vec: bytes }
762     }
763
764     /// Converts a `String` into a byte vector.
765     ///
766     /// This consumes the `String`, so we do not need to copy its contents.
767     ///
768     /// # Examples
769     ///
770     /// Basic usage:
771     ///
772     /// ```
773     /// let s = String::from("hello");
774     /// let bytes = s.into_bytes();
775     ///
776     /// assert_eq!(&[104, 101, 108, 108, 111][..], &bytes[..]);
777     /// ```
778     #[inline]
779     #[stable(feature = "rust1", since = "1.0.0")]
780     pub fn into_bytes(self) -> Vec<u8> {
781         self.vec
782     }
783
784     /// Extracts a string slice containing the entire `String`.
785     ///
786     /// # Examples
787     ///
788     /// Basic usage:
789     ///
790     /// ```
791     /// let s = String::from("foo");
792     ///
793     /// assert_eq!("foo", s.as_str());
794     /// ```
795     #[inline]
796     #[stable(feature = "string_as_str", since = "1.7.0")]
797     pub fn as_str(&self) -> &str {
798         self
799     }
800
801     /// Converts a `String` into a mutable string slice.
802     ///
803     /// # Examples
804     ///
805     /// Basic usage:
806     ///
807     /// ```
808     /// let mut s = String::from("foobar");
809     /// let s_mut_str = s.as_mut_str();
810     ///
811     /// s_mut_str.make_ascii_uppercase();
812     ///
813     /// assert_eq!("FOOBAR", s_mut_str);
814     /// ```
815     #[inline]
816     #[stable(feature = "string_as_str", since = "1.7.0")]
817     pub fn as_mut_str(&mut self) -> &mut str {
818         self
819     }
820
821     /// Appends a given string slice onto the end of this `String`.
822     ///
823     /// # Examples
824     ///
825     /// Basic usage:
826     ///
827     /// ```
828     /// let mut s = String::from("foo");
829     ///
830     /// s.push_str("bar");
831     ///
832     /// assert_eq!("foobar", s);
833     /// ```
834     #[inline]
835     #[stable(feature = "rust1", since = "1.0.0")]
836     pub fn push_str(&mut self, string: &str) {
837         self.vec.extend_from_slice(string.as_bytes())
838     }
839
840     /// Returns this `String`'s capacity, in bytes.
841     ///
842     /// # Examples
843     ///
844     /// Basic usage:
845     ///
846     /// ```
847     /// let s = String::with_capacity(10);
848     ///
849     /// assert!(s.capacity() >= 10);
850     /// ```
851     #[inline]
852     #[stable(feature = "rust1", since = "1.0.0")]
853     pub fn capacity(&self) -> usize {
854         self.vec.capacity()
855     }
856
857     /// Ensures that this `String`'s capacity is at least `additional` bytes
858     /// larger than its length.
859     ///
860     /// The capacity may be increased by more than `additional` bytes if it
861     /// chooses, to prevent frequent reallocations.
862     ///
863     /// If you do not want this "at least" behavior, see the [`reserve_exact`]
864     /// method.
865     ///
866     /// # Panics
867     ///
868     /// Panics if the new capacity overflows [`usize`].
869     ///
870     /// [`reserve_exact`]: struct.String.html#method.reserve_exact
871     /// [`usize`]: ../../std/primitive.usize.html
872     ///
873     /// # Examples
874     ///
875     /// Basic usage:
876     ///
877     /// ```
878     /// let mut s = String::new();
879     ///
880     /// s.reserve(10);
881     ///
882     /// assert!(s.capacity() >= 10);
883     /// ```
884     ///
885     /// This may not actually increase the capacity:
886     ///
887     /// ```
888     /// let mut s = String::with_capacity(10);
889     /// s.push('a');
890     /// s.push('b');
891     ///
892     /// // s now has a length of 2 and a capacity of 10
893     /// assert_eq!(2, s.len());
894     /// assert_eq!(10, s.capacity());
895     ///
896     /// // Since we already have an extra 8 capacity, calling this...
897     /// s.reserve(8);
898     ///
899     /// // ... doesn't actually increase.
900     /// assert_eq!(10, s.capacity());
901     /// ```
902     #[inline]
903     #[stable(feature = "rust1", since = "1.0.0")]
904     pub fn reserve(&mut self, additional: usize) {
905         self.vec.reserve(additional)
906     }
907
908     /// Ensures that this `String`'s capacity is `additional` bytes
909     /// larger than its length.
910     ///
911     /// Consider using the [`reserve`] method unless you absolutely know
912     /// better than the allocator.
913     ///
914     /// [`reserve`]: #method.reserve
915     ///
916     /// # Panics
917     ///
918     /// Panics if the new capacity overflows `usize`.
919     ///
920     /// # Examples
921     ///
922     /// Basic usage:
923     ///
924     /// ```
925     /// let mut s = String::new();
926     ///
927     /// s.reserve_exact(10);
928     ///
929     /// assert!(s.capacity() >= 10);
930     /// ```
931     ///
932     /// This may not actually increase the capacity:
933     ///
934     /// ```
935     /// let mut s = String::with_capacity(10);
936     /// s.push('a');
937     /// s.push('b');
938     ///
939     /// // s now has a length of 2 and a capacity of 10
940     /// assert_eq!(2, s.len());
941     /// assert_eq!(10, s.capacity());
942     ///
943     /// // Since we already have an extra 8 capacity, calling this...
944     /// s.reserve_exact(8);
945     ///
946     /// // ... doesn't actually increase.
947     /// assert_eq!(10, s.capacity());
948     /// ```
949     #[inline]
950     #[stable(feature = "rust1", since = "1.0.0")]
951     pub fn reserve_exact(&mut self, additional: usize) {
952         self.vec.reserve_exact(additional)
953     }
954
955     /// Tries to reserve capacity for at least `additional` more elements to be inserted
956     /// in the given `String`. The collection may reserve more space to avoid
957     /// frequent reallocations. After calling `reserve`, capacity will be
958     /// greater than or equal to `self.len() + additional`. Does nothing if
959     /// capacity is already sufficient.
960     ///
961     /// # Errors
962     ///
963     /// If the capacity overflows, or the allocator reports a failure, then an error
964     /// is returned.
965     ///
966     /// # Examples
967     ///
968     /// ```
969     /// #![feature(try_reserve)]
970     /// use std::collections::TryReserveError;
971     ///
972     /// fn process_data(data: &str) -> Result<String, TryReserveError> {
973     ///     let mut output = String::new();
974     ///
975     ///     // Pre-reserve the memory, exiting if we can't
976     ///     output.try_reserve(data.len())?;
977     ///
978     ///     // Now we know this can't OOM in the middle of our complex work
979     ///     output.push_str(data);
980     ///
981     ///     Ok(output)
982     /// }
983     /// # process_data("rust").expect("why is the test harness OOMing on 4 bytes?");
984     /// ```
985     #[unstable(feature = "try_reserve", reason = "new API", issue = "48043")]
986     pub fn try_reserve(&mut self, additional: usize) -> Result<(), TryReserveError> {
987         self.vec.try_reserve(additional)
988     }
989
990     /// Tries to reserves the minimum capacity for exactly `additional` more elements to
991     /// be inserted in the given `String`. After calling `reserve_exact`,
992     /// capacity will be greater than or equal to `self.len() + additional`.
993     /// Does nothing if the capacity is already sufficient.
994     ///
995     /// Note that the allocator may give the collection more space than it
996     /// requests. Therefore, capacity can not be relied upon to be precisely
997     /// minimal. Prefer `reserve` if future insertions are expected.
998     ///
999     /// # Errors
1000     ///
1001     /// If the capacity overflows, or the allocator reports a failure, then an error
1002     /// is returned.
1003     ///
1004     /// # Examples
1005     ///
1006     /// ```
1007     /// #![feature(try_reserve)]
1008     /// use std::collections::TryReserveError;
1009     ///
1010     /// fn process_data(data: &str) -> Result<String, TryReserveError> {
1011     ///     let mut output = String::new();
1012     ///
1013     ///     // Pre-reserve the memory, exiting if we can't
1014     ///     output.try_reserve(data.len())?;
1015     ///
1016     ///     // Now we know this can't OOM in the middle of our complex work
1017     ///     output.push_str(data);
1018     ///
1019     ///     Ok(output)
1020     /// }
1021     /// # process_data("rust").expect("why is the test harness OOMing on 4 bytes?");
1022     /// ```
1023     #[unstable(feature = "try_reserve", reason = "new API", issue = "48043")]
1024     pub fn try_reserve_exact(&mut self, additional: usize) -> Result<(), TryReserveError> {
1025         self.vec.try_reserve_exact(additional)
1026     }
1027
1028     /// Shrinks the capacity of this `String` to match its length.
1029     ///
1030     /// # Examples
1031     ///
1032     /// Basic usage:
1033     ///
1034     /// ```
1035     /// let mut s = String::from("foo");
1036     ///
1037     /// s.reserve(100);
1038     /// assert!(s.capacity() >= 100);
1039     ///
1040     /// s.shrink_to_fit();
1041     /// assert_eq!(3, s.capacity());
1042     /// ```
1043     #[inline]
1044     #[stable(feature = "rust1", since = "1.0.0")]
1045     pub fn shrink_to_fit(&mut self) {
1046         self.vec.shrink_to_fit()
1047     }
1048
1049     /// Shrinks the capacity of this `String` with a lower bound.
1050     ///
1051     /// The capacity will remain at least as large as both the length
1052     /// and the supplied value.
1053     ///
1054     /// Panics if the current capacity is smaller than the supplied
1055     /// minimum capacity.
1056     ///
1057     /// # Examples
1058     ///
1059     /// ```
1060     /// #![feature(shrink_to)]
1061     /// let mut s = String::from("foo");
1062     ///
1063     /// s.reserve(100);
1064     /// assert!(s.capacity() >= 100);
1065     ///
1066     /// s.shrink_to(10);
1067     /// assert!(s.capacity() >= 10);
1068     /// s.shrink_to(0);
1069     /// assert!(s.capacity() >= 3);
1070     /// ```
1071     #[inline]
1072     #[unstable(feature = "shrink_to", reason = "new API", issue = "56431")]
1073     pub fn shrink_to(&mut self, min_capacity: usize) {
1074         self.vec.shrink_to(min_capacity)
1075     }
1076
1077     /// Appends the given [`char`] to the end of this `String`.
1078     ///
1079     /// [`char`]: ../../std/primitive.char.html
1080     ///
1081     /// # Examples
1082     ///
1083     /// Basic usage:
1084     ///
1085     /// ```
1086     /// let mut s = String::from("abc");
1087     ///
1088     /// s.push('1');
1089     /// s.push('2');
1090     /// s.push('3');
1091     ///
1092     /// assert_eq!("abc123", s);
1093     /// ```
1094     #[inline]
1095     #[stable(feature = "rust1", since = "1.0.0")]
1096     pub fn push(&mut self, ch: char) {
1097         match ch.len_utf8() {
1098             1 => self.vec.push(ch as u8),
1099             _ => self.vec.extend_from_slice(ch.encode_utf8(&mut [0; 4]).as_bytes()),
1100         }
1101     }
1102
1103     /// Returns a byte slice of this `String`'s contents.
1104     ///
1105     /// The inverse of this method is [`from_utf8`].
1106     ///
1107     /// [`from_utf8`]: #method.from_utf8
1108     ///
1109     /// # Examples
1110     ///
1111     /// Basic usage:
1112     ///
1113     /// ```
1114     /// let s = String::from("hello");
1115     ///
1116     /// assert_eq!(&[104, 101, 108, 108, 111], s.as_bytes());
1117     /// ```
1118     #[inline]
1119     #[stable(feature = "rust1", since = "1.0.0")]
1120     pub fn as_bytes(&self) -> &[u8] {
1121         &self.vec
1122     }
1123
1124     /// Shortens this `String` to the specified length.
1125     ///
1126     /// If `new_len` is greater than the string's current length, this has no
1127     /// effect.
1128     ///
1129     /// Note that this method has no effect on the allocated capacity
1130     /// of the string
1131     ///
1132     /// # Panics
1133     ///
1134     /// Panics if `new_len` does not lie on a [`char`] boundary.
1135     ///
1136     /// [`char`]: ../../std/primitive.char.html
1137     ///
1138     /// # Examples
1139     ///
1140     /// Basic usage:
1141     ///
1142     /// ```
1143     /// let mut s = String::from("hello");
1144     ///
1145     /// s.truncate(2);
1146     ///
1147     /// assert_eq!("he", s);
1148     /// ```
1149     #[inline]
1150     #[stable(feature = "rust1", since = "1.0.0")]
1151     pub fn truncate(&mut self, new_len: usize) {
1152         if new_len <= self.len() {
1153             assert!(self.is_char_boundary(new_len));
1154             self.vec.truncate(new_len)
1155         }
1156     }
1157
1158     /// Removes the last character from the string buffer and returns it.
1159     ///
1160     /// Returns [`None`] if this `String` is empty.
1161     ///
1162     /// [`None`]: ../../std/option/enum.Option.html#variant.None
1163     ///
1164     /// # Examples
1165     ///
1166     /// Basic usage:
1167     ///
1168     /// ```
1169     /// let mut s = String::from("foo");
1170     ///
1171     /// assert_eq!(s.pop(), Some('o'));
1172     /// assert_eq!(s.pop(), Some('o'));
1173     /// assert_eq!(s.pop(), Some('f'));
1174     ///
1175     /// assert_eq!(s.pop(), None);
1176     /// ```
1177     #[inline]
1178     #[stable(feature = "rust1", since = "1.0.0")]
1179     pub fn pop(&mut self) -> Option<char> {
1180         let ch = self.chars().rev().next()?;
1181         let newlen = self.len() - ch.len_utf8();
1182         unsafe {
1183             self.vec.set_len(newlen);
1184         }
1185         Some(ch)
1186     }
1187
1188     /// Removes a [`char`] from this `String` at a byte position and returns it.
1189     ///
1190     /// This is an `O(n)` operation, as it requires copying every element in the
1191     /// buffer.
1192     ///
1193     /// # Panics
1194     ///
1195     /// Panics if `idx` is larger than or equal to the `String`'s length,
1196     /// or if it does not lie on a [`char`] boundary.
1197     ///
1198     /// [`char`]: ../../std/primitive.char.html
1199     ///
1200     /// # Examples
1201     ///
1202     /// Basic usage:
1203     ///
1204     /// ```
1205     /// let mut s = String::from("foo");
1206     ///
1207     /// assert_eq!(s.remove(0), 'f');
1208     /// assert_eq!(s.remove(1), 'o');
1209     /// assert_eq!(s.remove(0), 'o');
1210     /// ```
1211     #[inline]
1212     #[stable(feature = "rust1", since = "1.0.0")]
1213     pub fn remove(&mut self, idx: usize) -> char {
1214         let ch = match self[idx..].chars().next() {
1215             Some(ch) => ch,
1216             None => panic!("cannot remove a char from the end of a string"),
1217         };
1218
1219         let next = idx + ch.len_utf8();
1220         let len = self.len();
1221         unsafe {
1222             ptr::copy(self.vec.as_ptr().add(next), self.vec.as_mut_ptr().add(idx), len - next);
1223             self.vec.set_len(len - (next - idx));
1224         }
1225         ch
1226     }
1227
1228     /// Retains only the characters specified by the predicate.
1229     ///
1230     /// In other words, remove all characters `c` such that `f(c)` returns `false`.
1231     /// This method operates in place, visiting each character exactly once in the
1232     /// original order, and preserves the order of the retained characters.
1233     ///
1234     /// # Examples
1235     ///
1236     /// ```
1237     /// let mut s = String::from("f_o_ob_ar");
1238     ///
1239     /// s.retain(|c| c != '_');
1240     ///
1241     /// assert_eq!(s, "foobar");
1242     /// ```
1243     ///
1244     /// The exact order may be useful for tracking external state, like an index.
1245     ///
1246     /// ```
1247     /// let mut s = String::from("abcde");
1248     /// let keep = [false, true, true, false, true];
1249     /// let mut i = 0;
1250     /// s.retain(|_| (keep[i], i += 1).0);
1251     /// assert_eq!(s, "bce");
1252     /// ```
1253     #[inline]
1254     #[stable(feature = "string_retain", since = "1.26.0")]
1255     pub fn retain<F>(&mut self, mut f: F)
1256     where
1257         F: FnMut(char) -> bool,
1258     {
1259         let len = self.len();
1260         let mut del_bytes = 0;
1261         let mut idx = 0;
1262
1263         while idx < len {
1264             let ch = unsafe { self.get_unchecked(idx..len).chars().next().unwrap() };
1265             let ch_len = ch.len_utf8();
1266
1267             if !f(ch) {
1268                 del_bytes += ch_len;
1269             } else if del_bytes > 0 {
1270                 unsafe {
1271                     ptr::copy(
1272                         self.vec.as_ptr().add(idx),
1273                         self.vec.as_mut_ptr().add(idx - del_bytes),
1274                         ch_len,
1275                     );
1276                 }
1277             }
1278
1279             // Point idx to the next char
1280             idx += ch_len;
1281         }
1282
1283         if del_bytes > 0 {
1284             unsafe {
1285                 self.vec.set_len(len - del_bytes);
1286             }
1287         }
1288     }
1289
1290     /// Inserts a character into this `String` at a byte position.
1291     ///
1292     /// This is an `O(n)` operation as it requires copying every element in the
1293     /// buffer.
1294     ///
1295     /// # Panics
1296     ///
1297     /// Panics if `idx` is larger than the `String`'s length, or if it does not
1298     /// lie on a [`char`] boundary.
1299     ///
1300     /// [`char`]: ../../std/primitive.char.html
1301     ///
1302     /// # Examples
1303     ///
1304     /// Basic usage:
1305     ///
1306     /// ```
1307     /// let mut s = String::with_capacity(3);
1308     ///
1309     /// s.insert(0, 'f');
1310     /// s.insert(1, 'o');
1311     /// s.insert(2, 'o');
1312     ///
1313     /// assert_eq!("foo", s);
1314     /// ```
1315     #[inline]
1316     #[stable(feature = "rust1", since = "1.0.0")]
1317     pub fn insert(&mut self, idx: usize, ch: char) {
1318         assert!(self.is_char_boundary(idx));
1319         let mut bits = [0; 4];
1320         let bits = ch.encode_utf8(&mut bits).as_bytes();
1321
1322         unsafe {
1323             self.insert_bytes(idx, bits);
1324         }
1325     }
1326
1327     unsafe fn insert_bytes(&mut self, idx: usize, bytes: &[u8]) {
1328         let len = self.len();
1329         let amt = bytes.len();
1330         self.vec.reserve(amt);
1331
1332         unsafe {
1333             ptr::copy(self.vec.as_ptr().add(idx), self.vec.as_mut_ptr().add(idx + amt), len - idx);
1334             ptr::copy(bytes.as_ptr(), self.vec.as_mut_ptr().add(idx), amt);
1335             self.vec.set_len(len + amt);
1336         }
1337     }
1338
1339     /// Inserts a string slice into this `String` at a byte position.
1340     ///
1341     /// This is an `O(n)` operation as it requires copying every element in the
1342     /// buffer.
1343     ///
1344     /// # Panics
1345     ///
1346     /// Panics if `idx` is larger than the `String`'s length, or if it does not
1347     /// lie on a [`char`] boundary.
1348     ///
1349     /// [`char`]: ../../std/primitive.char.html
1350     ///
1351     /// # Examples
1352     ///
1353     /// Basic usage:
1354     ///
1355     /// ```
1356     /// let mut s = String::from("bar");
1357     ///
1358     /// s.insert_str(0, "foo");
1359     ///
1360     /// assert_eq!("foobar", s);
1361     /// ```
1362     #[inline]
1363     #[stable(feature = "insert_str", since = "1.16.0")]
1364     pub fn insert_str(&mut self, idx: usize, string: &str) {
1365         assert!(self.is_char_boundary(idx));
1366
1367         unsafe {
1368             self.insert_bytes(idx, string.as_bytes());
1369         }
1370     }
1371
1372     /// Returns a mutable reference to the contents of this `String`.
1373     ///
1374     /// # Safety
1375     ///
1376     /// This function is unsafe because it does not check that the bytes passed
1377     /// to it are valid UTF-8. If this constraint is violated, it may cause
1378     /// memory unsafety issues with future users of the `String`, as the rest of
1379     /// the standard library assumes that `String`s are valid UTF-8.
1380     ///
1381     /// # Examples
1382     ///
1383     /// Basic usage:
1384     ///
1385     /// ```
1386     /// let mut s = String::from("hello");
1387     ///
1388     /// unsafe {
1389     ///     let vec = s.as_mut_vec();
1390     ///     assert_eq!(&[104, 101, 108, 108, 111][..], &vec[..]);
1391     ///
1392     ///     vec.reverse();
1393     /// }
1394     /// assert_eq!(s, "olleh");
1395     /// ```
1396     #[inline]
1397     #[stable(feature = "rust1", since = "1.0.0")]
1398     pub unsafe fn as_mut_vec(&mut self) -> &mut Vec<u8> {
1399         &mut self.vec
1400     }
1401
1402     /// Returns the length of this `String`, in bytes, not [`char`]s or
1403     /// graphemes. In other words, it may not be what a human considers the
1404     /// length of the string.
1405     ///
1406     /// # Examples
1407     ///
1408     /// Basic usage:
1409     ///
1410     /// ```
1411     /// let a = String::from("foo");
1412     /// assert_eq!(a.len(), 3);
1413     ///
1414     /// let fancy_f = String::from("ƒoo");
1415     /// assert_eq!(fancy_f.len(), 4);
1416     /// assert_eq!(fancy_f.chars().count(), 3);
1417     /// ```
1418     #[inline]
1419     #[stable(feature = "rust1", since = "1.0.0")]
1420     pub fn len(&self) -> usize {
1421         self.vec.len()
1422     }
1423
1424     /// Returns `true` if this `String` has a length of zero, and `false` otherwise.
1425     ///
1426     /// # Examples
1427     ///
1428     /// Basic usage:
1429     ///
1430     /// ```
1431     /// let mut v = String::new();
1432     /// assert!(v.is_empty());
1433     ///
1434     /// v.push('a');
1435     /// assert!(!v.is_empty());
1436     /// ```
1437     #[inline]
1438     #[stable(feature = "rust1", since = "1.0.0")]
1439     pub fn is_empty(&self) -> bool {
1440         self.len() == 0
1441     }
1442
1443     /// Splits the string into two at the given index.
1444     ///
1445     /// Returns a newly allocated `String`. `self` contains bytes `[0, at)`, and
1446     /// the returned `String` contains bytes `[at, len)`. `at` must be on the
1447     /// boundary of a UTF-8 code point.
1448     ///
1449     /// Note that the capacity of `self` does not change.
1450     ///
1451     /// # Panics
1452     ///
1453     /// Panics if `at` is not on a `UTF-8` code point boundary, or if it is beyond the last
1454     /// code point of the string.
1455     ///
1456     /// # Examples
1457     ///
1458     /// ```
1459     /// # fn main() {
1460     /// let mut hello = String::from("Hello, World!");
1461     /// let world = hello.split_off(7);
1462     /// assert_eq!(hello, "Hello, ");
1463     /// assert_eq!(world, "World!");
1464     /// # }
1465     /// ```
1466     #[inline]
1467     #[stable(feature = "string_split_off", since = "1.16.0")]
1468     #[must_use = "use `.truncate()` if you don't need the other half"]
1469     pub fn split_off(&mut self, at: usize) -> String {
1470         assert!(self.is_char_boundary(at));
1471         let other = self.vec.split_off(at);
1472         unsafe { String::from_utf8_unchecked(other) }
1473     }
1474
1475     /// Truncates this `String`, removing all contents.
1476     ///
1477     /// While this means the `String` will have a length of zero, it does not
1478     /// touch its capacity.
1479     ///
1480     /// # Examples
1481     ///
1482     /// Basic usage:
1483     ///
1484     /// ```
1485     /// let mut s = String::from("foo");
1486     ///
1487     /// s.clear();
1488     ///
1489     /// assert!(s.is_empty());
1490     /// assert_eq!(0, s.len());
1491     /// assert_eq!(3, s.capacity());
1492     /// ```
1493     #[inline]
1494     #[stable(feature = "rust1", since = "1.0.0")]
1495     pub fn clear(&mut self) {
1496         self.vec.clear()
1497     }
1498
1499     /// Creates a draining iterator that removes the specified range in the `String`
1500     /// and yields the removed `chars`.
1501     ///
1502     /// Note: The element range is removed even if the iterator is not
1503     /// consumed until the end.
1504     ///
1505     /// # Panics
1506     ///
1507     /// Panics if the starting point or end point do not lie on a [`char`]
1508     /// boundary, or if they're out of bounds.
1509     ///
1510     /// [`char`]: ../../std/primitive.char.html
1511     ///
1512     /// # Examples
1513     ///
1514     /// Basic usage:
1515     ///
1516     /// ```
1517     /// let mut s = String::from("α is alpha, β is beta");
1518     /// let beta_offset = s.find('β').unwrap_or(s.len());
1519     ///
1520     /// // Remove the range up until the β from the string
1521     /// let t: String = s.drain(..beta_offset).collect();
1522     /// assert_eq!(t, "α is alpha, ");
1523     /// assert_eq!(s, "β is beta");
1524     ///
1525     /// // A full range clears the string
1526     /// s.drain(..);
1527     /// assert_eq!(s, "");
1528     /// ```
1529     #[stable(feature = "drain", since = "1.6.0")]
1530     pub fn drain<R>(&mut self, range: R) -> Drain<'_>
1531     where
1532         R: RangeBounds<usize>,
1533     {
1534         // Memory safety
1535         //
1536         // The String version of Drain does not have the memory safety issues
1537         // of the vector version. The data is just plain bytes.
1538         // Because the range removal happens in Drop, if the Drain iterator is leaked,
1539         // the removal will not happen.
1540         let len = self.len();
1541         let start = match range.start_bound() {
1542             Included(&n) => n,
1543             Excluded(&n) => n + 1,
1544             Unbounded => 0,
1545         };
1546         let end = match range.end_bound() {
1547             Included(&n) => n + 1,
1548             Excluded(&n) => n,
1549             Unbounded => len,
1550         };
1551
1552         // Take out two simultaneous borrows. The &mut String won't be accessed
1553         // until iteration is over, in Drop.
1554         let self_ptr = self as *mut _;
1555         // slicing does the appropriate bounds checks
1556         let chars_iter = self[start..end].chars();
1557
1558         Drain { start, end, iter: chars_iter, string: self_ptr }
1559     }
1560
1561     /// Removes the specified range in the string,
1562     /// and replaces it with the given string.
1563     /// The given string doesn't need to be the same length as the range.
1564     ///
1565     /// # Panics
1566     ///
1567     /// Panics if the starting point or end point do not lie on a [`char`]
1568     /// boundary, or if they're out of bounds.
1569     ///
1570     /// [`char`]: ../../std/primitive.char.html
1571     /// [`Vec::splice`]: ../../std/vec/struct.Vec.html#method.splice
1572     ///
1573     /// # Examples
1574     ///
1575     /// Basic usage:
1576     ///
1577     /// ```
1578     /// let mut s = String::from("α is alpha, β is beta");
1579     /// let beta_offset = s.find('β').unwrap_or(s.len());
1580     ///
1581     /// // Replace the range up until the β from the string
1582     /// s.replace_range(..beta_offset, "Α is capital alpha; ");
1583     /// assert_eq!(s, "Α is capital alpha; β is beta");
1584     /// ```
1585     #[stable(feature = "splice", since = "1.27.0")]
1586     pub fn replace_range<R>(&mut self, range: R, replace_with: &str)
1587     where
1588         R: RangeBounds<usize>,
1589     {
1590         // Memory safety
1591         //
1592         // Replace_range does not have the memory safety issues of a vector Splice.
1593         // of the vector version. The data is just plain bytes.
1594
1595         match range.start_bound() {
1596             Included(&n) => assert!(self.is_char_boundary(n)),
1597             Excluded(&n) => assert!(self.is_char_boundary(n + 1)),
1598             Unbounded => {}
1599         };
1600         match range.end_bound() {
1601             Included(&n) => assert!(self.is_char_boundary(n + 1)),
1602             Excluded(&n) => assert!(self.is_char_boundary(n)),
1603             Unbounded => {}
1604         };
1605
1606         unsafe { self.as_mut_vec() }.splice(range, replace_with.bytes());
1607     }
1608
1609     /// Converts this `String` into a [`Box`]`<`[`str`]`>`.
1610     ///
1611     /// This will drop any excess capacity.
1612     ///
1613     /// [`Box`]: ../../std/boxed/struct.Box.html
1614     /// [`str`]: ../../std/primitive.str.html
1615     ///
1616     /// # Examples
1617     ///
1618     /// Basic usage:
1619     ///
1620     /// ```
1621     /// let s = String::from("hello");
1622     ///
1623     /// let b = s.into_boxed_str();
1624     /// ```
1625     #[stable(feature = "box_str", since = "1.4.0")]
1626     #[inline]
1627     pub fn into_boxed_str(self) -> Box<str> {
1628         let slice = self.vec.into_boxed_slice();
1629         unsafe { from_boxed_utf8_unchecked(slice) }
1630     }
1631 }
1632
1633 impl FromUtf8Error {
1634     /// Returns a slice of [`u8`]s bytes that were attempted to convert to a `String`.
1635     ///
1636     /// # Examples
1637     ///
1638     /// Basic usage:
1639     ///
1640     /// ```
1641     /// // some invalid bytes, in a vector
1642     /// let bytes = vec![0, 159];
1643     ///
1644     /// let value = String::from_utf8(bytes);
1645     ///
1646     /// assert_eq!(&[0, 159], value.unwrap_err().as_bytes());
1647     /// ```
1648     #[stable(feature = "from_utf8_error_as_bytes", since = "1.26.0")]
1649     pub fn as_bytes(&self) -> &[u8] {
1650         &self.bytes[..]
1651     }
1652
1653     /// Returns the bytes that were attempted to convert to a `String`.
1654     ///
1655     /// This method is carefully constructed to avoid allocation. It will
1656     /// consume the error, moving out the bytes, so that a copy of the bytes
1657     /// does not need to be made.
1658     ///
1659     /// # Examples
1660     ///
1661     /// Basic usage:
1662     ///
1663     /// ```
1664     /// // some invalid bytes, in a vector
1665     /// let bytes = vec![0, 159];
1666     ///
1667     /// let value = String::from_utf8(bytes);
1668     ///
1669     /// assert_eq!(vec![0, 159], value.unwrap_err().into_bytes());
1670     /// ```
1671     #[stable(feature = "rust1", since = "1.0.0")]
1672     pub fn into_bytes(self) -> Vec<u8> {
1673         self.bytes
1674     }
1675
1676     /// Fetch a `Utf8Error` to get more details about the conversion failure.
1677     ///
1678     /// The [`Utf8Error`] type provided by [`std::str`] represents an error that may
1679     /// occur when converting a slice of [`u8`]s to a [`&str`]. In this sense, it's
1680     /// an analogue to `FromUtf8Error`. See its documentation for more details
1681     /// on using it.
1682     ///
1683     /// [`Utf8Error`]: ../../std/str/struct.Utf8Error.html
1684     /// [`std::str`]: ../../std/str/index.html
1685     /// [`u8`]: ../../std/primitive.u8.html
1686     /// [`&str`]: ../../std/primitive.str.html
1687     ///
1688     /// # Examples
1689     ///
1690     /// Basic usage:
1691     ///
1692     /// ```
1693     /// // some invalid bytes, in a vector
1694     /// let bytes = vec![0, 159];
1695     ///
1696     /// let error = String::from_utf8(bytes).unwrap_err().utf8_error();
1697     ///
1698     /// // the first byte is invalid here
1699     /// assert_eq!(1, error.valid_up_to());
1700     /// ```
1701     #[stable(feature = "rust1", since = "1.0.0")]
1702     pub fn utf8_error(&self) -> Utf8Error {
1703         self.error
1704     }
1705 }
1706
1707 #[stable(feature = "rust1", since = "1.0.0")]
1708 impl fmt::Display for FromUtf8Error {
1709     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1710         fmt::Display::fmt(&self.error, f)
1711     }
1712 }
1713
1714 #[stable(feature = "rust1", since = "1.0.0")]
1715 impl fmt::Display for FromUtf16Error {
1716     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1717         fmt::Display::fmt("invalid utf-16: lone surrogate found", f)
1718     }
1719 }
1720
1721 #[stable(feature = "rust1", since = "1.0.0")]
1722 impl Clone for String {
1723     fn clone(&self) -> Self {
1724         String { vec: self.vec.clone() }
1725     }
1726
1727     fn clone_from(&mut self, source: &Self) {
1728         self.vec.clone_from(&source.vec);
1729     }
1730 }
1731
1732 #[stable(feature = "rust1", since = "1.0.0")]
1733 impl FromIterator<char> for String {
1734     fn from_iter<I: IntoIterator<Item = char>>(iter: I) -> String {
1735         let mut buf = String::new();
1736         buf.extend(iter);
1737         buf
1738     }
1739 }
1740
1741 #[stable(feature = "string_from_iter_by_ref", since = "1.17.0")]
1742 impl<'a> FromIterator<&'a char> for String {
1743     fn from_iter<I: IntoIterator<Item = &'a char>>(iter: I) -> String {
1744         let mut buf = String::new();
1745         buf.extend(iter);
1746         buf
1747     }
1748 }
1749
1750 #[stable(feature = "rust1", since = "1.0.0")]
1751 impl<'a> FromIterator<&'a str> for String {
1752     fn from_iter<I: IntoIterator<Item = &'a str>>(iter: I) -> String {
1753         let mut buf = String::new();
1754         buf.extend(iter);
1755         buf
1756     }
1757 }
1758
1759 #[stable(feature = "extend_string", since = "1.4.0")]
1760 impl FromIterator<String> for String {
1761     fn from_iter<I: IntoIterator<Item = String>>(iter: I) -> String {
1762         let mut iterator = iter.into_iter();
1763
1764         // Because we're iterating over `String`s, we can avoid at least
1765         // one allocation by getting the first string from the iterator
1766         // and appending to it all the subsequent strings.
1767         match iterator.next() {
1768             None => String::new(),
1769             Some(mut buf) => {
1770                 buf.extend(iterator);
1771                 buf
1772             }
1773         }
1774     }
1775 }
1776
1777 #[stable(feature = "box_str2", since = "1.45.0")]
1778 impl FromIterator<Box<str>> for String {
1779     fn from_iter<I: IntoIterator<Item = Box<str>>>(iter: I) -> String {
1780         let mut buf = String::new();
1781         buf.extend(iter);
1782         buf
1783     }
1784 }
1785
1786 #[stable(feature = "herd_cows", since = "1.19.0")]
1787 impl<'a> FromIterator<Cow<'a, str>> for String {
1788     fn from_iter<I: IntoIterator<Item = Cow<'a, str>>>(iter: I) -> String {
1789         let mut iterator = iter.into_iter();
1790
1791         // Because we're iterating over CoWs, we can (potentially) avoid at least
1792         // one allocation by getting the first item and appending to it all the
1793         // subsequent items.
1794         match iterator.next() {
1795             None => String::new(),
1796             Some(cow) => {
1797                 let mut buf = cow.into_owned();
1798                 buf.extend(iterator);
1799                 buf
1800             }
1801         }
1802     }
1803 }
1804
1805 #[stable(feature = "rust1", since = "1.0.0")]
1806 impl Extend<char> for String {
1807     fn extend<I: IntoIterator<Item = char>>(&mut self, iter: I) {
1808         let iterator = iter.into_iter();
1809         let (lower_bound, _) = iterator.size_hint();
1810         self.reserve(lower_bound);
1811         iterator.for_each(move |c| self.push(c));
1812     }
1813
1814     #[inline]
1815     fn extend_one(&mut self, c: char) {
1816         self.push(c);
1817     }
1818
1819     #[inline]
1820     fn extend_reserve(&mut self, additional: usize) {
1821         self.reserve(additional);
1822     }
1823 }
1824
1825 #[stable(feature = "extend_ref", since = "1.2.0")]
1826 impl<'a> Extend<&'a char> for String {
1827     fn extend<I: IntoIterator<Item = &'a char>>(&mut self, iter: I) {
1828         self.extend(iter.into_iter().cloned());
1829     }
1830
1831     #[inline]
1832     fn extend_one(&mut self, &c: &'a char) {
1833         self.push(c);
1834     }
1835
1836     #[inline]
1837     fn extend_reserve(&mut self, additional: usize) {
1838         self.reserve(additional);
1839     }
1840 }
1841
1842 #[stable(feature = "rust1", since = "1.0.0")]
1843 impl<'a> Extend<&'a str> for String {
1844     fn extend<I: IntoIterator<Item = &'a str>>(&mut self, iter: I) {
1845         iter.into_iter().for_each(move |s| self.push_str(s));
1846     }
1847
1848     #[inline]
1849     fn extend_one(&mut self, s: &'a str) {
1850         self.push_str(s);
1851     }
1852 }
1853
1854 #[stable(feature = "box_str2", since = "1.45.0")]
1855 impl Extend<Box<str>> for String {
1856     fn extend<I: IntoIterator<Item = Box<str>>>(&mut self, iter: I) {
1857         iter.into_iter().for_each(move |s| self.push_str(&s));
1858     }
1859 }
1860
1861 #[stable(feature = "extend_string", since = "1.4.0")]
1862 impl Extend<String> for String {
1863     fn extend<I: IntoIterator<Item = String>>(&mut self, iter: I) {
1864         iter.into_iter().for_each(move |s| self.push_str(&s));
1865     }
1866
1867     #[inline]
1868     fn extend_one(&mut self, s: String) {
1869         self.push_str(&s);
1870     }
1871 }
1872
1873 #[stable(feature = "herd_cows", since = "1.19.0")]
1874 impl<'a> Extend<Cow<'a, str>> for String {
1875     fn extend<I: IntoIterator<Item = Cow<'a, str>>>(&mut self, iter: I) {
1876         iter.into_iter().for_each(move |s| self.push_str(&s));
1877     }
1878
1879     #[inline]
1880     fn extend_one(&mut self, s: Cow<'a, str>) {
1881         self.push_str(&s);
1882     }
1883 }
1884
1885 /// A convenience impl that delegates to the impl for `&str`.
1886 ///
1887 /// # Examples
1888 ///
1889 /// ```
1890 /// assert_eq!(String::from("Hello world").find("world"), Some(6));
1891 /// ```
1892 #[unstable(
1893     feature = "pattern",
1894     reason = "API not fully fleshed out and ready to be stabilized",
1895     issue = "27721"
1896 )]
1897 impl<'a, 'b> Pattern<'a> for &'b String {
1898     type Searcher = <&'b str as Pattern<'a>>::Searcher;
1899
1900     fn into_searcher(self, haystack: &'a str) -> <&'b str as Pattern<'a>>::Searcher {
1901         self[..].into_searcher(haystack)
1902     }
1903
1904     #[inline]
1905     fn is_contained_in(self, haystack: &'a str) -> bool {
1906         self[..].is_contained_in(haystack)
1907     }
1908
1909     #[inline]
1910     fn is_prefix_of(self, haystack: &'a str) -> bool {
1911         self[..].is_prefix_of(haystack)
1912     }
1913
1914     #[inline]
1915     fn strip_prefix_of(self, haystack: &'a str) -> Option<&'a str> {
1916         self[..].strip_prefix_of(haystack)
1917     }
1918
1919     #[inline]
1920     fn is_suffix_of(self, haystack: &'a str) -> bool {
1921         self[..].is_suffix_of(haystack)
1922     }
1923
1924     #[inline]
1925     fn strip_suffix_of(self, haystack: &'a str) -> Option<&'a str> {
1926         self[..].strip_suffix_of(haystack)
1927     }
1928 }
1929
1930 #[stable(feature = "rust1", since = "1.0.0")]
1931 impl PartialEq for String {
1932     #[inline]
1933     fn eq(&self, other: &String) -> bool {
1934         PartialEq::eq(&self[..], &other[..])
1935     }
1936     #[inline]
1937     fn ne(&self, other: &String) -> bool {
1938         PartialEq::ne(&self[..], &other[..])
1939     }
1940 }
1941
1942 macro_rules! impl_eq {
1943     ($lhs:ty, $rhs: ty) => {
1944         #[stable(feature = "rust1", since = "1.0.0")]
1945         #[allow(unused_lifetimes)]
1946         impl<'a, 'b> PartialEq<$rhs> for $lhs {
1947             #[inline]
1948             fn eq(&self, other: &$rhs) -> bool {
1949                 PartialEq::eq(&self[..], &other[..])
1950             }
1951             #[inline]
1952             fn ne(&self, other: &$rhs) -> bool {
1953                 PartialEq::ne(&self[..], &other[..])
1954             }
1955         }
1956
1957         #[stable(feature = "rust1", since = "1.0.0")]
1958         #[allow(unused_lifetimes)]
1959         impl<'a, 'b> PartialEq<$lhs> for $rhs {
1960             #[inline]
1961             fn eq(&self, other: &$lhs) -> bool {
1962                 PartialEq::eq(&self[..], &other[..])
1963             }
1964             #[inline]
1965             fn ne(&self, other: &$lhs) -> bool {
1966                 PartialEq::ne(&self[..], &other[..])
1967             }
1968         }
1969     };
1970 }
1971
1972 impl_eq! { String, str }
1973 impl_eq! { String, &'a str }
1974 impl_eq! { Cow<'a, str>, str }
1975 impl_eq! { Cow<'a, str>, &'b str }
1976 impl_eq! { Cow<'a, str>, String }
1977
1978 #[stable(feature = "rust1", since = "1.0.0")]
1979 impl Default for String {
1980     /// Creates an empty `String`.
1981     #[inline]
1982     fn default() -> String {
1983         String::new()
1984     }
1985 }
1986
1987 #[stable(feature = "rust1", since = "1.0.0")]
1988 impl fmt::Display for String {
1989     #[inline]
1990     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1991         fmt::Display::fmt(&**self, f)
1992     }
1993 }
1994
1995 #[stable(feature = "rust1", since = "1.0.0")]
1996 impl fmt::Debug for String {
1997     #[inline]
1998     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1999         fmt::Debug::fmt(&**self, f)
2000     }
2001 }
2002
2003 #[stable(feature = "rust1", since = "1.0.0")]
2004 impl hash::Hash for String {
2005     #[inline]
2006     fn hash<H: hash::Hasher>(&self, hasher: &mut H) {
2007         (**self).hash(hasher)
2008     }
2009 }
2010
2011 /// Implements the `+` operator for concatenating two strings.
2012 ///
2013 /// This consumes the `String` on the left-hand side and re-uses its buffer (growing it if
2014 /// necessary). This is done to avoid allocating a new `String` and copying the entire contents on
2015 /// every operation, which would lead to `O(n^2)` running time when building an `n`-byte string by
2016 /// repeated concatenation.
2017 ///
2018 /// The string on the right-hand side is only borrowed; its contents are copied into the returned
2019 /// `String`.
2020 ///
2021 /// # Examples
2022 ///
2023 /// Concatenating two `String`s takes the first by value and borrows the second:
2024 ///
2025 /// ```
2026 /// let a = String::from("hello");
2027 /// let b = String::from(" world");
2028 /// let c = a + &b;
2029 /// // `a` is moved and can no longer be used here.
2030 /// ```
2031 ///
2032 /// If you want to keep using the first `String`, you can clone it and append to the clone instead:
2033 ///
2034 /// ```
2035 /// let a = String::from("hello");
2036 /// let b = String::from(" world");
2037 /// let c = a.clone() + &b;
2038 /// // `a` is still valid here.
2039 /// ```
2040 ///
2041 /// Concatenating `&str` slices can be done by converting the first to a `String`:
2042 ///
2043 /// ```
2044 /// let a = "hello";
2045 /// let b = " world";
2046 /// let c = a.to_string() + b;
2047 /// ```
2048 #[stable(feature = "rust1", since = "1.0.0")]
2049 impl Add<&str> for String {
2050     type Output = String;
2051
2052     #[inline]
2053     fn add(mut self, other: &str) -> String {
2054         self.push_str(other);
2055         self
2056     }
2057 }
2058
2059 /// Implements the `+=` operator for appending to a `String`.
2060 ///
2061 /// This has the same behavior as the [`push_str`][String::push_str] method.
2062 #[stable(feature = "stringaddassign", since = "1.12.0")]
2063 impl AddAssign<&str> for String {
2064     #[inline]
2065     fn add_assign(&mut self, other: &str) {
2066         self.push_str(other);
2067     }
2068 }
2069
2070 #[stable(feature = "rust1", since = "1.0.0")]
2071 impl ops::Index<ops::Range<usize>> for String {
2072     type Output = str;
2073
2074     #[inline]
2075     fn index(&self, index: ops::Range<usize>) -> &str {
2076         &self[..][index]
2077     }
2078 }
2079 #[stable(feature = "rust1", since = "1.0.0")]
2080 impl ops::Index<ops::RangeTo<usize>> for String {
2081     type Output = str;
2082
2083     #[inline]
2084     fn index(&self, index: ops::RangeTo<usize>) -> &str {
2085         &self[..][index]
2086     }
2087 }
2088 #[stable(feature = "rust1", since = "1.0.0")]
2089 impl ops::Index<ops::RangeFrom<usize>> for String {
2090     type Output = str;
2091
2092     #[inline]
2093     fn index(&self, index: ops::RangeFrom<usize>) -> &str {
2094         &self[..][index]
2095     }
2096 }
2097 #[stable(feature = "rust1", since = "1.0.0")]
2098 impl ops::Index<ops::RangeFull> for String {
2099     type Output = str;
2100
2101     #[inline]
2102     fn index(&self, _index: ops::RangeFull) -> &str {
2103         unsafe { str::from_utf8_unchecked(&self.vec) }
2104     }
2105 }
2106 #[stable(feature = "inclusive_range", since = "1.26.0")]
2107 impl ops::Index<ops::RangeInclusive<usize>> for String {
2108     type Output = str;
2109
2110     #[inline]
2111     fn index(&self, index: ops::RangeInclusive<usize>) -> &str {
2112         Index::index(&**self, index)
2113     }
2114 }
2115 #[stable(feature = "inclusive_range", since = "1.26.0")]
2116 impl ops::Index<ops::RangeToInclusive<usize>> for String {
2117     type Output = str;
2118
2119     #[inline]
2120     fn index(&self, index: ops::RangeToInclusive<usize>) -> &str {
2121         Index::index(&**self, index)
2122     }
2123 }
2124
2125 #[stable(feature = "derefmut_for_string", since = "1.3.0")]
2126 impl ops::IndexMut<ops::Range<usize>> for String {
2127     #[inline]
2128     fn index_mut(&mut self, index: ops::Range<usize>) -> &mut str {
2129         &mut self[..][index]
2130     }
2131 }
2132 #[stable(feature = "derefmut_for_string", since = "1.3.0")]
2133 impl ops::IndexMut<ops::RangeTo<usize>> for String {
2134     #[inline]
2135     fn index_mut(&mut self, index: ops::RangeTo<usize>) -> &mut str {
2136         &mut self[..][index]
2137     }
2138 }
2139 #[stable(feature = "derefmut_for_string", since = "1.3.0")]
2140 impl ops::IndexMut<ops::RangeFrom<usize>> for String {
2141     #[inline]
2142     fn index_mut(&mut self, index: ops::RangeFrom<usize>) -> &mut str {
2143         &mut self[..][index]
2144     }
2145 }
2146 #[stable(feature = "derefmut_for_string", since = "1.3.0")]
2147 impl ops::IndexMut<ops::RangeFull> for String {
2148     #[inline]
2149     fn index_mut(&mut self, _index: ops::RangeFull) -> &mut str {
2150         unsafe { str::from_utf8_unchecked_mut(&mut *self.vec) }
2151     }
2152 }
2153 #[stable(feature = "inclusive_range", since = "1.26.0")]
2154 impl ops::IndexMut<ops::RangeInclusive<usize>> for String {
2155     #[inline]
2156     fn index_mut(&mut self, index: ops::RangeInclusive<usize>) -> &mut str {
2157         IndexMut::index_mut(&mut **self, index)
2158     }
2159 }
2160 #[stable(feature = "inclusive_range", since = "1.26.0")]
2161 impl ops::IndexMut<ops::RangeToInclusive<usize>> for String {
2162     #[inline]
2163     fn index_mut(&mut self, index: ops::RangeToInclusive<usize>) -> &mut str {
2164         IndexMut::index_mut(&mut **self, index)
2165     }
2166 }
2167
2168 #[stable(feature = "rust1", since = "1.0.0")]
2169 impl ops::Deref for String {
2170     type Target = str;
2171
2172     #[inline]
2173     fn deref(&self) -> &str {
2174         unsafe { str::from_utf8_unchecked(&self.vec) }
2175     }
2176 }
2177
2178 #[stable(feature = "derefmut_for_string", since = "1.3.0")]
2179 impl ops::DerefMut for String {
2180     #[inline]
2181     fn deref_mut(&mut self) -> &mut str {
2182         unsafe { str::from_utf8_unchecked_mut(&mut *self.vec) }
2183     }
2184 }
2185
2186 /// A type alias for [`Infallible`].
2187 ///
2188 /// This alias exists for backwards compatibility, and may be eventually deprecated.
2189 ///
2190 /// [`Infallible`]: ../../core/convert/enum.Infallible.html
2191 #[stable(feature = "str_parse_error", since = "1.5.0")]
2192 pub type ParseError = core::convert::Infallible;
2193
2194 #[stable(feature = "rust1", since = "1.0.0")]
2195 impl FromStr for String {
2196     type Err = core::convert::Infallible;
2197     #[inline]
2198     fn from_str(s: &str) -> Result<String, Self::Err> {
2199         Ok(String::from(s))
2200     }
2201 }
2202
2203 /// A trait for converting a value to a `String`.
2204 ///
2205 /// This trait is automatically implemented for any type which implements the
2206 /// [`Display`] trait. As such, `ToString` shouldn't be implemented directly:
2207 /// [`Display`] should be implemented instead, and you get the `ToString`
2208 /// implementation for free.
2209 ///
2210 /// [`Display`]: ../../std/fmt/trait.Display.html
2211 #[stable(feature = "rust1", since = "1.0.0")]
2212 pub trait ToString {
2213     /// Converts the given value to a `String`.
2214     ///
2215     /// # Examples
2216     ///
2217     /// Basic usage:
2218     ///
2219     /// ```
2220     /// let i = 5;
2221     /// let five = String::from("5");
2222     ///
2223     /// assert_eq!(five, i.to_string());
2224     /// ```
2225     #[rustc_conversion_suggestion]
2226     #[stable(feature = "rust1", since = "1.0.0")]
2227     fn to_string(&self) -> String;
2228 }
2229
2230 /// # Panics
2231 ///
2232 /// In this implementation, the `to_string` method panics
2233 /// if the `Display` implementation returns an error.
2234 /// This indicates an incorrect `Display` implementation
2235 /// since `fmt::Write for String` never returns an error itself.
2236 #[stable(feature = "rust1", since = "1.0.0")]
2237 impl<T: fmt::Display + ?Sized> ToString for T {
2238     #[inline]
2239     default fn to_string(&self) -> String {
2240         use fmt::Write;
2241         let mut buf = String::new();
2242         buf.write_fmt(format_args!("{}", self))
2243             .expect("a Display implementation returned an error unexpectedly");
2244         buf.shrink_to_fit();
2245         buf
2246     }
2247 }
2248
2249 #[stable(feature = "char_to_string_specialization", since = "1.46.0")]
2250 impl ToString for char {
2251     #[inline]
2252     fn to_string(&self) -> String {
2253         String::from(self.encode_utf8(&mut [0; 4]))
2254     }
2255 }
2256
2257 #[stable(feature = "str_to_string_specialization", since = "1.9.0")]
2258 impl ToString for str {
2259     #[inline]
2260     fn to_string(&self) -> String {
2261         String::from(self)
2262     }
2263 }
2264
2265 #[stable(feature = "cow_str_to_string_specialization", since = "1.17.0")]
2266 impl ToString for Cow<'_, str> {
2267     #[inline]
2268     fn to_string(&self) -> String {
2269         self[..].to_owned()
2270     }
2271 }
2272
2273 #[stable(feature = "string_to_string_specialization", since = "1.17.0")]
2274 impl ToString for String {
2275     #[inline]
2276     fn to_string(&self) -> String {
2277         self.to_owned()
2278     }
2279 }
2280
2281 #[stable(feature = "rust1", since = "1.0.0")]
2282 impl AsRef<str> for String {
2283     #[inline]
2284     fn as_ref(&self) -> &str {
2285         self
2286     }
2287 }
2288
2289 #[stable(feature = "string_as_mut", since = "1.43.0")]
2290 impl AsMut<str> for String {
2291     #[inline]
2292     fn as_mut(&mut self) -> &mut str {
2293         self
2294     }
2295 }
2296
2297 #[stable(feature = "rust1", since = "1.0.0")]
2298 impl AsRef<[u8]> for String {
2299     #[inline]
2300     fn as_ref(&self) -> &[u8] {
2301         self.as_bytes()
2302     }
2303 }
2304
2305 #[stable(feature = "rust1", since = "1.0.0")]
2306 impl From<&str> for String {
2307     #[inline]
2308     fn from(s: &str) -> String {
2309         s.to_owned()
2310     }
2311 }
2312
2313 #[stable(feature = "from_mut_str_for_string", since = "1.44.0")]
2314 impl From<&mut str> for String {
2315     /// Converts a `&mut str` into a `String`.
2316     ///
2317     /// The result is allocated on the heap.
2318     #[inline]
2319     fn from(s: &mut str) -> String {
2320         s.to_owned()
2321     }
2322 }
2323
2324 #[stable(feature = "from_ref_string", since = "1.35.0")]
2325 impl From<&String> for String {
2326     #[inline]
2327     fn from(s: &String) -> String {
2328         s.clone()
2329     }
2330 }
2331
2332 // note: test pulls in libstd, which causes errors here
2333 #[cfg(not(test))]
2334 #[stable(feature = "string_from_box", since = "1.18.0")]
2335 impl From<Box<str>> for String {
2336     /// Converts the given boxed `str` slice to a `String`.
2337     /// It is notable that the `str` slice is owned.
2338     ///
2339     /// # Examples
2340     ///
2341     /// Basic usage:
2342     ///
2343     /// ```
2344     /// let s1: String = String::from("hello world");
2345     /// let s2: Box<str> = s1.into_boxed_str();
2346     /// let s3: String = String::from(s2);
2347     ///
2348     /// assert_eq!("hello world", s3)
2349     /// ```
2350     fn from(s: Box<str>) -> String {
2351         s.into_string()
2352     }
2353 }
2354
2355 #[stable(feature = "box_from_str", since = "1.20.0")]
2356 impl From<String> for Box<str> {
2357     /// Converts the given `String` to a boxed `str` slice that is owned.
2358     ///
2359     /// # Examples
2360     ///
2361     /// Basic usage:
2362     ///
2363     /// ```
2364     /// let s1: String = String::from("hello world");
2365     /// let s2: Box<str> = Box::from(s1);
2366     /// let s3: String = String::from(s2);
2367     ///
2368     /// assert_eq!("hello world", s3)
2369     /// ```
2370     fn from(s: String) -> Box<str> {
2371         s.into_boxed_str()
2372     }
2373 }
2374
2375 #[stable(feature = "string_from_cow_str", since = "1.14.0")]
2376 impl<'a> From<Cow<'a, str>> for String {
2377     fn from(s: Cow<'a, str>) -> String {
2378         s.into_owned()
2379     }
2380 }
2381
2382 #[stable(feature = "rust1", since = "1.0.0")]
2383 impl<'a> From<&'a str> for Cow<'a, str> {
2384     #[inline]
2385     fn from(s: &'a str) -> Cow<'a, str> {
2386         Cow::Borrowed(s)
2387     }
2388 }
2389
2390 #[stable(feature = "rust1", since = "1.0.0")]
2391 impl<'a> From<String> for Cow<'a, str> {
2392     #[inline]
2393     fn from(s: String) -> Cow<'a, str> {
2394         Cow::Owned(s)
2395     }
2396 }
2397
2398 #[stable(feature = "cow_from_string_ref", since = "1.28.0")]
2399 impl<'a> From<&'a String> for Cow<'a, str> {
2400     #[inline]
2401     fn from(s: &'a String) -> Cow<'a, str> {
2402         Cow::Borrowed(s.as_str())
2403     }
2404 }
2405
2406 #[stable(feature = "cow_str_from_iter", since = "1.12.0")]
2407 impl<'a> FromIterator<char> for Cow<'a, str> {
2408     fn from_iter<I: IntoIterator<Item = char>>(it: I) -> Cow<'a, str> {
2409         Cow::Owned(FromIterator::from_iter(it))
2410     }
2411 }
2412
2413 #[stable(feature = "cow_str_from_iter", since = "1.12.0")]
2414 impl<'a, 'b> FromIterator<&'b str> for Cow<'a, str> {
2415     fn from_iter<I: IntoIterator<Item = &'b str>>(it: I) -> Cow<'a, str> {
2416         Cow::Owned(FromIterator::from_iter(it))
2417     }
2418 }
2419
2420 #[stable(feature = "cow_str_from_iter", since = "1.12.0")]
2421 impl<'a> FromIterator<String> for Cow<'a, str> {
2422     fn from_iter<I: IntoIterator<Item = String>>(it: I) -> Cow<'a, str> {
2423         Cow::Owned(FromIterator::from_iter(it))
2424     }
2425 }
2426
2427 #[stable(feature = "from_string_for_vec_u8", since = "1.14.0")]
2428 impl From<String> for Vec<u8> {
2429     /// Converts the given `String` to a vector `Vec` that holds values of type `u8`.
2430     ///
2431     /// # Examples
2432     ///
2433     /// Basic usage:
2434     ///
2435     /// ```
2436     /// let s1 = String::from("hello world");
2437     /// let v1 = Vec::from(s1);
2438     ///
2439     /// for b in v1 {
2440     ///     println!("{}", b);
2441     /// }
2442     /// ```
2443     fn from(string: String) -> Vec<u8> {
2444         string.into_bytes()
2445     }
2446 }
2447
2448 #[stable(feature = "rust1", since = "1.0.0")]
2449 impl fmt::Write for String {
2450     #[inline]
2451     fn write_str(&mut self, s: &str) -> fmt::Result {
2452         self.push_str(s);
2453         Ok(())
2454     }
2455
2456     #[inline]
2457     fn write_char(&mut self, c: char) -> fmt::Result {
2458         self.push(c);
2459         Ok(())
2460     }
2461 }
2462
2463 /// A draining iterator for `String`.
2464 ///
2465 /// This struct is created by the [`drain`] method on [`String`]. See its
2466 /// documentation for more.
2467 ///
2468 /// [`drain`]: struct.String.html#method.drain
2469 /// [`String`]: struct.String.html
2470 #[stable(feature = "drain", since = "1.6.0")]
2471 pub struct Drain<'a> {
2472     /// Will be used as &'a mut String in the destructor
2473     string: *mut String,
2474     /// Start of part to remove
2475     start: usize,
2476     /// End of part to remove
2477     end: usize,
2478     /// Current remaining range to remove
2479     iter: Chars<'a>,
2480 }
2481
2482 #[stable(feature = "collection_debug", since = "1.17.0")]
2483 impl fmt::Debug for Drain<'_> {
2484     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2485         f.pad("Drain { .. }")
2486     }
2487 }
2488
2489 #[stable(feature = "drain", since = "1.6.0")]
2490 unsafe impl Sync for Drain<'_> {}
2491 #[stable(feature = "drain", since = "1.6.0")]
2492 unsafe impl Send for Drain<'_> {}
2493
2494 #[stable(feature = "drain", since = "1.6.0")]
2495 impl Drop for Drain<'_> {
2496     fn drop(&mut self) {
2497         unsafe {
2498             // Use Vec::drain. "Reaffirm" the bounds checks to avoid
2499             // panic code being inserted again.
2500             let self_vec = (*self.string).as_mut_vec();
2501             if self.start <= self.end && self.end <= self_vec.len() {
2502                 self_vec.drain(self.start..self.end);
2503             }
2504         }
2505     }
2506 }
2507
2508 #[stable(feature = "drain", since = "1.6.0")]
2509 impl Iterator for Drain<'_> {
2510     type Item = char;
2511
2512     #[inline]
2513     fn next(&mut self) -> Option<char> {
2514         self.iter.next()
2515     }
2516
2517     fn size_hint(&self) -> (usize, Option<usize>) {
2518         self.iter.size_hint()
2519     }
2520
2521     #[inline]
2522     fn last(mut self) -> Option<char> {
2523         self.next_back()
2524     }
2525 }
2526
2527 #[stable(feature = "drain", since = "1.6.0")]
2528 impl DoubleEndedIterator for Drain<'_> {
2529     #[inline]
2530     fn next_back(&mut self) -> Option<char> {
2531         self.iter.next_back()
2532     }
2533 }
2534
2535 #[stable(feature = "fused", since = "1.26.0")]
2536 impl FusedIterator for Drain<'_> {}
2537
2538 #[stable(feature = "from_char_for_string", since = "1.46.0")]
2539 impl From<char> for String {
2540     #[inline]
2541     fn from(c: char) -> Self {
2542         c.to_string()
2543     }
2544 }