File tree 1 file changed +15
-2
lines changed
1 file changed +15
-2
lines changed Original file line number Diff line number Diff line change @@ -86,16 +86,29 @@ impl<T> ToOwned for T where T: Clone {
86
86
/// ```
87
87
/// use std::borrow::Cow;
88
88
///
89
- /// # #[allow(dead_code)]
90
89
/// fn abs_all(input: &mut Cow<[i32]>) {
91
90
/// for i in 0..input.len() {
92
91
/// let v = input[i];
93
92
/// if v < 0 {
94
- /// // clones into a vector the first time ( if not already owned)
93
+ /// // Clones into a vector if not already owned.
95
94
/// input.to_mut()[i] = -v;
96
95
/// }
97
96
/// }
98
97
/// }
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);
99
112
/// ```
100
113
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
101
114
pub enum Cow < ' a , B : ?Sized + ' a >
You can’t perform that action at this time.
0 commit comments