问题描述
我正在制作一个函数,它生成一个大小为 n 个随机数的数组,但我对 while 的比较抛出了一个错误.
while ar.len() as i32 <尺寸 { }
抱怨:预期 !
、(
、+
、、
、之一:
、<
或 >
,找到 {
.
如果我删除 as i32
它会抱怨 mismatch types
并且如果我将 as usize
添加到 size 变量,那么它不会'不要抱怨.
当您从较小的类型转换为较大的类型时,您不会丢失任何数据,但数据现在会占用更多空间.
当您从较大的类型转换为较小的类型时,可能会丢失一些数据,但数据占用的空间会更少.
假设我有一个 1 大小的盒子,可以容纳数字 0 到 9,另一个盒子大小为 2,可以容纳数字 0 到 99.>
如果我想存储数字 7;两个盒子都可以使用,但如果我使用较大的盒子,我会留下空间.我可以毫不费力地将值从较小的框移到较大的框.
如果我想存储数字 42;只有一个盒子能装下这个数字:较大的那个.如果我尝试将数字塞进较小的盒子中,则会丢失一些东西,通常是数字的上部.在这种情况下,我的 42 将变成 2!糟糕!
另外,signed和unsigned也有作用;当您在有符号数和无符号数之间进行转换时,您可能会错误地解释该值,因为像 -1 这样的数字会变成 255!
另见:
在这个特殊案例中,它有点复杂.usize
定义为指针大小的整数",通常是机器的本机大小.在 64 位 x64 处理器上,这意味着 usize
是 64 位,而在 32 位 x86 处理器上,它将是 32 位.
将 usize
转换为 i32
将根据您运行的机器类型进行不同的操作.
您收到的错误消息是因为您尝试过的代码在语法上不正确,并且编译器没有给出正确的错误消息.
你真的很想打字
while (ar.len() as i32)
括号有助于正确应用优先级.
为了安全起见,我将转换为更大的值:
while ar.len()
另见:
I am making a function that makes a array of size n random numbers but my comparison for the while throws an error.
while ar.len() as i32 < size { }
Complains with: expected one of !
, (
, +
, ,
, ::
, <
, or >
, found {
.
If I remove the as i32
it complains with mismatch types
and if I add a as usize
to the size variable then it doesn't complain.
When you cast from a smaller-sized type to a larger one, you won't lose any data, but the data will now take up more space.
When you cast from a larger-sized type to a smaller one, you might lose some of your data, but the data will take up less space.
Pretend I have a box of size 1 that can hold the numbers 0 to 9 and another box of size 2 that can hold the numbers 0 to 99.
If I want to store the number 7; both boxes will work, but I will have space left over if I use the larger box. I could move the value from the smaller box to the larger box without any trouble.
If I want to store the number 42; only one box can fit the number: the larger one. If I try to take the number and cram it in the smaller box, something will be lost, usually the upper parts of the number. In this case, my 42 would be transformed into a 2! Oops!
In addition, signed and unsigned plays a role; when you cast between signed and unsigned numbers, you might be incorrectly interpreting the value, as a number like -1 becomes 255!
See also:
In this particular case, it's a bit more complicated. A usize
is defined to be a "pointer-sized integer", which is usually the native size of the machine. On a 64-bit x64 processor, that means a usize
is 64 bits, and on a 32-bit x86 processor, it will be 32 bits.
Casting a usize
to a i32
thus will operate differently depending on what type of machine you are running on.
The error message you get is because the code you've tried isn't syntactically correct, and the compiler isn't giving a good error message.
You really want to type
while (ar.len() as i32) < size { }
The parenthesis will help the precedence be properly applied.
To be on the safe side, I'd cast to the larger value:
while ar.len() < size as usize { }
See also:
- How do I convert a usize to a u32 using TryFrom?
- How to idiomatically convert between u32 and usize?
- Why is type conversion from u64 to usize allowed using `as` but not `From`?
这篇关于从“usize"转换到“i32"与其他方式有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!