Bird
Raised Fist0
Rustprogramming~10 mins

Tuple structs in Rust - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Tuple structs
Define tuple struct
↓
Create instance with values
↓
Access fields by position
↓
Use instance in code
↓
End
Tuple structs are like named tuples with types, defined once, then created with values and accessed by position.
Execution Sample
Rust
struct Color(u8, u8, u8);

fn main() {
    let black = Color(0, 0, 0);
    println!("Black RGB: {}, {}, {}", black.0, black.1, black.2);
}
Defines a tuple struct Color, creates black with RGB 0,0,0, then prints its fields.
Execution Table
StepActionEvaluationResult
1Define tuple struct Color(u8, u8, u8)Creates a new type Color with 3 u8 fieldsType Color ready
2Create instance black = Color(0, 0, 0)Calls constructor with values 0,0,0black is Color(0,0,0)
3Access black.0Get first field of black0
4Access black.1Get second field of black0
5Access black.2Get third field of black0
6Print valuesPrints 'Black RGB: 0, 0, 0'Output: Black RGB: 0, 0, 0
💡 All steps complete, program ends after printing.
Variable Tracker
VariableStartAfter Step 2Final
blackundefinedColor(0, 0, 0)Color(0, 0, 0)
Key Moments - 2 Insights
Why do we access fields with .0, .1, .2 instead of names?
Tuple structs use positional fields without names, so you access them by index like black.0 (see execution_table steps 3-5).
Is Color a variable or a type?
Color is a type defined by the tuple struct (step 1), black is a variable instance of that type (step 2).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of black.1 at step 4?
A0
B1
Cundefined
DColor(0,0,0)
💡 Hint
Check execution_table row with Step 4, Access black.1
At which step is the tuple struct type Color defined?
AStep 2
BStep 3
CStep 1
DStep 6
💡 Hint
Look at execution_table row Step 1, defining Color
If we change black = Color(255, 255, 255), what would black.2 be at step 5?
A0
B255
Cundefined
DColor(255,255,255)
💡 Hint
Refer to variable_tracker and execution_table steps 2 and 5 for field values
Concept Snapshot
Tuple structs define a named type with unnamed fields.
Syntax: struct Name(Type1, Type2, ...);
Create instance: let x = Name(val1, val2);
Access fields by position: x.0, x.1, ...
Useful for simple grouped data with type safety.
Full Transcript
This example shows how to define a tuple struct named Color with three u8 fields. Then we create an instance black with values 0,0,0. We access each field by position using .0, .1, and .2. Finally, we print the values. The execution table traces each step: defining the type, creating the instance, accessing fields, and printing. The variable tracker shows black's value after creation. Key moments clarify why fields use numeric access and the difference between type and variable. The quiz tests understanding of field access, definition step, and changing values.

Practice

(1/5)
1. What is a tuple struct in Rust?
easy
A. A struct with named fields accessed by name
B. A struct with unnamed fields accessed by position
C. A function that returns a tuple
D. A tuple with named elements

Solution

  1. Step 1: Understand tuple struct definition

    A tuple struct groups values like a tuple but has a name for the whole struct.
  2. Step 2: Identify field access method

    Fields are unnamed and accessed by position using dot and index, unlike normal structs.
  3. Final Answer:

    A struct with unnamed fields accessed by position -> Option B
  4. Quick Check:

    Tuple struct = unnamed fields + named struct [OK]
Hint: Tuple structs have unnamed fields accessed by dot and index [OK]
Common Mistakes:
  • Confusing tuple structs with normal structs having named fields
  • Thinking tuple structs are just tuples without names
  • Assuming fields are accessed by name
2. Which of the following is the correct syntax to define a tuple struct named Color with three u8 fields?
easy
A. struct Color(u8, u8, u8);
B. struct Color { u8, u8, u8 }
C. struct Color(u8 u8 u8);
D. struct Color = (u8, u8, u8);

Solution

  1. Step 1: Recall tuple struct syntax

    Tuple structs use parentheses with types inside, separated by commas, no field names.
  2. Step 2: Check each option

    struct Color(u8, u8, u8); matches correct syntax: struct Name(Type, Type, ...); others have wrong braces, missing commas, or wrong assignment.
  3. Final Answer:

    struct Color(u8, u8, u8); -> Option A
  4. Quick Check:

    Tuple struct syntax = struct Name(Type, Type, ...); [OK]
Hint: Tuple structs use parentheses with types, no field names [OK]
Common Mistakes:
  • Using curly braces instead of parentheses
  • Omitting commas between types
  • Using equals sign instead of semicolon
3. What will be the output of this Rust code?
struct Point(i32, i32);
fn main() {
let p = Point(10, 20);
println!("{}", p.1);
}
medium
A. 20
B. Point(10, 20)
C. 10
D. Compilation error

Solution

  1. Step 1: Understand tuple struct field indexing

    Fields are accessed by position starting at 0, so p.0 is 10, p.1 is 20.
  2. Step 2: Identify printed value

    The code prints p.1, which is the second field, value 20.
  3. Final Answer:

    20 -> Option A
  4. Quick Check:

    p.1 = 20 [OK]
Hint: Tuple struct fields start at index 0, p.1 is second field [OK]
Common Mistakes:
  • Confusing field index starting at 1 instead of 0
  • Expecting full struct print without Debug trait
  • Assuming syntax error due to dot notation
4. Identify the error in this Rust code:
struct Size(u32, u32);
fn main() {
let s = Size { 100, 200 };
println!("{}", s.0);
}
medium
A. Missing semicolon after struct definition
B. Accessing field with dot notation is invalid
C. Using curly braces instead of parentheses to create tuple struct instance
D. Tuple structs cannot have u32 fields

Solution

  1. Step 1: Check instance creation syntax

    Tuple struct instances must be created using parentheses, e.g., Size(100, 200), not curly braces.
  2. Step 2: Verify field access and other syntax

    Accessing fields with dot and index is correct; semicolon is present; u32 fields are allowed.
  3. Final Answer:

    Using curly braces instead of parentheses to create tuple struct instance -> Option C
  4. Quick Check:

    Tuple struct instance = parentheses, not braces [OK]
Hint: Create tuple struct instances with parentheses, not braces [OK]
Common Mistakes:
  • Using braces {} instead of parentheses () for tuple struct instances
  • Thinking dot notation is invalid for tuple structs
  • Assuming field types are restricted
5. Given the tuple struct struct Rectangle(u32, u32);, how can you write a function area that takes a Rectangle and returns its area (width * height)?
hard
A. fn area(r: Rectangle) -> u32 { r.0 + r.1 }
B. fn area(r: Rectangle) -> u32 { r[0] * r[1] }
C. fn area(r: Rectangle) -> u32 { r.width * r.height }
D. fn area(r: Rectangle) -> u32 { r.0 * r.1 }

Solution

  1. Step 1: Access tuple struct fields by index

    Tuple struct fields are accessed by dot and index, e.g., r.0 and r.1.
  2. Step 2: Calculate area correctly

    Area is width times height, so multiply r.0 and r.1.
  3. Final Answer:

    fn area(r: Rectangle) -> u32 { r.0 * r.1 } -> Option D
  4. Quick Check:

    Use r.0 and r.1 to multiply for area [OK]
Hint: Use dot and index to access tuple struct fields for calculations [OK]
Common Mistakes:
  • Using indexing syntax like r[0] which is invalid
  • Trying to access fields by name when tuple struct has none
  • Adding fields instead of multiplying for area
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