Skip to content

Vec::splice() should explain what happens with an empty range #135369

Closed

Description

@djeedai

Location

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

Summary

As explained in this SO answer, which I assume to be correct, using Vec::splice() with an empty range like 3..3 and a non-empty replace_with argument allows inserting multiple values at once at the index specified by, I think, the range start. But this is not documented, and I don't think it's necessarily trivial for everyone (I had to lookup that SO answer to confirm). I think an example of inserting multiple values without removing any would be beneficial for understanding that splice() is the right tool for that job (and you don't need anything more complicated via iterators and loops or whatnot, like others on SO suggested).

Activity

  1. added
    A-docsArea: Documentation for any part of the project, including the compiler, standard library, and tools
    on Jan 11, 2025
  2. added
    needs-triageThis issue may need triage. Remove when done. See docs forge.rust-lang.org/release/issue-triaging
    on Jan 11, 2025
  3. added
    T-libsRelevant to the library team, which will review and decide on the PR/issue.
    on Jan 11, 2025
  4. removed
    needs-triageThis issue may need triage. Remove when done. See docs forge.rust-lang.org/release/issue-triaging
    on Jan 11, 2025
  5. added
    A-collectionsArea: `std::collections` (prefer A-binaryheap, A-btree, A-hashtable, A-linkedlist, A-vecdeque)
    on Jan 11, 2025
  6. cod10129 commented on Jan 11, 2025

    @cod10129
    Contributor

    I added an example to the Vec::splice docs in this commit.
    @rustbot claim
    I'm not very experienced with Rust contributions, so should I just make a PR now?

  7. the8472 commented on Jan 11, 2025

    @the8472
    Member

    Yes, documentation changes that do not introduce new API guarantees don't require more than a PR.

  8. added a commit that references this issue on Jan 14, 2025
    bf228a8
  9. added a commit that references this issue on Jan 14, 2025
    4d9bdcc
  10. added a commit that references this issue on Mar 11, 2025
    d64f897
  11. 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)
    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-docsArea: Documentation for any part of the project, including the compiler, standard library, and toolsA-slice-vecArea: `[T]` and `Vec<T>`T-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