-
Notifications
You must be signed in to change notification settings - Fork 13.1k
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
Comments
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]
} |
Nice solution... so do you suggest me to close down this enhancement request? |
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 |
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(). |
It would be great to get a doc PR for all of |
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`
Opened a PR for this: #46219 |
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`
…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`
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):
The text was updated successfully, but these errors were encountered: