0
0
Rustprogramming~10 mins

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

Choose your learning style9 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.