Compound data types group multiple values into one type. In Rust, common compound types are tuples and arrays.
Click to reveal answer
beginner
How do you define a tuple in Rust?
A tuple is defined by listing values inside parentheses, separated by commas. Example: <code>let t = (10, 20, 30);</code>
Click to reveal answer
beginner
How do you access elements in a tuple?
Use a dot and the index number starting at zero. Example: <code>let x = t.1;</code> gets the second element.
Click to reveal answer
intermediate
What is the difference between arrays and tuples in Rust?
Arrays hold multiple values of the same type and have fixed length. Tuples can hold different types and also have fixed length.
Click to reveal answer
beginner
How do you declare an array of five integers in Rust?
Use square brackets with the type and length. Example: <code>let arr: [i32; 5] = [1, 2, 3, 4, 5];</code>
Click to reveal answer
Which of these is a valid tuple in Rust?
A(10, "hello", true)
B[10, "hello", true]
C{10, "hello", true}
D<10, "hello", true>
✗ Incorrect
Tuples use parentheses and can hold different types, like (10, "hello", true).
How do you access the third element of a tuple named t?
At.2
Bt[2]
Ct(2)
Dt.3
✗ Incorrect
Tuple elements are accessed with dot and zero-based index, so t.2 is the third element.
What is true about Rust arrays?
AThey can hold different types of values.
BThey have a fixed size and hold values of the same type.
CThey can grow dynamically like vectors.
DThey are accessed using dot notation.
✗ Incorrect
Rust arrays have fixed size and all elements must be the same type.
Which syntax correctly declares an array of 3 floats?
Alet arr = {1.0, 2.0, 3.0};
Blet arr = (1.0, 2.0, 3.0);
Clet arr: [f32; 3] = [1.0, 2.0, 3.0];
Dlet arr: (f32, 3) = [1.0, 2.0, 3.0];
✗ Incorrect
Arrays use square brackets with type and length: [f32; 3].
What happens if you try to access an array element out of bounds in Rust?
AThe program compiles but returns zero.
BIt returns the last element automatically.
CIt silently ignores the access.
DRust panics at runtime and stops the program.
✗ Incorrect
Rust checks array bounds at runtime and panics if out of bounds to prevent errors.
Explain what compound data types are in Rust and give examples.
Think about how multiple values are grouped in Rust.
You got /3 concepts.
Describe how to declare and access elements in a tuple and an array in Rust.
Remember tuples use parentheses and dot notation; arrays use square brackets.
You got /4 concepts.
Practice
(1/5)
1. Which of the following best describes a tuple in Rust?
easy
A. A named collection of fields with the same type.
B. A dynamically sized list of elements all of the same type.
C. A fixed-size collection of values of possibly different types grouped together.
D. A function that returns multiple values.
Solution
Step 1: Understand tuple characteristics
Tuples group multiple values of different types into one fixed-size compound value.
Step 2: Compare with other options
A dynamically sized list of elements all of the same type describes arrays/vecs. A named collection of fields describes structs. A function that returns multiple values is unrelated to tuples.
Final Answer:
A fixed-size collection of values of possibly different types grouped together. -> Option C
Quick Check:
Tuple = fixed-size, mixed types [OK]
Hint: Tuples hold fixed mixed types by position [OK]
Common Mistakes:
Confusing tuples with arrays (dynamic size, same type)
Thinking tuples have named fields like structs
Assuming tuples can change size after creation
2. Which of the following is the correct syntax to define a tuple with an integer and a string in Rust?
easy
A. let my_tuple = [42, "hello"];
B. let my_tuple = (42, "hello");
C. let my_tuple = {42, "hello"};
D. let my_tuple = <42, "hello">;
Solution
Step 1: Recall tuple syntax
Tuples use parentheses () with comma-separated values, e.g., (value1, value2).
Step 2: Check each option
let my_tuple = (42, "hello"); uses parentheses correctly. let my_tuple = [42, "hello"]; uses square brackets (arrays). let my_tuple = {42, "hello"}; uses braces (blocks or structs). let my_tuple = <42, "hello">; uses angle brackets which are invalid here.
Final Answer:
let my_tuple = (42, "hello"); -> Option B
Quick Check:
Tuple syntax uses () [OK]
Hint: Tuples use parentheses () not brackets [] or braces {} [OK]
Common Mistakes:
Using square brackets [] instead of parentheses ()
Using braces {} which define blocks or structs
Using angle brackets <> which are invalid here
3. What is the output of the following Rust code?
let person = ("Alice", 30);
println!("{} is {} years old", person.0, person.1);
medium
A. Alice is 30 years old
B. ("Alice", 30) is {} years old
C. person.0 is person.1 years old
D. Compilation error due to tuple access
Solution
Step 1: Understand tuple indexing
Tuple elements are accessed by position using dot and index, e.g., person.0 is "Alice", person.1 is 30.
Step 2: Analyze the println! macro
The placeholders {} are replaced by person.0 and person.1 values, printing "Alice is 30 years old".
Final Answer:
Alice is 30 years old -> Option A
Quick Check:
Tuple access person.0 and person.1 works [OK]
Hint: Access tuple elements by .0, .1, etc. [OK]
Common Mistakes:
Trying to access tuple elements by name instead of position
Confusing tuple with array indexing syntax
Expecting tuple to print as a whole automatically
4. Identify the error in this Rust struct definition and fix it:
struct Point {
x: i32,
y i32,
}
medium
A. Trailing comma after last field is not allowed.
B. Struct fields cannot be integers.
C. Struct names must be lowercase.
D. Missing colon after y in struct field declaration.
Solution
Step 1: Check struct field syntax
Each field must have a name, colon, and type. The field 'y i32' is missing a colon.
Step 2: Validate other options
Fields can be integers (i32). Struct names are PascalCase by convention. Trailing commas are allowed in Rust structs.
Final Answer:
Missing colon after y in struct field declaration. -> Option D
Quick Check:
Field syntax requires colon between name and type [OK]
Hint: Struct fields need name: type syntax [OK]
Common Mistakes:
Omitting colon between field name and type
Thinking trailing commas are errors
Using lowercase struct names (allowed but not idiomatic)
5. Given the struct and tuple below, which code correctly creates a struct instance from the tuple values?
struct Color {
red: u8,
green: u8,
blue: u8,
}
let rgb = (255, 100, 50);
hard
A. let color = Color { red: rgb.0, green: rgb.1, blue: rgb.2 };
B. let color = Color(rgb.0, rgb.1, rgb.2);
C. let color = Color { 255, 100, 50 };
D. let color = (red: 255, green: 100, blue: 50);
Solution
Step 1: Understand struct initialization
Structs with named fields require field names and values in braces, e.g., Color { red: val, green: val, blue: val }.
Step 2: Use tuple values correctly
Tuple elements are accessed by position: rgb.0, rgb.1, rgb.2. let color = Color { red: rgb.0, green: rgb.1, blue: rgb.2 }; uses this correctly.
Step 3: Check incorrect options
let color = Color(rgb.0, rgb.1, rgb.2); uses tuple struct syntax, but Color is not a tuple struct. let color = Color { 255, 100, 50 }; omits field names. let color = (red: 255, green: 100, blue: 50); is invalid syntax.
Final Answer:
let color = Color { red: rgb.0, green: rgb.1, blue: rgb.2 }; -> Option A
Quick Check:
Struct init needs field names with values [OK]
Hint: Use field names when creating structs from tuples [OK]
Common Mistakes:
Trying to initialize struct without field names
Using tuple struct syntax for normal structs
Incorrect syntax for struct initialization
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.