Skip to content

Methods for extracting 1 element out of an owned slice and dropping the rest #376

Closed as not planned
@its-the-shrimp

Description

@its-the-shrimp

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)
    • Doesn't consume the slice, requiring one to mark it as mut even though you don't intend to mutate it;
    • Only works for T: Default;
  • owned_slice.swap_remove(n)
    • Doesn't consume the slice, requiring one to mark it as mut even though you don't intend to mutate it;
    • Only exists for Vec;
  • owned_slice.into_iter().nth(n)
    • Makes the slice irretrievable on failure.

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:

  • They're not beginner-friendly;
  • They can't be suggested as a hint from the compiler to make them more beginner-friendly.

If a standard method for such an extraction were to exist, the following hint could be added (at the end):

error[E0507]: cannot move out of index of `Vec<String>`
 --> src/main.rs:3:1
  |
3 | v[0]
  | ^^^^ move occurs because value has type `String`, which does not implement the `Copy` trait
  |
help: consider cloning the value if the performance cost is acceptable
  |
3 | v[0].clone()
  |     ++++++++
help: if the vector isn't needed anymore, consider using the `into_nth` method
  |
3 | v.into_nth(0).unwrap()
  |

For more information about this error, try `rustc --explain E0507`.

Solution sketch

impl<T> Vec<T> {
    pub fn into_nth(self, n: usize) -> Result<T, Self> { ... }
}

impl<T> [T] {
    pub fn into_nth(self: Box<Self>, n: usize) -> Result<T, Box<Self>> { ... }
}

impl<const N: usize, T> [T; N] {
    pub fn into_nth(self, n: usize) -> Result<T, [T; N]> { ... }
}

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

  • We think this problem seems worth solving, and the standard library might be the right place to solve it.
  • We think that this probably doesn't belong in the standard library.

Second, if there's a concrete solution:

  • We think this specific solution looks roughly right, approved, you or someone else should implement this. (Further review will still happen on the subsequent implementation PR.)
  • We're not sure this is the right solution, and the alternatives or other materials don't give us enough information to be sure about that. Here are some questions we have that aren't answered, or rough ideas about alternatives we'd want to see discussed.

Metadata

Metadata

Assignees

No one assigned

    Labels

    T-libs-apiapi-change-proposalA proposal to add or alter unstable APIs in the standard libraries

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions