A tuple struct in Rust is a struct that looks like a tuple but has a name. It groups related values without naming each field, using just types and positions.
Click to reveal answer
beginner
How do you define a tuple struct with two fields: an i32 and a f64?
You write:
struct MyTuple(i32, f64);
This creates a tuple struct named MyTuple with two fields accessed by position.
Click to reveal answer
beginner
How do you create an instance of a tuple struct MyTuple(i32, f64) with values 10 and 3.14?
You write: <pre>let value = MyTuple(10, 3.14);</pre> This creates a variable <code>value</code> holding the tuple struct with those values.
Click to reveal answer
beginner
How do you access the second field of a tuple struct instance value?
Use dot notation with the index: <pre>let second = value.1;</pre> Fields are zero-indexed, so <code>value.1</code> is the second field.
Click to reveal answer
intermediate
Why use tuple structs instead of regular structs in Rust?
Tuple structs are useful when you want to group values without naming each field, making code shorter and simpler when field names are not needed.
Click to reveal answer
How do you define a tuple struct named Color with three u8 fields?
Astruct Color { u8, u8, u8 }
Bstruct Color(u8, u8, u8);
Clet Color = (u8, u8, u8);
Dstruct Color[u8, u8, u8];
✗ Incorrect
Tuple structs use parentheses with types inside, like struct Color(u8, u8, u8);.
Given struct Point(i32, i32);, how do you create a point at (5, 10)?
Alet p = (5, 10);
Blet p = Point { 5, 10 };
Clet p = Point(5, 10);
Dlet p = Point.new(5, 10);
✗ Incorrect
You create an instance by calling the tuple struct like a function: Point(5, 10).
How do you access the first field of a tuple struct instance p?
Ap->0
Bp[0]
Cp.first
Dp.0
✗ Incorrect
Tuple struct fields are accessed by dot and index: p.0 for the first field.
Which of these is NOT a benefit of tuple structs?
AAllows naming each field explicitly
BShorter syntax than regular structs
CCan create new types distinct from tuples
DGrouping related values without naming fields
✗ Incorrect
Tuple structs do NOT name each field explicitly; they use positions instead.
What is the correct way to define a tuple struct with no fields?
Astruct Empty();
Bstruct Empty;
Cstruct Empty {}
Dstruct Empty[];
✗ Incorrect
A tuple struct with no fields uses empty parentheses: struct Empty();.
Explain what a tuple struct is and how it differs from a regular struct in Rust.
Think about how tuples and structs combine.
You got /4 concepts.
Describe how to create and use a tuple struct instance, including accessing its fields.
Imagine a tuple with a name.
You got /4 concepts.
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
Step 1: Understand tuple struct definition
A tuple struct groups values like a tuple but has a name for the whole struct.
Step 2: Identify field access method
Fields are unnamed and accessed by position using dot and index, unlike normal structs.
Final Answer:
A struct with unnamed fields accessed by position -> Option B
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
Step 1: Recall tuple struct syntax
Tuple structs use parentheses with types inside, separated by commas, no field names.
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.