Data Types
Data Types
Rust has two types of data types
Scalar stores single values. Ex: Int ,Chars and Boolean
Compound stores multiple values . Ex : Arrays and Tuples
These data types are built in into Rust's standard library and are stored in the stack.
Custom data structures are allowed by the user in rust to create and are stored in a heap.
Int & Float
Negative int should be declared as signed, as unsigned int can be larger than signed
Allocate different sizes for an int in multiples of 8 bits: 8,16,32,64,128
unsigned bit u8 = 0 to 255 signed bits i8 = -128 to 127
float are of twi types
f32 = 32 bits f64 = 64 bits
Boolean & Characters
bool used for decision making . True or False
char is a single letter or numbers , represented via an ASCII number. Rust uses 4 bytes, which allows to use any character in UTF-32
Arrays and Tuples
array creation
here i32 is the 32 bit int data type and 5 no.of elements
making large arrays
an array of 1000 elements is created and the first element is index will be [0]
accessing a array
Tuple is declare using regular parenthesise for both data types and initial values
Index of tuple is accessed via square braces and period (.)
deconstruction can be used to assign friendly name to elements
Strings
Rust comprises of two types of strings
Strings which is mutable
&str which is immutable
from conversion from slice (&str) to string
.to_string() or String::from()
from string to slice
&str or as_str()
Last updated
Was this helpful?