#[inline]
#[rustc_const_stable(feature = "const_string_new", since = "1.39.0")]
#[stable(feature = "rust1", since = "1.0.0")]
+ #[must_use]
pub const fn new() -> String {
String { vec: Vec::new() }
}
#[cfg(not(no_global_oom_handling))]
#[inline]
#[stable(feature = "rust1", since = "1.0.0")]
+ #[must_use]
pub fn with_capacity(capacity: usize) -> String {
String { vec: Vec::with_capacity(capacity) }
}
///
/// assert_eq!("Hello �World", output);
/// ```
+ #[must_use]
#[cfg(not(no_global_oom_handling))]
#[stable(feature = "rust1", since = "1.0.0")]
pub fn from_utf8_lossy(v: &[u8]) -> Cow<'_, str> {
/// String::from_utf16_lossy(v));
/// ```
#[cfg(not(no_global_oom_handling))]
+ #[must_use]
#[inline]
#[stable(feature = "rust1", since = "1.0.0")]
pub fn from_utf16_lossy(v: &[u16]) -> String {
/// assert_eq!("💖", sparkle_heart);
/// ```
#[inline]
+ #[must_use]
#[stable(feature = "rust1", since = "1.0.0")]
pub unsafe fn from_utf8_unchecked(bytes: Vec<u8>) -> String {
String { vec: bytes }
/// assert_eq!("foo", s.as_str());
/// ```
#[inline]
+ #[must_use]
#[stable(feature = "string_as_str", since = "1.7.0")]
pub fn as_str(&self) -> &str {
self
/// assert_eq!("FOOBAR", s_mut_str);
/// ```
#[inline]
+ #[must_use]
#[stable(feature = "string_as_str", since = "1.7.0")]
pub fn as_mut_str(&mut self) -> &mut str {
self
/// assert!(s.capacity() >= 10);
/// ```
#[inline]
+ #[must_use]
#[stable(feature = "rust1", since = "1.0.0")]
pub fn capacity(&self) -> usize {
self.vec.capacity()
/// assert_eq!(&[104, 101, 108, 108, 111], s.as_bytes());
/// ```
#[inline]
+ #[must_use]
#[stable(feature = "rust1", since = "1.0.0")]
pub fn as_bytes(&self) -> &[u8] {
&self.vec
///
/// # Safety
///
- /// This function is unsafe because it does not check that the bytes passed
- /// to it are valid UTF-8. If this constraint is violated, it may cause
- /// memory unsafety issues with future users of the `String`, as the rest of
- /// the standard library assumes that `String`s are valid UTF-8.
+ /// This function is unsafe because the returned `&mut Vec` allows writing
+ /// bytes which are not valid UTF-8. If this constraint is violated, using
+ /// the original `String` after dropping the `&mut Vec` may violate memory
+ /// safety, as the rest of the standard library assumes that `String`s are
+ /// valid UTF-8.
///
/// # Examples
///
///
/// assert_eq!(&[0, 159], value.unwrap_err().as_bytes());
/// ```
+ #[must_use]
#[stable(feature = "from_utf8_error_as_bytes", since = "1.26.0")]
pub fn as_bytes(&self) -> &[u8] {
&self.bytes[..]
/// // the first byte is invalid here
/// assert_eq!(1, error.valid_up_to());
/// ```
+ #[must_use]
#[stable(feature = "rust1", since = "1.0.0")]
pub fn utf8_error(&self) -> Utf8Error {
self.error
/// let _ = drain.next().unwrap();
/// assert_eq!(drain.as_str(), "bc");
/// ```
+ #[must_use]
#[stable(feature = "string_drain_as_str", since = "1.55.0")]
pub fn as_str(&self) -> &str {
self.iter.as_str()