Chapter 24: Rust Arrays

Rust Arrays.

Rust arrays are one of the simplest but also one of the most strict data structures in the language. They are very different from arrays in Python, JavaScript or even C in some ways — and understanding them properly helps you later when you move to slices, Vec, and borrowing.

Let me explain Rust arrays like your patient teacher sitting next to you with VS Code open: slowly, clearly, with many small examples you can copy-paste and run right now, everyday Hyderabad analogies, comparisons to other languages, and all the important details step by step.

1. What is an Array in Rust? (The Core Idea)

In Rust, an array is:

  • Fixed-size collection of elements of the same type
  • Size is known at compile time and part of the type
  • Stored on the stack (very fast, no heap allocation)
  • Syntax: [T; N] where T = element type, N = exact number of elements

Example:

Rust
  • Type = [f32; 7]
  • You cannot add or remove elements after creation
  • You cannot have an array of size 5 and assign it to a variable that expects size 6 — compiler error!

2. Creating Arrays – All the Common Ways

Rust

Important rule: The size must be a constant expression known at compile time — no runtime variables!

Wrong:

Rust

3. Accessing Elements – Index & Safety

Rust
  • arr[index] → direct access, panics on out-of-bounds (debug & release)
  • arr.get(index) → returns Option<&T> → safe & idiomatic

4. Iterating Over Arrays

Rust gives you several nice ways:

Rust

5. Arrays vs Slices vs Vec – Quick Comparison

Feature Array [T; N] Slice &[T] / &mut [T] Vec<T>
Size Fixed at compile time Dynamic view Dynamic / growable
Memory Stack Borrowed (stack/heap/static) Heap
Can grow? No No Yes
Type includes size? Yes — [i32; 5] ≠ [i32; 6] No — just &[i32] No — just Vec<i32>
Typical use Small fixed data Function params, views Most real-world lists
Performance Fastest (no indirection) Very fast Slightly slower (heap)

6. Passing Arrays to Functions

Rust

→ In real code you almost always use slices &[T] in function parameters — much more flexible.

7. Common Gotchas & Tips (Teacher Warnings)

  • No dynamic size — if size comes from user input or runtime → use Vec
  • Copy trait — arrays of Copy types are copied when passed by value
  • No negative indexing — arr[-1] does not work
  • Bounds panic — always prefer .get() or .get_mut() in unsafe situations
  • Stack size limit — very large arrays (e.g. [u8; 10_000_000]) may cause stack overflow → use Vec or Box<[u8]>

Practice Challenge for Today

Create a small project:

Bash

In main.rs try to:

  1. Create array of 7 days temperatures [f32; 7]
  2. Compute average using loop (sum / 7.0)
  3. Find max and min temperature
  4. Create function that takes &[f32] and prints min/max/avg
  5. Try passing array of wrong size → see compiler error

Want next?

  • Slices in much more depth (they are super important)?
  • Vec<T> deep dive with all methods?
  • Iterators on arrays/slices (.iter(), .enumerate(), .map())?
  • Or custom structs containing arrays?

Just tell me — your Rust teacher is right here with you! 🦀🚀

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *