Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

rust

unsafe

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?

like image 435
Shailesh Kumar Avatar asked Jul 16 '26 18:07

Shailesh Kumar


1 Answers

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

&a[0] as *const int
like image 161
James Moughan Avatar answered Jul 18 '26 13:07

James Moughan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!