Every value in Rust is of a certain data type, which tells Rust what kind of data is being specified so it knows how to work with that data. We’ll look at two data type subsets: scalar
and compound
.在Rust中每一个值都有确定的变量类型,以告知Rust使用的数据是哪一种指定的类型,Rust从而得知如何使用该数据,我们将看到两种数据类型子集:scalar和compound。
请牢记Rust是一门静态语言,这意味着在编译时就必须确定数据的类型。编译器通常可以根据数据的值或我们如何使用数据来推断其类型。比如:
// 关键字 变量名: 类型注释 = 变量值
let guess: u32 = "42".parse().expect("Not a number!");
if we don't add the : u32 type annotation shown in the preceding code, Rust will display the following error, which means the complier needs more imformation from us to know which type we want to use.
$ cargo build
Compiling no_type_annotations v0.1.0 (file:///projects/no_type_annotations)
error[E0282]: type annotations needed
--> src/main.rs:2:9
|
2 | let guess = "42".parse().expect("Not a number!");
| ^^^^^
|
help: consider giving `guess` an explicit type
|
2 | let guess: _ = "42".parse().expect("Not a number!");
| +++
For more information about this error, try `rustc --explain E0282`.
error: could not compile `no_type_annotations` due to previous error
Scalar Types(标量类型)
A scalar type represents a single value. Rust has four primary scalar types: integers, floating-point numbers, Booleans, and characters.
You may recognize these from other programming languages. Let’s jump into how they work in Rust.
Integer types
| Length | Signed | Unsigned |
|--------|--------|----------|
| 8-bit | i8 | u8 |
| 16-bit | i16 | u16 |
| 32-bit | i32 | u32 |
| 64-bit | i64 | u64 |
|128-bit | i128 | u128 |
| arch | isize | usize |
Each variant can be either signed or unsigned and has an explicit size. Signed and unsigned refer to whether it’s possible for the number to be negative—in other words, whether the number needs to have a sign with it (signed) or whether it will only ever be positive and can therefore be represented without a sign (unsigned).每一种可以是有符号(具有正负标识)的或无符号的(不具有正负标识),并且具有明确的大小。有符号和无符号是指数字是否可能为负数,也就是说数字需要有符号 || 它只会是正数(因此可以无符号来表示)
标签:__,type,number,scalar,error,bit,Data,Rust From: https://www.cnblogs.com/ashet/p/18121681