4

Something like this:

let source = [0u8; 40];
let a = source[0..11];
let b = source[11..40];
Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Constantine
  • 1,802
  • 3
  • 23
  • 37

1 Answers1

7

Use slice::split_at:

fn main() {
    let source = [0u8; 40];
    let (a, b) = source.split_at(11);
    println!("{}, {}", a.len(), b.len())
}

There's also split_at_mut, discussed in How to operate on 2 mutable slices of a Rust array.

In this case, you can also take multiple subslices because it's immutable. These may be overlapping:

fn main() {
    let source = [0u8; 40];
    let a = &source[0..11];
    let b = &source[11..40];
    println!("{}, {}", a.len(), b.len())
}

into couple of copies?

The point of slices is that copies aren't made. A slice is just a pointer to the start of the data and a length. The data itself is shared.

I want to make 'a' and 'b' arrays instead of slices

Then you should check out How to get a slice as an array in Rust?

Community
  • 1
  • 1
Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
  • And if I want to make 'a' and 'b' arrays instead of slices? "couple of copies" -> means to make a, b independent from source. – Constantine Mar 27 '17 at 14:03