📖 Scenario: You are building a simple program to represent points in 2D space using tuple structs in Rust. Tuple structs are like regular structs but their fields have no names, only types and positions.
🎯 Goal: Create a tuple struct called Point with two i32 fields. Then create a variable of type Point with specific coordinates, and finally print the coordinates.
📋 What You'll Learn
Create a tuple struct named Point with two i32 fields
Create a variable called origin of type Point with values 0 and 0
Create a variable called point of type Point with values 10 and 20
Print the values of point fields using tuple indexing syntax
💡 Why This Matters
🌍 Real World
Tuple structs are useful when you want to group related values without naming each field, such as coordinates, colors, or simple data containers.
💼 Career
Understanding tuple structs helps in Rust programming for system-level tasks, game development, and performance-critical applications where simple data grouping is needed.
Progress0 / 4 steps
1
Create the tuple struct Point
Write a tuple struct called Point with two i32 fields.
Rust
Hint
Use the syntax struct Name(Type1, Type2); to create a tuple struct.
2
Create variables origin and point
Create a variable called origin of type Point with values 0 and 0. Then create a variable called point of type Point with values 10 and 20.
Rust
Hint
Create variables by calling the tuple struct with values like Point(0, 0).
3
Access the fields of point
Use tuple indexing syntax to access the first and second fields of point and assign them to variables x and y.
Rust
Hint
Access tuple struct fields using dot and index, like point.0 and point.1.
4
Print the coordinates of point
Print the values of x and y in the format: Point coordinates: (10, 20) using println!.
Rust
Hint
Use println!("Point coordinates: ({}, {})", x, y); to print the 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
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.