Skip to content

swap_unchecked ? #71874

Closed
Closed
@leonardo-m

Description

@leonardo-m

A slice method similar to slice::get_unchecked_mut that could be added:

#[inline]
pub unsafe fn swap_unchecked(&mut self, a: usize, b: usize) {
    unsafe {
        // Can't take two mutable loans from one vector, so instead
        // just cast them to their raw pointers to do the swap.
        let pa: *mut T = self.get_unchecked_mut(a);
        let pb: *mut T = self.get_unchecked_mut(b);
        ptr::swap(pa, pb);
    }
}

To be added to the std lib only if there's enough usage for this method.

See also:
https://crates.io/crates/unchecked-index

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-collectionsArea: `std::collections`C-feature-requestCategory: A feature request, i.e: not implemented / a PR.T-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions