Visitar URL original
Consider adding `Vec::try_with_capacity(_in)` and friends · Issue #91913 · rust-lang/rust · GitHub
Skip to content

Consider adding Vec::try_with_capacity(_in) and friends #91913

Open

Description

@poliorcetics

With try_reserve(_exact) stabilized, the following pattern is going to start cropping up (it already does in our internal code base):

let mut buf = Vec::new();
buf.try_reserve_exact(cap)?;

Would adding one or two extra methods per container; try_with_capacity(capacity: usize) -> Result<.., TryReserveError> and try_with_capacity_in(capacity: usize, alloc: A) -> Result<.., TryReserveError> be appropriate ?

Note that the two-liner above works perfectly to the best of my knowledge, it is just a question of conciseness. I also find it makes intent clearer from the start, but other may have diverging opinions on this.

Affected types would be:

  • try_with_capacity_in(capacity: usize, alloc: A) -> Result<.., TryReserveError> (see all types):
    • std::collections::VecDeque
    • std::vec::Vec
  • try_with_capacity(capacity: usize) -> Result<.., TryReserveError> (see all types):
    • std::collections::BinaryHeap
    • std::collections::VecDeque
    • std::collections::hash_map::HashMap
    • std::collections::hash_set::HashSet
    • std::ffi::OsString
    • std::io::BufReader
    • std::io::BufWriter
    • std::io::LineWriter
    • std::path::PathBuf
    • std::string::String
    • std::vec::Vec

@rustbot label T-libs C-feature-request T-libs-api

Aside: I'm not sure if this falls under A-allocators

Activity

  1. added
    C-feature-requestCategory: A feature request, i.e: not implemented / a PR.
    T-libsRelevant to the library team, which will review and decide on the PR/issue.
    T-libs-api[DEPRECATED; DO NOT USE]
    on Dec 14, 2021
  2. poliorcetics commented on Dec 22, 2021

    @poliorcetics
    ContributorAuthor

    @rustbot claim

  3. poliorcetics commented on Jan 7, 2022

    @poliorcetics
    ContributorAuthor

    Related to #91789

  4. TennyZhuang commented on Jan 14, 2022

    @TennyZhuang
    Contributor

    Related to #86942

  5. dpaoliello commented on Mar 29, 2022

    @dpaoliello
    Contributor

    I've added this for Vec: #95051

  6. removed their assignment
    on Jul 20, 2022
  7. kornelski commented on Jan 30, 2024

    @kornelski
    Contributor

    I'd like to revive this. Previous PR tried to add too many extra methods. Let's focus on just try_with_capacity().

    There is a need for this method beyond convenience. try_reserve calls finish_grow which deliberately is not inline. However finish_grow takes old and new Layout as arguments, which adds code bloat at the call site, and does unnecesary checks for reallocation. OTOH with_capacity inlines to a very simple rustc_alloc call and an alloc error handler call, and try_with_capacity could be even leaner.

  8. kornelski commented on Jan 30, 2024

    @kornelski
    Contributor

    @rustbot claim

  9. 17 remaining items

  10. added a commit that references this issue on Mar 9, 2024
  11. added a commit that references this issue on Mar 9, 2024
  12. c410-f3r commented on Jul 4, 2024

    @c410-f3r
    Contributor

    What is the status of this feature? IFAICT, there are no listed concerns.

  13. poliorcetics commented on Jul 4, 2024

    @poliorcetics
    ContributorAuthor

    From what I can tell, no one has implemented the methods nor has anyone asked for stabilization of those that were (I think).

  14. kornelski commented on Jul 4, 2024

    @kornelski
    Contributor

    try_with_capacity is implemented (unstable).

    https://doc.rust-lang.org/nightly/std/vec/struct.Vec.html#method.try_with_capacity

    The contentious issue was about potentially endless proliferation of try_ methods. This will probably remain controversial and block stabilization, until either somehow allowing try_ on everything becomes an accepted policy, or some other solution emerges that gives Rust something like method overloading or optional params to deal with all the try_ and _in combinations.

  15. c410-f3r commented on Jul 4, 2024

    @c410-f3r
    Contributor

    Thank you guys.

    Looks like the try_ matter should be brought up to the lib team, otherwise progress won't be made.

  16. eirnym commented on Jun 5, 2026

    @eirnym

    I would prefer to stabilize these methods portion-wise (not all at once). Even try_with_capacity provides quite an improvement of potential error handling in libs and apps.

  17. added
    C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFC
    on Oct 22, 2026
  18. clubby789 commented on Oct 22, 2026

    @clubby789
    Contributor

    This is implemented and this issue is referenced as a tracking issue.

  19. added
    A-allocatorsArea: Custom and system allocators
    A-collectionsArea: `std::collections` (prefer A-binaryheap, A-btree, A-hashtable, A-linkedlist, A-vecdeque)
    on Oct 22, 2026
  20. added
    A-slice-vecArea: `[T]` and `Vec<T>`
    and removed
    A-collectionsArea: `std::collections` (prefer A-binaryheap, A-btree, A-hashtable, A-linkedlist, A-vecdeque)
    C-feature-requestCategory: A feature request, i.e: not implemented / a PR.
    on Nov 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

A-allocatorsArea: Custom and system allocatorsA-slice-vecArea: `[T]` and `Vec<T>`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