Skip to content

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

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

Closed
djeedai opened this issue Jan 11, 2025 · 2 comments · Fixed by #135381
Closed

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

djeedai opened this issue Jan 11, 2025 · 2 comments · Fixed by #135381
Assignees
Labels
A-collections Area: `std::collections` A-docs Area: Documentation for any part of the project, including the compiler, standard library, and tools T-libs Relevant to the library team, which will review and decide on the PR/issue.

Comments

@djeedai
Copy link

djeedai commented Jan 11, 2025

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).

@djeedai djeedai added the A-docs Area: Documentation for any part of the project, including the compiler, standard library, and tools label Jan 11, 2025
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Jan 11, 2025
@fmease fmease added the T-libs Relevant to the library team, which will review and decide on the PR/issue. label Jan 11, 2025
@Noratrieb Noratrieb removed the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Jan 11, 2025
@fmease fmease added the A-collections Area: `std::collections` label Jan 11, 2025
@cod10129
Copy link
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?

@the8472
Copy link
Member

the8472 commented Jan 11, 2025

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

@bors bors closed this as completed in bf228a8 Jan 14, 2025
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Jan 14, 2025
Rollup merge of rust-lang#135381 - cod10129:vec-splice-doc, r=tgross35

Add an example for `Vec::splice` inserting elements without removing

This example clearly showcases how `splice` can be used to insert multiple elements efficiently at an index into a vector.

Fixes rust-lang#135369.

The added example:

> Using `splice` to insert new items into a vector efficiently at a specific position indicated by an empty range:
> ```rust
> let mut v = vec![1, 5];
> let new = [2, 3, 4];
> v.splice(1..1, new);
> assert_eq!(v, [1, 2, 3, 4, 5]);
> ```

`@rustbot` label A-docs A-collections
github-actions bot pushed a commit to tautschnig/verify-rust-std that referenced this issue Mar 11, 2025
Add an example for `Vec::splice` inserting elements without removing

This example clearly showcases how `splice` can be used to insert multiple elements efficiently at an index into a vector.

Fixes rust-lang#135369.

The added example:

> Using `splice` to insert new items into a vector efficiently at a specific position indicated by an empty range:
> ```rust
> let mut v = vec![1, 5];
> let new = [2, 3, 4];
> v.splice(1..1, new);
> assert_eq!(v, [1, 2, 3, 4, 5]);
> ```

`@rustbot` label A-docs A-collections
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-collections Area: `std::collections` A-docs Area: Documentation for any part of the project, including the compiler, standard library, and tools T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants