0Pricing
Learn Rust Coding · Lesson

Compound Types

Tuples and arrays.

Compound Types is a free Learn Rust Coding lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Learn Rust Coding learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

What Are Compound Types?

Compound types group multiple values into one. Rust has two primitive compound types:

  • Tuples — fixed-size groups of possibly different types.
  • Arrays — fixed-size groups of the same type.
fn main() {
    let tup = (1, 2.0, 'c');
    let arr = [10, 20, 30];
    println!("{} {}", tup.0, arr[0]);
}

Creating Tuples

A tuple bundles values inside parentheses. Each element can be a different type.

The tuple's type is the combination of its element types.

fn main() {
    let person: (&str, u32, f64) = ("Alice", 30, 5.6);
    println!("{} is {} years old", person.0, person.1);
}

Accessing Tuple Elements

Access tuple elements by index using a dot, starting at 0.

  • tup.0 is the first element.
  • tup.1 is the second.
fn main() {
    let point = (3, 7);
    let x = point.0;
    let y = point.1;
    println!("x={} y={}", x, y);
}

Destructuring Tuples

You can break a tuple into separate variables in one step. This is called destructuring.

fn main() {
    let coords = (1.5, 2.5, 3.5);
    let (a, b, c) = coords;
    println!("{} {} {}", a, b, c);
}

The Unit Type

An empty tuple () is called the unit type. It represents 'no meaningful value'.

Functions that return nothing actually return ().

fn main() {
    let nothing = ();
    println!("unit value is {:?}", nothing);
}

Creating Arrays

An array holds multiple values of the same type with a fixed length, written in square brackets.

Its type is [type; length].

fn main() {
    let days: [&str; 3] = ["Mon", "Tue", "Wed"];
    let numbers = [1, 2, 3, 4, 5];
    println!("{} {}", days[1], numbers[4]);
}

Array Indexing

Access array elements with square brackets and a zero-based index.

Accessing an out-of-bounds index causes a runtime panic — Rust checks bounds for safety.

fn main() {
    let colors = ["red", "green", "blue"];
    println!("first {}", colors[0]);
    println!("last {}", colors[2]);
}

Repeated Initial Values

To fill an array with the same value, use [value; count].

This creates an array of count elements all equal to value.

fn main() {
    let zeros = [0; 5];
    println!("{:?}", zeros);
    println!("length is {}", zeros.len());
}

Array Length

The .len() method returns the number of elements in an array. Length is fixed at compile time.

If you need a growable list, use a Vec instead.

fn main() {
    let scores = [88, 92, 75, 100];
    println!("there are {} scores", scores.len());
}

Iterating an Array

Use a for loop to visit each element of an array in order.

fn main() {
    let nums = [2, 4, 6, 8];
    let mut sum = 0;
    for n in nums {
        sum += n;
    }
    println!("sum = {}", sum);
}

Tuples vs Arrays

When to use each:

  • Use a tuple for a fixed group of different types, like a coordinate plus a label.
  • Use an array for a fixed group of the same type, like a list of scores.
fn main() {
    let record = ("Bob", [90, 85, 95]);
    println!("{} first score {}", record.0, (record.1)[0]);
}

Quick Check

Choose the correct statement about Rust compound types.

Recap

Rust's compound types:

  • Tuples(a, b, c), mixed types, accessed by .0, destructurable.
  • Arrays[a, b, c], same type, fixed length, accessed by [index].
  • () is the unit type.
fn main() {
    let (name, scores) = ("Cara", [70, 80, 90]);
    let mut total = 0;
    for s in scores {
        total += s;
    }
    println!("{} total {}", name, total);
}

Frequently asked questions

Is the “Compound Types” lesson free?

Yes — the full text of “Compound Types” is free to read here on the web, and the Learn Rust Coding course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Learn Rust Coding course, upgrade to CoddyKit PRO.

What will I learn in “Compound Types”?

Tuples and arrays. You practise Learn Rust Coding with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Learn Rust Coding?

No prior experience is required. Learn Rust Coding on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Compound Types” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Learn Rust Coding lesson?

Yes. Every Learn Rust Coding lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. let and Mutability
  2. Scalar Types
  3. Compound Types
  4. Shadowing
← Back to Learn Rust Coding