Overflow
  • 👋Welcome To Overflow
  • Knowledge Base
    • Linux Kernel Development
      • Kernel Basic
    • Rust
      • Getting Started
      • Data Types
Powered by GitBook
On this page
  • Data Types
  • Int & Float
  • Boolean & Characters
  • Arrays and Tuples
  • Strings

Was this helpful?

  1. Knowledge Base
  2. Rust

Data Types

Data Types

Rust has two types of data types

  1. Scalar stores single values. Ex: Int ,Chars and Boolean

  2. 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

fn main()
{
    let int : i8 = -20;
    let uint : u8 =20;
}

float are of twi types

f32 = 32 bits f64 = 64 bits

fn main()
{
    let f32 : f32 = 8.2;
    let f64 : f64 = 3.14;
}

Boolean & Characters

bool used for decision making . True or False

fn main()
{
    let bool: bool = true;
}

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

fn main()
{
    let char : char = 'A';
}

Arrays and Tuples

array creation

fn main()
{
    let array:[i32;5] = [1,2,3,4,5];
}

here i32 is the 32 bit int data type and 5 no.of elements

making large arrays

fn main()
{
    let array:[i32; 1000] = [0; 1000];
}

an array of 1000 elements is created and the first element is index will be [0]

accessing a array

fn  main()
{
    let array:[i32; 5] = [1,2,3,4,5];
    println!("{}", array[2]);
}

Tuple is declare using regular parenthesise for both data types and initial values

Index of tuple is accessed via square braces and period (.)

fn main()
{
    let tuple:(&str, &str, i32) =("H","I",32);
    println!("{} {} byte int {}", tuple.0,  tuple.1, tuple.2);
}

deconstruction can be used to assign friendly name to elements

fn main()
{
    let tuple:(&str, &str, i32) =("H","I",32);
    let (first, last, i):(&str, &str, i32)= tuple;
    println!("{} {} byte int {}", first,last,i);
}

Strings

Rust comprises of two types of strings

  1. Strings which is mutable

  2. &str which is immutable

fn main()
{
    let slice:&str = "Rust Rover";
}

from conversion from slice (&str) to string

.to_string() or String::from()

fn main()
{
    let slice:&str = "Rust Rover";
    
    let str:string=slice.to_string();
    //or
    let str:string="Rust Rover".to_string();
    //or
    let str:string = String::from("Rust Rover");
}

from string to slice

&str or as_str()

fn main()
{
    let co:String = String::form ("Rust Rover");
    
    let slice : (&str) = &co;
    //or
    let slice:&str = str.as_str();
}
PreviousGetting Started

Last updated 1 year ago

Was this helpful?

Page cover image