Rust how to convert from array to std::raw:::Slice

4.3k views Asked by At

std::raw::Slice is defined as :

pub struct Slice<T> {
    pub data: *const T,
    pub len: uint,
}

I am trying something like this:

use std::raw::Slice as RawSlice;
let a = [1i,2,3,4];
let s : RawSlice<int>= RawSlice{data: a as *const int, 
        len : a.len()};

This doesn't compile. error: non-scalar cast: [int, ..4] as *const int. I am basically unable to figure out how to get a pointer to the beginning of the array.

A similar concern is: how to convert from [int] to *const int?

1

There are 1 answers

2
James Moughan On BEST ANSWER

You can take a pointer to the first element of the array and convert it:

&a[0] as *const int