-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
String/Vec::splice(RangeArgument, IntoIterator) #1432
Conversation
Any reason you don't return an iterator that consumes the removed elements like splice? I'd also just call it Second, you can fall back on allocating temporary storage and copying twice if |
Both good points. |
It looks like this functionality already exists! http://bluss.github.io/arrayvec/doc/odds/vec/trait.VecExt.html#tymethod.splice |
|
I think this is covered in #1253, just, as commented, should be generalized to Vec as well. |
I’ve updated the RFC to incorporate some feedback (rename to @alexcrichton Could the libs team discuss this? Is it desired in |
This all seems pretty reasonable to me, and the libs team process for this is to:
So along those lines anyone can always discuss this! If @aturon or anyone else on the libs team thinks this is ready for FCP then we'll likely move it in. |
Another possibility is to add this as functionality to the Drain iterator. (This was @bluss's idea when I discussed it with him). Basically having something like: FWIW there are quite a few languages which have this operation as a first class feature, for example, Python ( |
Indeed, now that this new method was changed to return an iterator I think it’s equivalent to |
Would this be a duplicate fn if |
Would |
I'm not sure which parameter you mean, but I was imagining this (I very well may be wrong here!): Given:
it would look like:
|
Ah, but I see splice is supposed to return the spliced-out slice. Perhaps that's just an oversight in the |
The IndexAssign RFC /pull/1129 doesn't actually propose adding any operations to slices using IndexAssign. |
True, but that could be revisited. It certainly seems like it's be nice. |
🔔 This RFC is now entering its week-long final comment period 🔔 |
How about adding two methods |
@ollie27 In the current proposal, you don't have to consume the iterator, you just have to drop it. |
Well that's still an extra thing you have to do. |
Drops are automatic. You don't have to explicitly do anything.
|
Ah good point, I was thinking of assigning the result to a variable for some reason. |
} | ||
} | ||
|
||
struct Splice<I: IntoIterator> { |
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.
I think this should be Iterator
else it won't work out. But that's a technicality.
The new flexible length semantics seem proportionally complex to me. |
@bluss I don’t understand your last comment. I guess flexible length refers to the iterator of new values not having to be the same length as the range it replaces. But what does proportionally complex mean? |
splice found a nice way to solve it for any iterator I think. I just mean that splice is simple for good input (exact size-ish) and a bit more involved for the fallback cases, but it's not too complex. Complexity was my main worry with the expanded scope with non-exact size iterators. |
This RFC was discussed yesterday during libs triage, and the decision was to merge. We discussed whether we want APIs like this to run wild as it's unclear if the API of types like Thanks for the RFC @SimonSapin |
Tracking issue: rust-lang/rust#32310 |
@alexcrichton Did the libs team discuss unresolved questions? In particular, when to consume the input iterator. |
@SimonSapin We didn't discuss that in depth in the meeting (and in general are OK tweaking this point prior to stabilization). From my perspective, I think it makes the most sense to consume on drop, given that there isn't necessarily a direct relationship between the number of items being drained and those being incorporated. (That is, if you had an interleaving semantics, in general interleaving would only happen for some prefix.) It seems simpler and more consistent to just do them all at once after the drop. For the record, I'm just a little uncomfortable with so much of the action being tied to |
I agree that doing so much in |
Implement Vec::splice and String::splice (RFC 1432) RFC: rust-lang/rfcs#1432, tracking issue: rust-lang#32310 A rebase of rust-lang#32355 with a few more tests. Let me know if you have any ideas for more tests. cc @SimonSapin
Implement Vec::splice and String::splice (RFC 1432) RFC: rust-lang/rfcs#1432, tracking issue: rust-lang#32310 A rebase of rust-lang#32355 with a few more tests. Let me know if you have any ideas for more tests. cc @SimonSapin
Implement Vec::splice and String::splice (RFC 1432) RFC: rust-lang/rfcs#1432, tracking issue: rust-lang#32310 A rebase of rust-lang#32355 with a few more tests. Let me know if you have any ideas for more tests. cc @SimonSapin
Implement Vec::splice and String::splice (RFC 1432) RFC: rust-lang/rfcs#1432, tracking issue: rust-lang#32310 A rebase of rust-lang#32355 with a few more tests. Let me know if you have any ideas for more tests. cc @SimonSapin
Implement Vec::splice and String::splice (RFC 1432) RFC: rust-lang/rfcs#1432, tracking issue: #32310 A rebase of #32355 with a few more tests. Let me know if you have any ideas for more tests. cc @SimonSapin
Add a
replace_slice
method toVec<T>
andString
removes a range of elements,and replaces it in place with a given sequence of values.
The new sequence does not necessarily have the same length as the range it replaces.
Rendered.