-
Notifications
You must be signed in to change notification settings - Fork 13k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add example for Cow #53113
Add example for Cow #53113
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -141,6 +141,40 @@ impl<T> ToOwned for T | |
/// let mut input = Cow::from(vec![-1, 0, 1]); | ||
/// abs_all(&mut input); | ||
/// ``` | ||
/// | ||
/// ``` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Now please add an "empty" line before the example. 😆 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
/// use std::borrow::{Cow, ToOwned}; | ||
/// | ||
/// struct Items<'a, X: 'a> where [X]: ToOwned<Owned=Vec<X>> { | ||
/// values: Cow<'a, [X]>, | ||
/// } | ||
/// | ||
/// impl<'a, X: Clone + 'a> Items<'a, X> where [X]: ToOwned<Owned=Vec<X>> { | ||
/// fn new(v: Cow<'a, [X]>) -> Self { | ||
/// Items { values: v } | ||
/// } | ||
/// } | ||
/// | ||
/// // Creates a container from borrowed values of a slice | ||
/// let readonly = [1, 2]; | ||
/// let borrowed = Items::new((&readonly[..]).into()); | ||
/// match borrowed { | ||
/// Items { values: Cow::Borrowed(b) } => println!("borrowed {:?}", b), | ||
/// _ => panic!("expect borrowed value"), | ||
/// } | ||
/// | ||
/// let mut clone_on_write = borrowed; | ||
/// // Mutates the data from slice into owned vec and pushes a new value on top | ||
/// clone_on_write.values.to_mut().push(3); | ||
/// println!("clone_on_write = {:?}", clone_on_write.values); | ||
/// | ||
/// // The data was mutated. Let check it out. | ||
/// match clone_on_write { | ||
/// Items { values: Cow::Owned(_) } => println!("clone_on_write contains owned data"), | ||
/// _ => panic!("expect owned data"), | ||
/// } | ||
/// ``` | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please remove this empty line. |
||
#[stable(feature = "rust1", since = "1.0.0")] | ||
pub enum Cow<'a, B: ?Sized + 'a> | ||
where B: ToOwned | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add an "introduction" sentence explaining what does this new example (otherwise it's strange to just have two examples following each other).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done