Skip to content

Commit d015610

Browse files
committed
Auto merge of #42414 - frewsxcv:frewsxcv/improve-cow-docs, r=QuietMisdreavus
Improve `Cow` method doc examples. None
2 parents 13eb0ec + 07cae10 commit d015610

File tree

1 file changed

+29
-7
lines changed

1 file changed

+29
-7
lines changed

src/libcollections/borrow.rs

+29-7
Original file line numberDiff line numberDiff line change
@@ -191,13 +191,16 @@ impl<'a, B: ?Sized> Cow<'a, B>
191191
/// # Examples
192192
///
193193
/// ```
194+
/// use std::ascii::AsciiExt;
194195
/// use std::borrow::Cow;
195196
///
196-
/// let mut cow: Cow<[_]> = Cow::Owned(vec![1, 2, 3]);
197+
/// let mut cow = Cow::Borrowed("foo");
198+
/// cow.to_mut().make_ascii_uppercase();
197199
///
198-
/// let hello = cow.to_mut();
199-
///
200-
/// assert_eq!(hello, &[1, 2, 3]);
200+
/// assert_eq!(
201+
/// cow,
202+
/// Cow::Owned(String::from("FOO")) as Cow<str>
203+
/// );
201204
/// ```
202205
#[stable(feature = "rust1", since = "1.0.0")]
203206
pub fn to_mut(&mut self) -> &mut <B as ToOwned>::Owned {
@@ -219,14 +222,33 @@ impl<'a, B: ?Sized> Cow<'a, B>
219222
///
220223
/// # Examples
221224
///
225+
/// Calling `into_owned` on a `Cow::Borrowed` clones the underlying data
226+
/// and becomes a `Cow::Owned`:
227+
///
222228
/// ```
223229
/// use std::borrow::Cow;
224230
///
225-
/// let cow: Cow<[_]> = Cow::Owned(vec![1, 2, 3]);
231+
/// let s = "Hello world!";
232+
/// let cow = Cow::Borrowed(s);
233+
///
234+
/// assert_eq!(
235+
/// cow.into_owned(),
236+
/// Cow::Owned(String::from(s))
237+
/// );
238+
/// ```
239+
///
240+
/// Calling `into_owned` on a `Cow::Owned` is a no-op:
241+
///
242+
/// ```
243+
/// use std::borrow::Cow;
226244
///
227-
/// let hello = cow.into_owned();
245+
/// let s = "Hello world!";
246+
/// let cow: Cow<str> = Cow::Owned(String::from(s));
228247
///
229-
/// assert_eq!(vec![1, 2, 3], hello);
248+
/// assert_eq!(
249+
/// cow.into_owned(),
250+
/// Cow::Owned(String::from(s))
251+
/// );
230252
/// ```
231253
#[stable(feature = "rust1", since = "1.0.0")]
232254
pub fn into_owned(self) -> <B as ToOwned>::Owned {

0 commit comments

Comments
 (0)