Skip to content

Commit a8dc297

Browse files
committed
Improve doc example for std::borrow::Cow.
1 parent a8d189a commit a8dc297

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

src/libcollections/borrow.rs

+15-2
Original file line numberDiff line numberDiff line change
@@ -86,16 +86,29 @@ impl<T> ToOwned for T where T: Clone {
8686
/// ```
8787
/// use std::borrow::Cow;
8888
///
89-
/// # #[allow(dead_code)]
9089
/// fn abs_all(input: &mut Cow<[i32]>) {
9190
/// for i in 0..input.len() {
9291
/// let v = input[i];
9392
/// if v < 0 {
94-
/// // clones into a vector the first time (if not already owned)
93+
/// // Clones into a vector if not already owned.
9594
/// input.to_mut()[i] = -v;
9695
/// }
9796
/// }
9897
/// }
98+
///
99+
/// // No clone occurs because `input` doesn't need to be mutated.
100+
/// let slice = [0, 1, 2];
101+
/// let mut input = Cow::from(&slice[..]);
102+
/// abs_all(&mut input);
103+
///
104+
/// // Clone occurs because `input` needs to be mutated.
105+
/// let slice = [-1, 0, 1];
106+
/// let mut input = Cow::from(&slice[..]);
107+
/// abs_all(&mut input);
108+
///
109+
/// // No clone occurs because `input` is already owned.
110+
/// let mut input = Cow::from(vec![-1, 0, 1]);
111+
/// abs_all(&mut input);
99112
/// ```
100113
#[stable(feature = "rust1", since = "1.0.0")]
101114
pub enum Cow<'a, B: ?Sized + 'a>

0 commit comments

Comments
 (0)