fn main() {
let a = [1, 2, 3, 4, 5];
let nice_slice = a[1..4];
if nice_slice == [2, 3, 4] {
println!("Nice slice!");
} else {
println!("Not quite what I was expecting... I see: {:?}", nice_slice);
}
}
the size for values of type [{integer}]
cannot be known at compilation time [E0277]
为什么这行加个&就好了
加了类型就多了个&,难道这类型size就可以known at compilation time?
1
共 1 条评论, 1 页
评论区
写评论切片的类型标记为 &[T]。 &a才是切片的正确写法,不加&,a就是个数组啊,[1..4]确定切片的范围。 &[1,2,3,4,5][1..4]例如这样个切片在数组[1,2,3,4,5]创建一个切片,范围从下表1到4. [1,2,3,4,5][1]是数组[1,2,3,4,5]取1位置的值,下标写成[1..4],编译器就不知道怎么计算位置了。