DataTypes in Rust | Divyansh Rai

Shared on Rust

editor-img
Divyansh Rai
Jul 19, 2022
shared in

DataTypes in Rust Rust is a statically typed language, which means that it must know the types of all variables at compile time. Today, I will throw some light on different datatypes (scalar) in rust

# 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 An integer is a number without a fractional component. This type declaration indicates that the value it’s associated with should be an unsigned integer (signed integer types start with i, instead of u) that takes up 32 bits of space. The figure shows the built-in integer types in Rust.

media

## Floating Point Types: Rust also has two primitive types for floating-point numbers, which are numbers with decimal points. Rust’s floating-point types are f32 and f64, which are 32 bits and 64 bits in size, respectively. The default type is f64 because on modern CPUs it’s roughly the same speed as f32 but is capable of more precision.

Here’s an example that shows floating-point numbers in action: fn main() { let x = 2.0; // f64 let y: f32 = 3.0; // f32 }

## Boolean Type As in most other programming languages, a Boolean type in Rust has two possible values: true and false. Booleans are one byte in size. The Boolean type in Rust is specified using bool. For example: fn main() { let t = true; let f: bool = false; // with explicit type annotation }

## Character Type Rust’s char type is the language’s most primitive alphabetic type. Here’s some examples of declaring char values: fn main() { let c = 'z'; let z: char = 'ℤ'; // with explicit type annotation let heart_eyed_cat = '😻'; }


Tagged users
editor-img
Rajat
@28-mace-windu
I don't know. I'm pretty much done with everythinghh!
Checkout related posts on: