文字整数值在Rust中是否具有特定类型? [英] Do literal integral values have a specific type in Rust?
本文介绍了文字整数值在Rust中是否具有特定类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在 https://doc.rust-lang.org中/book/primitive-types.html#numeric-types ,它表示为
让x = 42; //x的类型为i32
let x = 42; // x has type i32
这意味着x
的默认类型为i32
.
That means x
has the type i32
as default.
但是在 http://rustbyexample.com/cast/literals.html 中,它说那
后缀文字,它们的类型取决于它们的使用方式
Unsuffixed literal, their types depend on how they are used
我知道我不能使用i32
为向量建立索引,但是以下代码可以工作:
I know I can't use i32
to index the vector, but the following code works:
fn main() {
let v = vec![1, 2, 3, 4, 5];
let j = 1; // j has default type i32? or it has type when it is first used?
// And what is the type of 1?
println!("{}", v[1]); // is 1 a usize?
println!("{}", v[j]);
}
那么,文字整数值的类型是什么?
So, what is the type of a literal integral value?
推荐答案
查看全文