0
0
Rustprogramming~5 mins

Compound data types in Rust - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What are compound data types in Rust?
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>
How do you access the third element of a tuple named t?
At.2
Bt[2]
Ct(2)
Dt.3
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.
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];
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.
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.