-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
Conversation
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @withoutboats (or someone else) soon. If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes. Please see the contribution instructions for more information. |
Ping from triage @withoutboats / @rust-lang/docs: This PR requires your review. |
@@ -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 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
src/liballoc/borrow.rs
Outdated
/// _ => panic!("expect owned data"), | ||
/// } | ||
/// ``` | ||
|
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 remove this empty line.
Thanks for the docs addition @kpp! You mentioned that users in the ruRust chat have been asking how to do this. Do you think the main reason users are struggling with this because of the /// If you want to have a structure with a generic type that implements `ToOwned`:
///
/// ```rust
/// struct Items<'a, X: 'a>
/// where [X]: ToOwned<Owned=Vec<X>>
/// {
/// values: Cow<'a, [X]>,
/// }
/// ```
|
It is hard to tell. Maybe because in this example you don't need to specify Here is how users try to implement it (forgetting to specify
They got this error:
They add
And they get sad errors like:
Probably yes. Or shall we change the compiler warning? |
src/liballoc/borrow.rs
Outdated
@@ -142,6 +142,7 @@ impl<T> ToOwned for T | |||
/// abs_all(&mut input); | |||
/// ``` | |||
/// | |||
/// Another example showing how to keep `Cow` in a struct: | |||
/// ``` |
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.
Now please add an "empty" line before the example. 😆
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
All good for me. |
Ping from triage @withoutboats! This PR needs your review. |
📌 Commit 5bfb785 has been approved by |
…omez Add example for Cow Add one more example that shows how to keep `Cow` in a struct. Link to playground: https://play.rust-lang.org/?gist=a9256bdd034b44bc3cdd0044bbcdbb7c&version=stable&mode=debug&edition=2015 Users ask this question in [ruRust](https://gitter.im/ruRust/general) chat time to time and it is not obvious to add `ToOwned<Owned=Target>` to requirements of generic params.
…omez Add example for Cow Add one more example that shows how to keep `Cow` in a struct. Link to playground: https://play.rust-lang.org/?gist=a9256bdd034b44bc3cdd0044bbcdbb7c&version=stable&mode=debug&edition=2015 Users ask this question in [ruRust](https://gitter.im/ruRust/general) chat time to time and it is not obvious to add `ToOwned<Owned=Target>` to requirements of generic params.
…omez Add example for Cow Add one more example that shows how to keep `Cow` in a struct. Link to playground: https://play.rust-lang.org/?gist=a9256bdd034b44bc3cdd0044bbcdbb7c&version=stable&mode=debug&edition=2015 Users ask this question in [ruRust](https://gitter.im/ruRust/general) chat time to time and it is not obvious to add `ToOwned<Owned=Target>` to requirements of generic params.
…omez Add example for Cow Add one more example that shows how to keep `Cow` in a struct. Link to playground: https://play.rust-lang.org/?gist=a9256bdd034b44bc3cdd0044bbcdbb7c&version=stable&mode=debug&edition=2015 Users ask this question in [ruRust](https://gitter.im/ruRust/general) chat time to time and it is not obvious to add `ToOwned<Owned=Target>` to requirements of generic params.
…omez Add example for Cow Add one more example that shows how to keep `Cow` in a struct. Link to playground: https://play.rust-lang.org/?gist=a9256bdd034b44bc3cdd0044bbcdbb7c&version=stable&mode=debug&edition=2015 Users ask this question in [ruRust](https://gitter.im/ruRust/general) chat time to time and it is not obvious to add `ToOwned<Owned=Target>` to requirements of generic params.
Rollup of 20 pull requests Successful merges: - #51760 (Add another PartialEq example) - #53113 (Add example for Cow) - #53129 (remove `let x = baz` which was obscuring the real error) - #53389 (document effect of join on memory ordering) - #53472 (Use FxHash{Map,Set} instead of the default Hash{Map,Set} everywhere in rustc.) - #53476 (Add partialeq implementation for TryFromIntError type) - #53513 (Force-inline `shallow_resolve` at its hottest call site.) - #53655 (set applicability) - #53702 (Fix stabilisation version for macro_vis_matcher.) - #53727 (Do not suggest dereferencing in macro) - #53732 (save-analysis: Differentiate foreign functions and statics.) - #53740 (add llvm-readobj to llvm-tools-preview) - #53743 (fix a typo: taget_env -> target_env) - #53747 (Rustdoc fixes) - #53753 (expand keep-stage --help text) - #53756 (Fix typo in comment) - #53768 (move file-extension based .gitignore down to src/) - #53785 (Fix a comment in src/libcore/slice/mod.rs) - #53786 (Replace usages of 'bad_style' with 'nonstandard_style'.) - #53806 (Fix UI issues on Implementations on Foreign types) Failed merges: r? @ghost
Add one more example that shows how to keep
Cow
in a struct.Link to playground: https://play.rust-lang.org/?gist=a9256bdd034b44bc3cdd0044bbcdbb7c&version=stable&mode=debug&edition=2015
Users ask this question in ruRust chat time to time and it is not obvious to add
ToOwned<Owned=Target>
to requirements of generic params.