SoFunction
Updated on 2025-03-10

Specific use of Rust scalar types

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,i8Represents an 8-bit signed integer, andi64Represents a 64-bit signed integer. Rust also supports binary (0bBeginning), octal (0oBeginning) and hexadecimal (0xThe 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,0xffRepresents a hexadecimal number, and0o77Represents 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 asusizeUnsigned 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:f32andf64, 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.0Default isf64type, and3.0Identified asf32type. 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:trueandfalse

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 typecharRepresentation, 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!