# 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

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

float are of twi types

**f32** = 32 bits **f64** = 64 bits

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

### Boolean & Characters

**bool** used for decision making . True or False

```rust
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

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

### Arrays and Tuples

array creation

```rust
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

```rust
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

```rust
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 (.)

```rust
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

```rust
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

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

from conversion from slice (\&str) to string

**.to\_string() or String::from()**

```rust
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()**

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


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://overflow.kshitijraj.dev/knowledge-base/rust/data-types.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
