Repository navigation
Tracking issue for slice_take #62280
Copy link
Copy link
Closed
Labels
A-sliceArea: `[T]`Area: `[T]`C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFCCategory: An issue tracking the progress of sth. like the implementation of an RFCT-libs-api[DEPRECATED; DO NOT USE][DEPRECATED; DO NOT USE]disposition-mergeThis issue / PR is in PFCP or FCP with a disposition to merge it.This issue / PR is in PFCP or FCP with a disposition to merge it.finished-final-comment-periodThe final comment period is finished for this PR / Issue.The final comment period is finished for this PR / Issue.
Milestone
Description
Feature gate: #![feature(slice_take)]
Public API
impl<T> [T] {
fn split_off<'a, R: OneSidedRange<usize>>(self: &mut &'a Self, range: R) -> Option<&'a Self>;
fn split_off_mut<'a, R: OneSidedRange<usize>>(self: &mut &'a mut Self, range: R) -> Option<&'a mut Self>;
fn split_off_first<'a>(self: &mut &'a Self) -> Option<&'a T>;
fn split_off_first_mut<'a>(self: &mut &'a mut Self) -> Option<&'a mut T>;
fn split_off_last<'a>(self: &mut &'a Self) -> Option<&'a T>;
fn split_off_last_mut<'a>(self: &mut &'a mut Self) -> Option<&'a mut T>;
}
// core::ops
trait OneSidedRange<T: ?Sized>: RangeBounds<T> {}
impl<T> OneSidedRange<T> for RangeTo<T> where Self: RangeBounds<T>;
impl<T> OneSidedRange<T> for RangeFrom<T> where Self: RangeBounds<T>;
impl<T> OneSidedRange<T> for RangeToInclusive<T> where Self: RangeBounds<T>;Steps / History
- Introduce <&[_]>::split_off and <&str>::split_off #49173
- Add
take_...functions to slices #62282 - Take 2: Add
take_...functions to slices #77065 - Add slice take methods #88502
- Rename
slice::take...methods tosplit_off...#136555 - Final comment period (FCP)
- Stabilization PR
Unresolved Questions
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
A-sliceArea: `[T]`Area: `[T]`C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFCCategory: An issue tracking the progress of sth. like the implementation of an RFCT-libs-api[DEPRECATED; DO NOT USE][DEPRECATED; DO NOT USE]disposition-mergeThis issue / PR is in PFCP or FCP with a disposition to merge it.This issue / PR is in PFCP or FCP with a disposition to merge it.finished-final-comment-periodThe final comment period is finished for this PR / Issue.The final comment period is finished for this PR / Issue.
Activity
KodrAus commentedon Jul 30, 2020
m-ou-se commentedon Nov 28, 2020
SimonSapin commentedon Jan 31, 2022
https://doc.rust-lang.org/nightly/std/ops/trait.OneSidedRange.html links to #69780 which is closed. Either it should be reopened (and some details filled in) or
#[unstable]attributes should be changed to point here instead, depending on whether it’s useful to trackOneSidedRangeseparately.lilyball commentedon Apr 27, 2022
Can we implement
OneSidedRangeforRangeFullas well? I'd like to be able to sayself.slice.take(..)as a shorthand forstd::mem::swap(&mut self.slice, &[])(or I suppose forself.slice.take(0..)). This doesn't fit the namingOneSidedRangeor the description that says the range is unbounded on one side1, but I don't see any reason whyRangeFullshould not be usable here.Footnotes
Technically it doesn't say it has to be bounded on the other side, but it does list
..as a non-compliant type. ↩andersk commentedon Nov 4, 2022
Perhaps also rename
OneSidedRangetoUnboundedRange, in that case?safinaskar commentedon Jun 3, 2023
Please, rename. Method
takeis unusable with its current name, because it conflicts withstd::io::Read::take.Let me describe my actual production use case.
First I read some data from file using
std::io::Read::read_to_end. To be able to read data I have to writeuse std::io::Read. Then I started to parse that data using various functions, including<[T]>::take. But I was unable to do this because ofuse std::io::Read, see playground: https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=e467873898a5f160420a5d6f13274ffaclarfonthey commentedon Jun 5, 2023
FYI: #69780 is still being linked for
OneSideRangein the docs when that issue is closed; should the tracking issue be moved to here?jongiddy commentedon Jul 1, 2023
I agree with @safinaskar that a different name for
take(andtake_mut) should be considered. I liketake_sliceandtake_slice_mutfor the current API.But changing the API to replace
take(range)withtake_before(usize)andtake_after(usize)(and possiblytake_all()) would be clearer and removes the need to resolve issues withOneSidedRange.jongiddy commentedon Sep 25, 2023
I'd love to see taking from a slice stabilized. But the introduction of
OneSidedRangeseems to be the wrong path to take. I looked at the previous discussion for the benefits of addingOneSidedRange. They are:OneSidedRangedid not solve the method naming problem, the original design withoutOneSidedRangecan also use regular indices rather than length from the end, and I don't see any other code that usesOneSidedRange. The only objective benefit realized from the above is that the number of methods reduced from 8 to 6.Meanwhile the
OneSidedRangecode requires two match statements to turn the range parameter back into a single index and then call separate paths for the two possible directions. It also adds panic code for the unreachable variants when changing the range to an index and a direction.The benefits of
OneSidedRangejust do not justify the additional complexity, especially as part of the standard library.I propose the creation of a new implementation PR to remove
OneSidedRangeand replacetakewithtake_beforeandtake_aftermethods +_mutvariants. I'm happy to make the PR (but also happy for someone else to do it). But I'd like to check if anyone is strongly opposed first.cc @nox @cramertj @timvermeulen @ibraheemdev who worked on the previous implementation PR's.
WaffleLapkin commentedon Oct 2, 2023
@safinaskar this conflict is indeed very unfortunate, however do note that there are workarounds. Notably you can:
Readtrait in a different module or function (you can writeuseinside a function) than the usage of the take method(&mut s).take(..1))<[_]>::take(&mut s, ..1))s.take(..1)whereshas type&mut &[_])See playground.
32 remaining items
rfcbot commentedon Feb 18, 2025
🔔 This is now entering its final comment period, as per the review above. 🔔
rfcbot commentedon Feb 28, 2025
The final comment period, with a disposition to merge, as per the review above, is now complete.
As the automated representative of the governance process, I would like to thank the author for their work and everyone else who contributed.
This will be merged soon.
Rollup merge of rust-lang#137829 - cramertj:stabilize-split-off, r=jh…
Rollup merge of rust-lang#137829 - cramertj:stabilize-split-off, r=jh…
Rollup merge of rust-lang#137829 - cramertj:stabilize-split-off, r=jh…
Unrolled build for rust-lang#137829
Rollup merge of rust-lang#136555 - cramertj:split_off, r=dtolnay
Rollup merge of rust-lang#137829 - cramertj:stabilize-split-off, r=jh…
stepancheg commentedon Mar 16, 2025
Overlap with
Vecmight be misleading. @Kimundi raised this concern above, but maybe did not emphasize it enough. Consider this example:Play.