7

I want to write the end of a slice to the top of the same slice.

let mut foo = [1, 2, 3, 4, 5];

foo[..2].copy_from_slice(&[4..]); // error: multiple references to same data (mut and not)

assert!(foo, [4, 5, 3, 4, 5]);

I've seen How to operate on 2 mutable slices of a Rust array

I want the maximum performance possible (for example, by using foo.as_ptr()).

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Kerollmops
  • 303
  • 4
  • 15
  • Unfortunately there doesn't seem to exist a function similar to `collect` that fills up an `IterMut` from an iterator. – starblue Sep 21 '16 at 05:59
  • 1
    See also [How to idiomatically copy a slice?](http://stackoverflow.com/q/28219231/155423) – Shepmaster Sep 22 '16 at 00:13

4 Answers4

8

To copy data from one range inside a slice to another in general (allowing overlap), we can't even use .split_at_mut().

I would use .split_at_mut() primarily otherwise. (Is there anything that makes you think the bounds check is not going to be optimized out? Also, are you copying enough data that it's a small effect in comparison?)

Anyway, this is how you could wrap std::ptr::copy (overlap-allowing copy, a.k.a memmove) in a safe or an unsafe function.

use std::ptr::copy;
use std::ops::Range;

/// Copy the range `data[from]` onto the index `to` and following
///
/// **Panics** if `from` or `to` is out of bounds
pub fn move_memory<T: Copy>(data: &mut [T], from: Range<usize>, to: usize) {
    assert!(from.start <= from.end);
    assert!(from.end <= data.len());
    assert!(to <= data.len() - (from.end - from.start));
    unsafe {
        move_memory_unchecked(data, from, to);
    }
}

pub unsafe fn move_memory_unchecked<T: Copy>(data: &mut [T], from: Range<usize>, to: usize) {
    debug_assert!(from.start <= from.end);
    debug_assert!(from.end <= data.len());
    debug_assert!(to <= data.len() - (from.end - from.start));
    let ptr = data.as_mut_ptr();
    copy(ptr.offset(from.start as isize),
         ptr.offset(to as isize),
         from.end - from.start)
}

fn main() {
    let mut data = [0, 1, 2, 3, 4, 5, 6, 7];
    move_memory(&mut data, 2..6, 0);
    println!("{:?}", data);
    move_memory(&mut data, 0..3, 5);
    println!("{:?}", data);
}

Playground link

bluss
  • 12,472
  • 1
  • 49
  • 48
  • Being a beginner in Rust can I ask how vector elements drop would work in this case for type T when type T implements Drop Trait? – Vincenzo Maggio Sep 21 '16 at 14:22
  • 1
    Sure. `T: Copy` means that `T` is trivially copyable (byte-by-byte) and it also means `T` can not have a destructor. So we don't have to worry about drop here. (There is also no vector in this answer, just a mutable slice). – bluss Sep 21 '16 at 15:20
  • Ah yeah didn't notice the Copy constraint. Thanks. – Vincenzo Maggio Sep 21 '16 at 15:22
6

Rust 1.37 (2019-08-15) adds the library function slice::copy_within which does exactly what you want:

let mut foo = [1, 2, 3, 4, 5];

foo.copy_within(3 .. 5, 0);  // <-- THIS

assert_eq!(foo, [4, 5, 3, 4, 5]);
Nayuki
  • 17,911
  • 6
  • 53
  • 80
4

If your types implement Copy and the subslices are not overlapping:

fn main() {
    let mut v = [1, 2, 3, 4, 5, 6];

    {
        let (left, right) = v.split_at_mut(3);
        // Perform further work to ensure slices are the same length, as needed
        left.copy_from_slice(right);
    }

    assert!(v == [4, 5, 6, 4, 5, 6]);
}
Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
  • If the two slices have different sizes, the function will panic and in my case I want to copy the 5 last bytes at the front of the same slice... – Kerollmops Sep 22 '16 at 00:06
  • 1
    @Kerosene yep, that's why I put the comment *"ensure slices are the same length"*. ^_^ – Shepmaster Sep 22 '16 at 00:08
2

I found a better way to do what I want:

fn main() {
    let mut v = [1, 2, 3, 4, 5, 6];

    // scoped to restrict the lifetime of the borrows
    {
        let (left, right) = v.split_at_mut(3);
        assert!(left == [1, 2, 3]);
        assert!(right == [4, 5, 6]);
        for (l, r) in left.iter_mut().zip(right) {
            *l = *r;
        }
    }

    assert!(v == [4, 5, 6, 4, 5, 6]);
}
Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Kerollmops
  • 303
  • 4
  • 15