Skip to content

Tracking issue for Cow::is_borrowed and Cow::is_owned #65143

Open

Description

@clarfonthey

View all comments

I just tried to use these in my own code and was kind of shocked they didn't exist.

Justification: this seems like a common Rust pattern. We have is_some and is_none for Option, is_ok and is_err for Result, etc., so, it seems pretty fair to have is_borrowed and is_owned for Cow.

Having as_borrowed and as_owned wouldn't really make much sense, as a simple & and &mut/to_mut cover those use cases. But, these check functions are pretty useful on their own.


Unresolved question: do we want to ship these or defer to matches! and the future possibility of is?

Activity

  1. added
    C-feature-requestCategory: A feature request, i.e: not implemented / a PR.
    T-libs-api[DEPRECATED; DO NOT USE]
    C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFC
    B-unstableBlocker: Implemented in the nightly compiler and unstable.
    and removed
    C-feature-requestCategory: A feature request, i.e: not implemented / a PR.
    on Oct 5, 2019
  2. changed the title [-]Cow::is_borrowed and Cow::is_owned[/-] [+]Tracking issue: Cow::is_borrowed and Cow::is_owned[/+] on Oct 5, 2019
  3. petertodd commented on Oct 12, 2019

    @petertodd
    Contributor

    I'm no expert on how backwards compatibility is handled. But Cow implements Deref, which means these new methods wouldn't be backwards compatible as they'd conflict with methods of the same name on the inner object. To fix this you'll have to change them to take cow: &Self, similar to what most of the methods on Box/Rc etc. take.

  4. changed the title [-]Tracking issue: Cow::is_borrowed and Cow::is_owned[/-] [+]Feature request: Cow::is_borrowed and Cow::is_owned[/+] on Oct 18, 2019
  5. added
    C-feature-requestCategory: A feature request, i.e: not implemented / a PR.
    and removed
    B-unstableBlocker: Implemented in the nightly compiler and unstable.
    C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFC
    on Oct 18, 2019
  6. SimonSapin commented on Oct 18, 2019

    @SimonSapin
    Contributor

    Procedural: I’ve edited the title and labels to reflect that this is not implemented.

    My opinion: I’m not sure that these method carry their weight, since if let Cow::Borrowed(_) = foo or matches!(foo, Cow::Borrowed(_)) can be used. Neither of these existed when Option::is_some was added, and Option is much more common than Cow.

  7. clarfonthey commented on Oct 22, 2019

    @clarfonthey
    ContributorAuthor

    @petertodd That's fair; that said, though, I think that having methods named is_borrowed or is_owned on a type which implements ToOwned would be extremely unusual, because to_owned would not affect said methods.

    @SimonSapin I agree that Option is much more common than Cow, but I feel that these are still useful enough to carry their own weight. This is partially because I was writing code and was honestly surprised they didn't exist, so, I'm already pre-biased to assume that they should exist.

    Mostly, I figured that adding these methods would be worthwhile because they're unlikely to clash with existing ones, and can incubate in nightly for a while to see if they do carry their weight. If they don't, they can be removed.

  8. added a commit that references this issue on Oct 23, 2019
  9. clarfonthey commented on Oct 23, 2019

    @clarfonthey
    ContributorAuthor

    It appears that this is now merged.

  10. 27 remaining items

  11. added 2 commits that reference this issue on Oct 12, 2024
  12. added a commit that references this issue on Oct 13, 2024
  13. scovich commented on Nov 4, 2024

    @scovich
    Contributor

    While matches! is a working solution, is_borrowed/is_owned would be much less cognitively demanding one, so to speak. More like natural language (if cow is owned, then). And IDE completion friendly, easy to find: cow.<TAB COMPLETION>. While know and remember matches!(cow, Cow::Owned(_)) you must know and remember.

    We already write option.is_some(), result.is_ok(), str.is_empty() etc right?

    +1 from me here -- every time I've tried to use matches! in the past (for any reason), reviewers complain that it's hard to read and ask if there's way to avoid it.

    As long as this feature isn't stable, I just end up injecting the methods with an extension trait for readability purposes.

  14. BartMassey commented on Jan 30, 2025

    @BartMassey
    Contributor

    I was quite surprised that these were not available on stable. +1 for stabilizing them: matches!() is a poorer alternative in my opinion, being hard to read and write. I don't understand the downside at all, I think?

  15. GKFX commented on Feb 2, 2025

    @GKFX
    Contributor

    @BartMassey My concern is that determining which variant of an enum a variable is should be a clear and easy application of pattern-matching. Pattern-matching is a crucial Rust feature, and if a simple example of it is considered so verbose or cryptic that it has to be wrapped in a helper function, then to me it demonstrates a need for something like the is keyword to make it more accessible (rust-lang/rfcs#3573), rather than creating helper functions to work around the problem.

    As for other is_* methods in the standard library:

    • Ordering provides helper methods not just for single variants but also pairs of variants (e.g. greater than or equal to, is_ge). This makes sense for that type.
    • Most of the remainder (filesystem stuff, numeric types, is_empty on collections) aren't enums.
    • It's really Option and Result that are the outliers here with is_ok/is_err/is_some/is_none, and I personally would like to drop the Clippy lint that advertises them, particularly if the is keyword passes its RFC.
  16. BartMassey commented on Feb 2, 2025

    @BartMassey
    Contributor

    @GKFX I understand that the prior art is complicated, and that we are planning better features for the future.

    That said, this five-year-old issue (opened before matches existed) asks to add a couple of convenience methods that do not get in the way of anything, require zero maintenance, satisfy the expectations of many existing users including myself, are simple to implement, understand, use and document. Let's just stabilize them.

    It seems to me that std::borrow::Cow is less-used than it could reasonably be. I think this is at least partly because it presents an unfamiliar minimalist interface that is hard to understand. I avoided teaching it in my intro Rust classes for many years for this reason. This change is a step in the right direction.

    (As for Clippy, I strongly prefer that it not take positions on choices that could reasonably be made multiple ways — but that's a separate issue.)

  17. hkBst commented on Mar 9, 2025

    @hkBst
    Member

    #138217 moves these to associated functions, but also reveals that there is only one single use of Cow::is_owned and none of is_borrowed within core/alloc/std. (Otherwise those uses would also have needed adjusting to the change.)

    If I saw correctly these were introduced in October 2019?

  18. added a commit that references this issue on Oct 30, 2025
  19. added a commit that references this issue on Oct 30, 2025
  20. added a commit that references this issue on Oct 30, 2025
  21. added a commit that references this issue on Nov 30, 2025
  22. added
    T-libsRelevant to the library team, which will review and decide on the PR/issue.
    and removed
    T-libs-api[DEPRECATED; DO NOT USE]
    on Aug 12, 2026
  23. removed
    I-libs-radarLibs issues that are tracked on the team's radar.
    on Oct 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    B-unstableBlocker: Implemented in the nightly compiler and unstable.C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFCT-libsRelevant to the library team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions