[英]How can I declare a &[&T]] in rust?
I have this code I cannot compile : 我有不能编译的这段代码:
let vec1 = [1i, 2i, 3i];
let vec2 = [4i, 5i];
let v: &[&[int]] = [&vec1, &vec2];
What I want here is clear : I want to indicate v contains &[int] items, ie references to arrays of heterogenous sizes. 我在这里想要的很清楚:我想表明v包含&[int]项,即对异构大小数组的引用。 But, no matter how I tag v's type, I get compiler errors.
但是,无论我如何标记v的类型,都会出现编译器错误。 The above states
以上状态
tst.rs:8:29: 8:34 error: mismatched types: expected `&[int, ..3]`, found `&[int, ..2]` (expected array, found array)
tst.rs:8 let v: &[&[int]] = [&vec1, &vec2];
^~~~~
tst.rs:8:21: 8:35 error: mismatched types: expected `&[&[int]]`, found `[&[int, ..3], ..2]` (expected &-ptr, found array)
tst.rs:8 let v: &[&[int]] = [&vec1, &vec2];
^~~~~~~~~~~~~~
What is the solution here ? 这里有什么解决方案?
let vec1 = [1i, 2i, 3i];
let vec2 = [4i, 5i];
let v = [vec1.as_slice(), vec2.as_slice()];
let v: &[&[int]] = v.as_slice();
The first v
is of type [&[int], ..2]
and the second v
is of type &[&[int]]
. 第一个
v
为[&[int], ..2]
类型,第二个v
为&[&[int]]
。 We can't define v
of type &[&[int]]
directly because we must define storage for the [&[int], ..2]
before we can borrow it. 我们无法直接定义
&[&[int]]
类型的v
,因为我们必须先定义[&[int], ..2]
存储空间,然后才能借用它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.