[英]Rust how to convert from array to std::raw:::Slice
std::raw::Slice
定義為:
pub struct Slice<T> {
pub data: *const T,
pub len: uint,
}
我正在嘗試這樣的事情:
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()};
這不編譯。 error: non-scalar cast: [int, ..4] as *const int
。 我基本上無法弄清楚如何獲得指向數組開頭的指針。
類似的問題是:如何從[int]轉換為* const int?
您可以獲取指向數組的第一個元素的指針並進行轉換:
&a[0] as *const int
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.