]> git.lizzy.rs Git - rust.git/commitdiff
Improve doc examples for `Cow::into_owned`.
authorCorey Farwell <coreyf@rwell.org>
Sat, 3 Jun 2017 18:40:23 +0000 (14:40 -0400)
committerCorey Farwell <coreyf@rwell.org>
Sat, 3 Jun 2017 18:40:23 +0000 (14:40 -0400)
src/libcollections/borrow.rs

index 0de52b6696fcf02febb16085c5eb2860edbb52c1..a889b635e87ec12ea5d2dab361b735c0cf5c1cab 100644 (file)
@@ -219,14 +219,33 @@ pub fn to_mut(&mut self) -> &mut <B as ToOwned>::Owned {
     ///
     /// # Examples
     ///
+    /// Calling `into_owned` on a `Cow::Borrowed` clones the underlying data
+    /// and becomes a `Cow::Owned`:
+    ///
     /// ```
     /// use std::borrow::Cow;
     ///
-    /// let cow: Cow<[_]> = Cow::Owned(vec![1, 2, 3]);
+    /// let s = "Hello world!";
+    /// let cow = Cow::Borrowed(s);
+    ///
+    /// assert_eq!(
+    ///   cow.into_owned(),
+    ///   Cow::Owned(String::from(s))
+    /// );
+    /// ```
+    ///
+    /// Calling `into_owned` on a `Cow::Owned` is a no-op:
+    ///
+    /// ```
+    /// use std::borrow::Cow;
     ///
-    /// let hello = cow.into_owned();
+    /// let s = "Hello world!";
+    /// let cow: Cow<str> = Cow::Owned(String::from(s));
     ///
-    /// assert_eq!(vec![1, 2, 3], hello);
+    /// assert_eq!(
+    ///   cow.into_owned(),
+    ///   Cow::Owned(String::from(s))
+    /// );
     /// ```
     #[stable(feature = "rust1", since = "1.0.0")]
     pub fn into_owned(self) -> <B as ToOwned>::Owned {