-
Notifications
You must be signed in to change notification settings - Fork 20
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
Methods for extracting 1 element out of an owned slice and dropping the rest #376
Comments
I don't think that there's anything in particular that's wrong with this API; I'm just struggling to understand how this could be a useful enough API to justify its existence. Isolating a single element of a list and dropping the rest feels like a wasteful operation that shouldn't be done in most cases. |
Yeah, I wonder if there's a more generic solution that would enable this use-case but would enable other use-cases as well. Maybe change the API to return an array instead? |
for let v: [T; N] = ...;
{...}
let [v0, ..] = v; unfortunately this doesn't work for |
Also worth mentioning that for boxed slices, converting to a vec first is relatively painless and requires just one method call, and generally if you plan to do mutation you should keep it as a vec anyway instead of stripping the excess capacity. |
Ideally it's true, but a lot of times you don't have control over the API, it gives you a vector but in reality you only really care about one element, you might not be able to just not construct the vector/boxed slice/array from the start.
Yet you need to come up with it, there's no straightforward answer to extracting 1 element from a boxed slice.
True, but, again, one doesn't always get to change the API. Pattern matching is yet another way to extract the first/last element, but also very limited; there are multiple approaches and not one if them is universal enough, I think having 1 intended way to do such a trivial indexing operation would make the language more beginner-friendly overall, since having 1 intended method means the compiler can recommend it when it encounters an expression of the form |
We discussed this ACP in this week's standard library API team meeting, and those present were not convinced that this API would be broadly applicable enough to get a dedicated method. We would recommend continuing to use the already available approach using if n < owned_slice.len() {
Ok(owned_slice.into_iter().nth(n).unwrap())
} else {
Err(owned_slice)
} Or for a contiguous range of elements as mentioned in #376 (comment): if offset + N < owned_slice.len() {
let mut iter = owned_slice.into_iter().skip(offset);
Ok([(); N].map(|_| iter.next().unwrap()))
} else {
Err(owned_slice)
} If any of I would also accept a standard library docs PR pointing out the |
Proposal
Make extracting 1 element out of an owned slice and dropping the rest more straightforward by introducing an
into_nth
method to the 3 main owned slice types:Vec<T>
,Box<[T]>
,[T; N]
Problem statement
Turning an owned slice into 1 of its elements is in theory a trivial task, yet the APIs of the 3 main owned slice types and the functionality of
Index{Mut}
traits makes it unjustifiably harder.There are 3 main ways to do so, but they're more of the "best effort" type of solution:
owned_slice.get_mut(n).map(std::mem::take)
mut
even though you don't intend to mutate it;T: Default
;owned_slice.swap_remove(n)
mut
even though you don't intend to mutate it;Vec
;owned_slice.into_iter().nth(n)
There's another disadvantage that's mutual for all the 3 approaches above and for anything else there might be:
they're not the obvious solution, not the standard one. This means that:
If a standard method for such an extraction were to exist, the following hint could be added (at the end):
Solution sketch
Alternatives
The only alternate solution here is a trait for by-value indexing to control the behaviour of an indexing expression, but there are no plans for this as far as I'm aware, this approach would take much longer, and wouldn't be worth the wait since methods can exist just as well in the meantime.
What happens now?
This issue contains an API change proposal (or ACP) and is part of the libs-api team feature lifecycle. Once this issue is filed, the libs-api team will review open proposals as capability becomes available. Current response times do not have a clear estimate, but may be up to several months.
Possible responses
The libs team may respond in various different ways. First, the team will consider the problem (this doesn't require any concrete solution or alternatives to have been proposed):
Second, if there's a concrete solution:
The text was updated successfully, but these errors were encountered: