简体   繁体   English

Rust如何从数组转换为std :: raw ::: Slice

[英]Rust how to convert from array to std::raw:::Slice

std::raw::Slice is defined as : std::raw::Slice定义为:

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 . 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? 类似的问题是:如何从[int]转换为* const int?

您可以获取指向数组的第一个元素的指针并进行转换:

&a[0] as *const int

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM