Shailesh Kumar
Shailesh Kumar

Reputation: 6957

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

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?

Upvotes: 3

Views: 4361

Answers (1)

James Moughan
James Moughan

Reputation: 413

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

&a[0] as *const int

Upvotes: 8

Related Questions