Bird
Raised Fist0
Rustprogramming~10 mins

Compound data types in Rust - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Compound data types
Start
↓
Define Compound Type
↓
Create Instance
↓
Access Elements
↓
Use Elements in Code
↓
End
This flow shows how to define, create, and use compound data types like tuples and structs in Rust.
Execution Sample
Rust
fn main() {
    let person = ("Alice", 30);
    println!("Name: {}, Age: {}", person.0, person.1);
}
This code creates a tuple with a name and age, then prints each element.
Execution Table
StepActionVariable/ExpressionValue/ResultNotes
1Define tuple 'person'person("Alice", 30)Tuple created with two elements
2Access first elementperson.0"Alice"Access name by index 0
3Access second elementperson.130Access age by index 1
4Print outputprintln!Name: Alice, Age: 30Output to console
5End--Program finishes
💡 Program ends after printing the tuple elements.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
personundefined("Alice", 30)("Alice", 30)("Alice", 30)("Alice", 30)
Key Moments - 2 Insights
Why do we use person.0 and person.1 to access tuple elements?
Tuples in Rust are accessed by position using dot and index, as shown in steps 2 and 3 of the execution table.
Can we change the values inside the tuple after creation?
By default, variables are immutable in Rust. To change tuple values, you must declare it with 'mut'. This example uses an immutable tuple.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of person.1 at step 3?
A30
B"Alice"
Cundefined
D0
💡 Hint
Check the row for step 3 in the execution_table where person.1 is accessed.
At which step does the program print the output?
AStep 2
BStep 4
CStep 1
DStep 5
💡 Hint
Look for the println! action in the execution_table.
If we want to change the age in the tuple, what must we do?
AUse person.2 to access age
BTuples cannot store numbers
CDeclare the tuple as mutable with 'mut'
DChange the tuple type to a struct
💡 Hint
Refer to the key_moments about mutability and tuple access.
Concept Snapshot
Compound data types group multiple values.
Tuples hold fixed-size, ordered elements accessed by index.
Use dot and number to access tuple elements (e.g., person.0).
Variables are immutable by default; use 'mut' to change.
Structs are named fields (not shown here).
Useful to bundle related data together.
Full Transcript
This lesson shows how Rust uses compound data types like tuples to group multiple values. We start by defining a tuple named 'person' with a name and age. Each element is accessed by its position using dot notation, for example, person.0 for the name and person.1 for the age. The program prints these values. Variables in Rust are immutable by default, so to change tuple contents, you must declare them mutable. This example uses an immutable tuple. The execution table traces each step, showing variable values and actions. The key moments clarify common confusions about tuple access and mutability. The visual quiz tests understanding of tuple indexing, output timing, and mutability requirements. The snapshot summarizes the key points for quick review.

Practice

(1/5)
1. Which of the following best describes a tuple in Rust?
easy
A. A named collection of fields with the same type.
B. A dynamically sized list of elements all of the same type.
C. A fixed-size collection of values of possibly different types grouped together.
D. A function that returns multiple values.

Solution

  1. Step 1: Understand tuple characteristics

    Tuples group multiple values of different types into one fixed-size compound value.
  2. Step 2: Compare with other options

    A dynamically sized list of elements all of the same type describes arrays/vecs. A named collection of fields describes structs. A function that returns multiple values is unrelated to tuples.
  3. Final Answer:

    A fixed-size collection of values of possibly different types grouped together. -> Option C
  4. Quick Check:

    Tuple = fixed-size, mixed types [OK]
Hint: Tuples hold fixed mixed types by position [OK]
Common Mistakes:
  • Confusing tuples with arrays (dynamic size, same type)
  • Thinking tuples have named fields like structs
  • Assuming tuples can change size after creation
2. Which of the following is the correct syntax to define a tuple with an integer and a string in Rust?
easy
A. let my_tuple = [42, "hello"];
B. let my_tuple = (42, "hello");
C. let my_tuple = {42, "hello"};
D. let my_tuple = <42, "hello">;

Solution

  1. Step 1: Recall tuple syntax

    Tuples use parentheses () with comma-separated values, e.g., (value1, value2).
  2. Step 2: Check each option

    let my_tuple = (42, "hello"); uses parentheses correctly. let my_tuple = [42, "hello"]; uses square brackets (arrays). let my_tuple = {42, "hello"}; uses braces (blocks or structs). let my_tuple = <42, "hello">; uses angle brackets which are invalid here.
  3. Final Answer:

    let my_tuple = (42, "hello"); -> Option B
  4. Quick Check:

    Tuple syntax uses () [OK]
Hint: Tuples use parentheses () not brackets [] or braces {} [OK]
Common Mistakes:
  • Using square brackets [] instead of parentheses ()
  • Using braces {} which define blocks or structs
  • Using angle brackets <> which are invalid here
3. What is the output of the following Rust code?
let person = ("Alice", 30);
println!("{} is {} years old", person.0, person.1);
medium
A. Alice is 30 years old
B. ("Alice", 30) is {} years old
C. person.0 is person.1 years old
D. Compilation error due to tuple access

Solution

  1. Step 1: Understand tuple indexing

    Tuple elements are accessed by position using dot and index, e.g., person.0 is "Alice", person.1 is 30.
  2. Step 2: Analyze the println! macro

    The placeholders {} are replaced by person.0 and person.1 values, printing "Alice is 30 years old".
  3. Final Answer:

    Alice is 30 years old -> Option A
  4. Quick Check:

    Tuple access person.0 and person.1 works [OK]
Hint: Access tuple elements by .0, .1, etc. [OK]
Common Mistakes:
  • Trying to access tuple elements by name instead of position
  • Confusing tuple with array indexing syntax
  • Expecting tuple to print as a whole automatically
4. Identify the error in this Rust struct definition and fix it:
struct Point {
    x: i32,
    y i32,
}
medium
A. Trailing comma after last field is not allowed.
B. Struct fields cannot be integers.
C. Struct names must be lowercase.
D. Missing colon after y in struct field declaration.

Solution

  1. Step 1: Check struct field syntax

    Each field must have a name, colon, and type. The field 'y i32' is missing a colon.
  2. Step 2: Validate other options

    Fields can be integers (i32). Struct names are PascalCase by convention. Trailing commas are allowed in Rust structs.
  3. Final Answer:

    Missing colon after y in struct field declaration. -> Option D
  4. Quick Check:

    Field syntax requires colon between name and type [OK]
Hint: Struct fields need name: type syntax [OK]
Common Mistakes:
  • Omitting colon between field name and type
  • Thinking trailing commas are errors
  • Using lowercase struct names (allowed but not idiomatic)
5. Given the struct and tuple below, which code correctly creates a struct instance from the tuple values?
struct Color {
    red: u8,
    green: u8,
    blue: u8,
}

let rgb = (255, 100, 50);
hard
A. let color = Color { red: rgb.0, green: rgb.1, blue: rgb.2 };
B. let color = Color(rgb.0, rgb.1, rgb.2);
C. let color = Color { 255, 100, 50 };
D. let color = (red: 255, green: 100, blue: 50);

Solution

  1. Step 1: Understand struct initialization

    Structs with named fields require field names and values in braces, e.g., Color { red: val, green: val, blue: val }.
  2. Step 2: Use tuple values correctly

    Tuple elements are accessed by position: rgb.0, rgb.1, rgb.2. let color = Color { red: rgb.0, green: rgb.1, blue: rgb.2 }; uses this correctly.
  3. Step 3: Check incorrect options

    let color = Color(rgb.0, rgb.1, rgb.2); uses tuple struct syntax, but Color is not a tuple struct. let color = Color { 255, 100, 50 }; omits field names. let color = (red: 255, green: 100, blue: 50); is invalid syntax.
  4. Final Answer:

    let color = Color { red: rgb.0, green: rgb.1, blue: rgb.2 }; -> Option A
  5. Quick Check:

    Struct init needs field names with values [OK]
Hint: Use field names when creating structs from tuples [OK]
Common Mistakes:
  • Trying to initialize struct without field names
  • Using tuple struct syntax for normal structs
  • Incorrect syntax for struct initialization
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard