/// # Examples
///
/// ```
- /// # #![feature(collections, core)]
- /// let s = String::from_str("hello");
+ /// # #![feature(collections)]
+ /// let s = String::from("hello");
/// assert_eq!(&s[..], "hello");
/// ```
#[inline]
- #[unstable(feature = "collections",
- reason = "needs investigation to see if to_string() can match perf")]
+ #[unstable(feature = "collections", reason = "use `String::from` instead")]
+ #[deprecated(since = "1.2.0", reason = "use `String::from` instead")]
#[cfg(not(test))]
pub fn from_str(string: &str) -> String {
String { vec: <[_]>::to_vec(string.as_bytes()) }
/// # Examples
///
/// ```
- /// # #![feature(core)]
- /// use std::str::Utf8Error;
- ///
/// let hello_vec = vec![104, 101, 108, 108, 111];
/// let s = String::from_utf8(hello_vec).unwrap();
/// assert_eq!(s, "hello");
/// # Examples
///
/// ```
- /// # #![feature(collections)]
- /// let s = String::from_str("hello");
+ /// let s = String::from("hello");
/// let bytes = s.into_bytes();
/// assert_eq!(bytes, [104, 101, 108, 108, 111]);
/// ```
/// # Examples
///
/// ```
- /// # #![feature(collections)]
- /// let mut s = String::from_str("foo");
+ /// let mut s = String::from("foo");
/// s.push_str("bar");
/// assert_eq!(s, "foobar");
/// ```
///
/// ```
/// let mut s = String::new();
- /// s.reserve(10);
+ /// s.reserve_exact(10);
/// assert!(s.capacity() >= 10);
/// ```
#[inline]
/// # Examples
///
/// ```
- /// # #![feature(collections)]
- /// let mut s = String::from_str("foo");
+ /// let mut s = String::from("foo");
/// s.reserve(100);
/// assert!(s.capacity() >= 100);
/// s.shrink_to_fit();
/// # Examples
///
/// ```
- /// # #![feature(collections)]
- /// let mut s = String::from_str("abc");
+ /// let mut s = String::from("abc");
/// s.push('1');
/// s.push('2');
/// s.push('3');
#[inline]
#[stable(feature = "rust1", since = "1.0.0")]
pub fn push(&mut self, ch: char) {
- if (ch as u32) < 0x80 {
- self.vec.push(ch as u8);
- return;
- }
+ match ch.len_utf8() {
+ 1 => self.vec.push(ch as u8),
+ ch_len => {
+ let cur_len = self.len();
+ // This may use up to 4 bytes.
+ self.vec.reserve(ch_len);
- let cur_len = self.len();
- // This may use up to 4 bytes.
- self.vec.reserve(4);
-
- unsafe {
- // Attempt to not use an intermediate buffer by just pushing bytes
- // directly onto this string.
- let slice = slice::from_raw_parts_mut (
- self.vec.as_mut_ptr().offset(cur_len as isize),
- 4
- );
- let used = ch.encode_utf8(slice).unwrap_or(0);
- self.vec.set_len(cur_len + used);
+ unsafe {
+ // Attempt to not use an intermediate buffer by just pushing bytes
+ // directly onto this string.
+ let slice = slice::from_raw_parts_mut (
+ self.vec.as_mut_ptr().offset(cur_len as isize),
+ ch_len
+ );
+ let used = ch.encode_utf8(slice).unwrap_or(0);
+ self.vec.set_len(cur_len + used);
+ }
+ }
}
}
/// # Examples
///
/// ```
- /// # #![feature(collections)]
- /// let s = String::from_str("hello");
- /// let b: &[_] = &[104, 101, 108, 108, 111];
- /// assert_eq!(s.as_bytes(), b);
+ /// let s = String::from("hello");
+ /// assert_eq!(s.as_bytes(), [104, 101, 108, 108, 111]);
/// ```
#[inline]
#[stable(feature = "rust1", since = "1.0.0")]
/// # Examples
///
/// ```
- /// # #![feature(collections)]
- /// let mut s = String::from_str("hello");
+ /// let mut s = String::from("hello");
/// s.truncate(2);
/// assert_eq!(s, "he");
/// ```
/// # Examples
///
/// ```
- /// # #![feature(collections)]
- /// let mut s = String::from_str("foo");
+ /// let mut s = String::from("foo");
/// assert_eq!(s.pop(), Some('o'));
/// assert_eq!(s.pop(), Some('o'));
/// assert_eq!(s.pop(), Some('f'));
/// # Examples
///
/// ```
- /// # #![feature(collections)]
- /// let mut s = String::from_str("foo");
+ /// let mut s = String::from("foo");
/// assert_eq!(s.remove(0), 'f');
/// assert_eq!(s.remove(1), 'o');
/// assert_eq!(s.remove(0), 'o');
/// # Examples
///
/// ```
- /// # #![feature(collections)]
- /// let mut s = String::from_str("hello");
+ /// let mut s = String::from("hello");
/// unsafe {
/// let vec = s.as_mut_vec();
/// assert!(vec == &[104, 101, 108, 108, 111]);
/// # Examples
///
/// ```
- /// # #![feature(collections_drain)]
+ /// # #![feature(drain)]
///
/// let mut s = String::from("α is alpha, β is beta");
/// let beta_offset = s.find('β').unwrap_or(s.len());
/// s.drain(..);
/// assert_eq!(s, "");
/// ```
- #[unstable(feature = "collections_drain",
+ #[unstable(feature = "drain",
reason = "recently added, matches RFC")]
pub fn drain<R>(&mut self, range: R) -> Drain where R: RangeArgument<usize> {
// Memory safety
}
}
+#[stable(feature = "extend_ref", since = "1.2.0")]
+impl<'a> Extend<&'a char> for String {
+ fn extend<I: IntoIterator<Item=&'a char>>(&mut self, iter: I) {
+ self.extend(iter.into_iter().cloned());
+ }
+}
+
#[stable(feature = "rust1", since = "1.0.0")]
impl<'a> Extend<&'a str> for String {
fn extend<I: IntoIterator<Item=&'a str>>(&mut self, iterable: I) {
/// Wrapper type providing a `&String` reference via `Deref`.
#[unstable(feature = "collections")]
+#[deprecated(since = "1.2.0",
+ reason = "replaced with deref coercions or Borrow")]
+#[allow(deprecated)]
pub struct DerefString<'a> {
x: DerefVec<'a, u8>
}
+#[allow(deprecated)]
impl<'a> Deref for DerefString<'a> {
type Target = String;
/// string_consumer(&as_string("foo"));
/// ```
#[unstable(feature = "collections")]
+#[deprecated(since = "1.2.0",
+ reason = "replaced with deref coercions or Borrow")]
+#[allow(deprecated)]
pub fn as_string<'a>(x: &'a str) -> DerefString<'a> {
DerefString { x: as_vec(x.as_bytes()) }
}
-/// Error returned from `String::from_str`
+/// Error returned from `String::from`
#[unstable(feature = "str_parse_error", reason = "may want to be replaced with \
Void if it ever exists")]
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
type Err = ParseError;
#[inline]
fn from_str(s: &str) -> Result<String, ParseError> {
- Ok(String::from_str(s))
+ Ok(String::from(s))
}
}
}
/// A draining iterator for `String`.
-#[unstable(feature = "collections_drain", reason = "recently added")]
+#[unstable(feature = "drain", reason = "recently added")]
pub struct Drain<'a> {
/// Will be used as &'a mut String in the destructor
string: *mut String,
unsafe impl<'a> Sync for Drain<'a> {}
unsafe impl<'a> Send for Drain<'a> {}
-#[unstable(feature = "collections_drain", reason = "recently added")]
+#[unstable(feature = "drain", reason = "recently added")]
impl<'a> Drop for Drain<'a> {
fn drop(&mut self) {
unsafe {
}
}
-#[unstable(feature = "collections_drain", reason = "recently added")]
+#[unstable(feature = "drain", reason = "recently added")]
impl<'a> Iterator for Drain<'a> {
type Item = char;
}
}
-#[unstable(feature = "collections_drain", reason = "recently added")]
+#[unstable(feature = "drain", reason = "recently added")]
impl<'a> DoubleEndedIterator for Drain<'a> {
#[inline]
fn next_back(&mut self) -> Option<char> {