Skip to content

Commit 6f3919d

Browse files
committed
Improve doc examples for Cow::into_owned.
1 parent 4225019 commit 6f3919d

File tree

1 file changed

+22
-3
lines changed

1 file changed

+22
-3
lines changed

src/libcollections/borrow.rs

+22-3
Original file line numberDiff line numberDiff line change
@@ -219,14 +219,33 @@ impl<'a, B: ?Sized> Cow<'a, B>
219219
///
220220
/// # Examples
221221
///
222+
/// Calling `into_owned` on a `Cow::Borrowed` clones the underlying data
223+
/// and becomes a `Cow::Owned`:
224+
///
222225
/// ```
223226
/// use std::borrow::Cow;
224227
///
225-
/// let cow: Cow<[_]> = Cow::Owned(vec![1, 2, 3]);
228+
/// let s = "Hello world!";
229+
/// let cow = Cow::Borrowed(s);
230+
///
231+
/// assert_eq!(
232+
/// cow.into_owned(),
233+
/// Cow::Owned(String::from(s))
234+
/// );
235+
/// ```
236+
///
237+
/// Calling `into_owned` on a `Cow::Owned` is a no-op:
238+
///
239+
/// ```
240+
/// use std::borrow::Cow;
226241
///
227-
/// let hello = cow.into_owned();
242+
/// let s = "Hello world!";
243+
/// let cow: Cow<str> = Cow::Owned(String::from(s));
228244
///
229-
/// assert_eq!(vec![1, 2, 3], hello);
245+
/// assert_eq!(
246+
/// cow.into_owned(),
247+
/// Cow::Owned(String::from(s))
248+
/// );
230249
/// ```
231250
#[stable(feature = "rust1", since = "1.0.0")]
232251
pub fn into_owned(self) -> <B as ToOwned>::Owned {

0 commit comments

Comments
 (0)