Data types in Rust

 Data types in Rust

Description

  • Every value in Rust has a unique data type.
  • Rust automatically define primitive data type without explaining it explicitly.
  • Rust surely a statically typed language, which describe that it must know the kinds of all variables at compile time.

Data types in RUST:

Scalar type

A scalar type represents one value. Rust has four primary scalar types.

○ Integer.

○ Floating-point.

○ Boolean.

○ Character

Compound type

The Compound type can group multiple values into one type. Rust has two primitive compound types.

○ Tuples

○ Arrays

Scalar type

Integer type

Length                                     Signed                                     Unsigned

8-bit                                             i8                                               u8

16-bit                                           i16                                             u16

32-bit                                           i32                                             u32

64-bit                                           i64                                             u64

Arch                                             isize                                          usize

  • Each integer in Rust are often either signed or unsigned and has a particular size.
  • The isize and usize types depends on the type of computer our program is running on: 64 bits if we’re on a 64-bit architecture and 32 bits if you’re on a 32-bit architecture.
  • Once we do not declare type of an integer then Rust will by default take i32 for an integer.
  • Each signed variant may store numbers from -(2n – 1) to 2n – 1 – 1 inclusive.
  • The unsigned variants may store numbers from 0 to 2n – 1.let variable: u32 = 100; we will write integer literals in any of the forms given below

Number literals Example

  • Decimal                                      98_222
  • Hex                                             0xff
  • Octal                                           0o77
  • Binary                                         0b1111_0000
  • Byte                                            (u8 only) b’A’

Floating-Point Types:

  • The Rust has also two primitive types for floating-point numbers, these are numbers having decimal points.
  • The Rust’s floating-point types are included f32 and f64, these are 32 bits and 64 bits in size, respectively.
  • If we don’t declare type of floating point then Rust will by default take f64 for that floating point type.
  • The f32 type has a single-precision float, and f64 has double precision.
  • let variable: f32 = 1.23;

Boolean Type:

  • In Rust a Boolean type has two possible values: true and false.
  • Booleans are one byte in size.
  • In Rust the Boolean type  is specified using bool.
  • let variable: bool = true;

Character Type:

  • Rust supports letters too.
  • Char type in Rust is that the language’s most primitive alphabetic type.
  • The characters are specified with single quotes, as against string literals, which use quotation mark .
  • Char type of Rust is four bytes in size and represents a Unicode Scalar Value.
  • let c = ‘z’;

Compound Type

Tuple Type:

  •  The tuple may be a general way of grouping together some number of other values with a spread of types.
  • The Tuples have a hard and fast length, once declared; they can’t grow or shrink in size.Contain heterogeneous data.
  • We generate a tuple by writing a comma-separated list of values inside parentheses.We will access a tuple element directly by employing a period (.) followed by the index of the worth we would like to access.

Array Type:

  • To possess differently a set of multiple values is with an array contains homogeneous data.
  • Each and every element of an array must have an equivalent type not type of a Tuple.
  • With square brackets writing an array’s type is completed  containing the sort of every element within the array followed by a semicolon and therefore the number of elements within that the array.We would access elements of an array using indexing in bracket .
  • The array isn’t flexible and not allowed to grow or shrink in size.
  • The Declaration: let array = [1, 2, 3, 4, 5];
  • Accessing an array element: let value1 = array[0];
  • Example let a = [1, 2, 3, 4, 5];println!(“The value of}
  • Result: the worth of element is: 4 
  • element is: {}”, a[element]);
  • let element = 3;
  • fn main() {

1 thought on “Data types in Rust”

Leave a Comment