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.
fn main() {
let person = ("Alice", 30);
println!("Name: {}, Age: {}", person.0, person.1);
}| Step | Action | Variable/Expression | Value/Result | Notes |
|---|---|---|---|---|
| 1 | Define tuple 'person' | person | ("Alice", 30) | Tuple created with two elements |
| 2 | Access first element | person.0 | "Alice" | Access name by index 0 |
| 3 | Access second element | person.1 | 30 | Access age by index 1 |
| 4 | Print output | println! | Name: Alice, Age: 30 | Output to console |
| 5 | End | - | - | Program finishes |
| Variable | Start | After Step 1 | After Step 2 | After Step 3 | Final |
|---|---|---|---|---|---|
| person | undefined | ("Alice", 30) | ("Alice", 30) | ("Alice", 30) | ("Alice", 30) |
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.