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