# Data Types

Rust is statically-typed (like Java), but (unlike Java) it generally infers the types. If many types are possible (like when using `parse`), then we have to specify the type. With that, let's discuss the various types!

### Integers

Many languages have a single integer type. Not Rust! Rust has five different integer types based on the length and whether they are signed (possibly negative) or unsigned (always positive):

| Length | Signed | Unsigned |
| ------ | ------ | -------- |
| 8-bit  | i8     | u8       |
| 16-bit | i16    | u16      |
| 32-bit | i32    | u32      |
| 64-bit | i64    | u64      |
| arch   | isize  | usize    |

What should you default to? Probably `i32`. It's what Rust defaults to and is generally the fastest, even on 64-bit systems.

What can you enter as an integer? Any of the following options!

| Number literals  | Example       |
| ---------------- | ------------- |
| Decimal          | `98_222`      |
| Hex              | `0xff`        |
| Octal            | `0o77`        |
| Binary           | `0b1111_0000` |
| Byte (`u8` only) | `b'A'`        |

### Floats

Floating point numbers are a bit simpler. There are only two types: `f32` and `f64`. In this case, the Rust default is `f64`.

```rust
let x = 2.0; // f64
let y: f32 = 3.0; // f32
```

### Boolean

Just like most other languages, Rust has a boolean type that has two possible values: `true` or `false`.

```rust
let t = true;
let f: bool = false;
```

### Characters

`char` represents a Unicode Scalar Value, which means it can represent more than just A-z. Here's an example:

```rust
let c = 'z';
let z = 'ℤ';
let heart_eyed_cat = '😻';
```

### Tuples

A tuple groups together any number of values with random types. Here are some examples

```rust
let tup: (i32, f64, u8) = (500, 6.4, 1); // type annotation is optional
let tup = (500, 6.4, 1);
let (x, y, z) = tup; // destructure through pattern matching
let x = tup.0 // or by getting the element at the index!
let y = tup.1
let z = tup.2
```

What happens if you go out of bounds?

![](/files/-LBuahp3MmrC-r_fzEro)

### Arrays

Arrays are similar to tuples but every element must have the same type. In Rust, once an array is created, it cannot grow or shrink. Here's an example of a common use for arrays:

```rust
let months = ["January", "February", "March", "April", "May", "June", "July",
              "August", "September", "October", "November", "December"];
```

Unlike tuples, you cannot easily destructure arrays via pattiern matching. But you can get the element at the index like so:

```rust
let arr = [1, 2, 3];
let one = arr[0];
```

Unlike the tuple out-of-bounds message, if you go out of bounds with an array, the message is very clear:

![](/files/-LBub7GSlMTpGFBTZIX3)

Since the index starts at zero, it must always be one less than the length. So this error message is telling us we are going one too far!


---

# 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://jmkoni.gitbook.io/rust/common-concepts/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.
