Skip to content

Example in docs of swap_with_slice() #45636

@leonardo-m

Description

@leonardo-m

The recently introduced slice method swap_with_slice() can't be used to swap two nonoverlapping parts of a single slice. But when you reshuffle parts of a single slice I think it could be useful. So I suggest to offer a slice function that performs this operation safely:

v.swap_nonoverlapping(i, j, n);

That's just a wrapper around (plus panics):

use std::ptr::swap_nonoverlapping;
swap_nonoverlapping(v[i ..].as_mut_ptr(), v[j ..].as_mut_ptr(), n);

Activity

Thiez

Thiez commented on Oct 30, 2017

@Thiez
Contributor

The recently introduced slice method swap_with_slice() can't be used to swap two nonoverlapping parts of a single slice.

Sure it can be:

#![feature(swap_with_slice)]
fn main() {
    let mut slice = [3, 4, 1, 2];
    {
        let (fst, snd) = slice.split_at_mut(2);
        fst.swap_with_slice(snd);
    }
    println!("{:?}", slice); // prints [1, 2, 3, 4]
}
leonardo-m

leonardo-m commented on Oct 30, 2017

@leonardo-m
Author

Nice solution... so do you suggest me to close down this enhancement request?

Thiez

Thiez commented on Oct 30, 2017

@Thiez
Contributor

I am not part of the official Rust project, so I have no authority to tell you to do anything 😄. If you think the code I've shown is good enough that you feel your proposed enhancement is no longer relevant, then sure, close the issue. But if you still think a swap_nonoverlapping method would be a good idea, then by all means continue. However, the usual way of getting additions to the standard library is to go through the RFC process. Be warned, it's quite a lot of work.

leonardo-m

leonardo-m commented on Oct 30, 2017

@leonardo-m
Author

I think your solution is good enough for my problem. But I think eventually we'll need a solution for the discoverability of similar solutions combining two or three functions of the std library ::-) It's a matter of documentation... So I keep this issue open, but I suggest the addition of your solution to the docs of swap_with_slice().

scottmcm

scottmcm commented on Oct 30, 2017

@scottmcm
Member

It would be great to get a doc PR for all of copy_from_slice, clone_from_slice, and swap_with_slice that demonstrate using range indexing and split_at to work with sub-slices. A few people have shown up on IRC asking about that after getting panics for different lengths and not knowing what to do.

changed the title [-]slice swap_nonoverlapping[/-] [+]Example in docs of swap_with_slice()[/+] on Oct 30, 2017
added
C-enhancementCategory: An issue proposing an enhancement or a PR with one.
A-docsArea: Documentation for any part of the project, including the compiler, standard library, and tools
on Oct 31, 2017
self-assigned this
on Nov 23, 2017
added a commit that references this issue on Nov 23, 2017
f58e5e5
frewsxcv

frewsxcv commented on Nov 23, 2017

@frewsxcv
Member

Opened a PR for this: #46219

added a commit that references this issue on Nov 24, 2017
1ad38f2
added a commit that references this issue on Nov 29, 2017
6006c0f
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

A-docsArea: Documentation for any part of the project, including the compiler, standard library, and toolsC-enhancementCategory: An issue proposing an enhancement or a PR with one.P-mediumMedium priority

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

    Development

    No branches or pull requests

      Participants

      @steveklabnik@Thiez@frewsxcv@TimNN@scottmcm

      Issue actions

        Example in docs of swap_with_slice() · Issue #45636 · rust-lang/rust