我是否必须使用 usize 来访问向量的元素? [英] Do I have to use a usize to access elements of a vector?
问题描述
我有一个 2D 向量,它拒绝使用 i32
值进行索引,但是如果我使用 as usize
来转换这些值,它会起作用:
I have a 2D vector that rejects indexing using i32
values, but works if I cast those values using as usize
:
#[derive(Clone)]
struct Color;
struct Pixel {
color: Color,
}
fn shooting_star(p: &mut Vec<Vec<Pixel>>, x: i32, y: i32, w: i32, h: i32, c: Color) {
for i in x..=w {
for j in y..=h {
p[i][j].color = c.clone();
}
}
}
fn main() {}
当我编译时,我收到错误信息
When I compile, I get the error message
error[E0277]: the trait bound `i32: std::slice::SliceIndex<[std::vec::Vec<Pixel>]>` is not satisfied
--> src/main.rs:11:13
|
11 | p[i][j].color = c.clone();
| ^^^^ slice indices are of type `usize` or ranges of `usize`
|
= help: the trait `std::slice::SliceIndex<[std::vec::Vec<Pixel>]>` is not implemented for `i32`
= note: required because of the requirements on the impl of `std::ops::Index<i32>` for `std::vec::Vec<std::vec::Vec<Pixel>>`
如果我把代码改成有
p[i as usize][j as usize].color = c.clone();
然后一切正常.然而,这感觉就像是一个非常奇怪的选择,没有理由不被 Vec
类型处理.
Then everything works fine. However, this feels like it would be a really bizarre choice with no reason not to be handled by the Vec
type.
在文档中,有很多例子喜欢
assert_eq!(vec[0], 1);
据我所知,如果一个没有小数的普通数字默认是 i32
,那么使用 i32
来索引就没有理由不工作了.>
By my understanding, if a plain number with no decimal is by default an i32
, then there's no reason using an i32
to index shouldn't work.
推荐答案
与 Java、C# 甚至 C++ 不同,Rust 中的数字文字没有固定类型.文字的数字类型通常由编译器推断,或使用后缀(0usize
、0.0f64
等)显式声明.在这方面,assert_eq!(vec[0], 1);
中 0
文字的类型被推断为usize
,因为 Rust 只允许 Vec
索引 usize
类型的数字.
Unlike Java, C# or even C++, numeric literals in Rust do not have a fixed type. The numeric type of a literal is usually inferred by the compiler, or explicitly stated using a suffix (0usize
, 0.0f64
, and so on). In that regard, the type of the 0
literal in assert_eq!(vec[0], 1);
is inferred to be a usize
, since Rust allows Vec
indexing by numbers of type usize
only.
至于使用 usize
作为索引类型的基本原理:usize
相当于目标架构中的 word.因此,usize
可以引用运行程序的计算机的所有可能内存位置的索引/地址.因此,向量的最大可能长度是 isize
(isize::MAX == usize::MAX/2
) 中可以包含的最大可能值.为 Vec
使用 usize
大小和索引可防止创建和使用大于可用内存本身的向量.
As for the rationale behind using usize
as the indexing type: a usize
is equivalent to a word in the target architecture. Thus, a usize
can refer to the index/address of all possible memory locations for the computer the program is running on. Thus, the maximum possible length of a vector is the maximum possible value that can be contained in a isize
(isize::MAX == usize::MAX / 2
). Using usize
sizes and indices for a Vec
prevents the creation and usage of a vector larger than the available memory itself.
此外,使用一个足够大的无符号整数来引用所有可能的内存位置允许删除两个动态检查,一个,提供的大小/索引是非负的(如果 isize
是使用,此检查必须手动实施),第二,创建向量或取消引用向量的值不会导致计算机内存不足.但是,只有当向量中存储的类型适合一个词时才能保证后者.
Furthermore, the usage of an unsigned integer just large enough to refer all possible memory locations allows the removal of two dynamic checks, one, the supplied size/index is non-negative (if isize
was used, this check would have to be implemented manually), and two, creating a vector or dereferencing a value of a vector will not cause the computer to run out of memory. However, the latter is guaranteed only when the type stored in the vector fits into one word.
这篇关于我是否必须使用 usize 来访问向量的元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!