Skip to content
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

Example in docs of swap_with_slice() #45636

Closed
leonardo-m opened this issue Oct 30, 2017 · 6 comments
Closed

Example in docs of swap_with_slice() #45636

leonardo-m opened this issue Oct 30, 2017 · 6 comments
Assignees
Labels
A-docs Area: documentation for any part of the project, including the compiler, standard library, and tools C-enhancement Category: An issue proposing an enhancement or a PR with one. P-medium Medium priority

Comments

@leonardo-m
Copy link

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);
@Thiez
Copy link
Contributor

Thiez commented Oct 30, 2017

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
Copy link
Author

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

@Thiez
Copy link
Contributor

Thiez commented Oct 30, 2017

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
Copy link
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
Copy link
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.

@leonardo-m leonardo-m changed the title slice swap_nonoverlapping Example in docs of swap_with_slice() Oct 30, 2017
@TimNN TimNN added C-enhancement Category: An issue proposing an enhancement or a PR with one. A-docs Area: documentation for any part of the project, including the compiler, standard library, and tools labels Oct 31, 2017
@steveklabnik steveklabnik added the P-medium Medium priority label Oct 31, 2017
@frewsxcv frewsxcv self-assigned this Nov 23, 2017
frewsxcv added a commit that referenced this issue Nov 23, 2017
Fixes #45636.

- Demonstrate how to use these operations with slices of differing
  lengths
- Demonstrate how to swap/copy/clone sub-slices of a slice using
  `split_at_mut`
@frewsxcv
Copy link
Member

Opened a PR for this: #46219

frewsxcv added a commit that referenced this issue Nov 24, 2017
Fixes #45636.

- Demonstrate how to use these operations with slices of differing
  lengths
- Demonstrate how to swap/copy/clone sub-slices of a slice using
  `split_at_mut`
kennytm added a commit to kennytm/rust that referenced this issue Nov 29, 2017
…uillaumeGomez

Improve documentation for slice swap/copy/clone operations.

Fixes rust-lang#45636.

- Demonstrate how to use these operations with slices of differing
  lengths
- Demonstrate how to swap/copy/clone sub-slices of a slice using
  `split_at_mut`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-docs Area: documentation for any part of the project, including the compiler, standard library, and tools C-enhancement Category: An issue proposing an enhancement or a PR with one. P-medium Medium priority
Projects
None yet
Development

No branches or pull requests

6 participants