In Rust, data types are divided into scalar types and composite types. This blog will focus on Rust's scalar types, including integer types, floating point types, boolean types, and character types.
Integer Type
Rust provides a variety of integer types, divided into signed integers and unsigned integers. Signed integers represent positive, zero, or negative numbers, while unsigned integers represent only non-negative numbers.
Signed integer (i type)
let n1: i8 = 10; let n2: i64 = 0b1111_000;
In the above code,i8
Represents an 8-bit signed integer, andi64
Represents a 64-bit signed integer. Rust also supports binary (0b
Beginning), octal (0o
Beginning) and hexadecimal (0x
The integer representation of the surface quantity at the beginning).
Unsigned integer (type u)
let n3 = 0xff; // Hexadecimal representationlet n4: u64 = 0o77; // Octal representation
In the above code,0xff
Represents a hexadecimal number, and0o77
Represents an octal number. Rust's integer type can flexibly use different denominations, which facilitates developers to choose the appropriate denomination according to their needs.
Operating system related integer types
let n6: usize = 564; // The length is determined by the operating system
Rust provides operating system-related integer types such asusize
Unsigned integer representing the size of a pointer. This makes the code more portable and adapts to the needs of different platforms.
Floating point type
Rust supports two floating point types:f32
andf64
, representing single-precision floating-point numbers and double-precision floating-point numbers respectively.
let x = 2.0; // Default is f64let y: f32 = 3.0; // Specified as f32
In the above code,2.0
Default isf64
type, and3.0
Identified asf32
type. Rust's floating point type provides sufficient precision while allowing developers to choose the right precision to balance performance and accuracy.
Boolean type
Rust's boolean type has two values:true
andfalse
。
let t = true; let f: bool = false;
Boolean types play an important role in conditional judgment and logical operations, making the code clearer and readable.
Character Type
Rust's character typechar
Representation, used to store a single Unicode character.
let c1 = 'z'; let y: char = 'c';
In the above code,'z'
and'c'
All are literals of character types. Rust's character type supports Unicode characters, making it more convenient to process multilingual text.
Overall, Rust's scalar types provide a wealth of choices to suit the needs of various data representations. With a clear type system and flexible literal notation, Rust shows a strong advantage in handling basic data types.
This is the end of this article about the specific use of Rust scalar types. For more related Rust scalar types, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!