# Arrays & Slices

## Array

* An array is a collection of objects of the same type `T`, stored in contiguous memory.
* Arrays are created using brackets `[]`, and their length, which is known at compile time, is part of their type signature `[T; length]`.
* Arrays are stack allocated.
* Arrays own their data.
* Arrays can be automatically borrowed as slices.

  ```rust
  let m = [1, 2, 3]; // Rust will infer the type and length
  let n: [i32; 3] = [1, 2, 3];
  ```

## Slice

* Slices are similar to arrays, but their length is not known at compile time.
* Slices are views into data; they do not own the data.
* Slices are references and follow Rust's borrowing rules.
* Slices are a fat pointer:

  * A reference to the start of the data.
  * The length of the slice.

  ```rust
  let x = [1, 2, 3];
  let slice = &x[1..]; // A slice starting from index 1
  ```

## Reference

* <https://doc.rust-lang.org/rust-by-example/primitives/array.html>


---

# 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://bagus-cahyono.gitbook.io/programming-notes/rust/arrays_and_slices.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.
