0
0
Rustprogramming~20 mins

Compound data types in Rust - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
๐ŸŽ–๏ธ
Rust Compound Data Types Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ“ Predict Output
intermediate
2:00remaining
What is the output of this Rust tuple code?

Consider the following Rust code using tuples. What will it print?

Rust
fn main() {
    let tup: (i32, f64, u8) = (500, 6.4, 1);
    let (x, y, z) = tup;
    println!("{} {} {}", x, y, z);
}
A6.4 500 1
B500 6.4 1
C500 1 6.4
DError: cannot print tuple elements directly
Attempts:
2 left
๐Ÿ’ก Hint

Remember tuple elements keep their order and types.

โ“ Predict Output
intermediate
2:00remaining
What is the output of this Rust array code?

What will this Rust program print?

Rust
fn main() {
    let arr = [10, 20, 30, 40, 50];
    let slice = &arr[1..4];
    println!("{:?}", slice);
}
A[20, 30, 40]
B[10, 20, 30, 40]
C[30, 40, 50]
DError: cannot slice arrays this way
Attempts:
2 left
๐Ÿ’ก Hint

Remember Rust slices include the start index but exclude the end index.

๐Ÿ”ง Debug
advanced
2:00remaining
Why does this Rust struct code fail to compile?

Look at this Rust code defining and using a struct. Why does it fail to compile?

Rust
struct Point {
    x: i32,
    y: i32,
}

fn main() {
    let p = Point { x: 5 };
    println!("({}, {})", p.x, p.y);
}
AError because Point struct must derive Debug trait
BError because struct fields must be mutable
CError because println! macro syntax is wrong
DError because field 'y' is missing when creating Point
Attempts:
2 left
๐Ÿ’ก Hint

Check if all struct fields are initialized when creating an instance.

โ“ Predict Output
advanced
2:00remaining
What is the output of this Rust enum and match code?

What will this Rust program print?

Rust
enum Message {
    Quit,
    Move { x: i32, y: i32 },
    Write(String),
}

fn main() {
    let msg = Message::Move { x: 10, y: 20 };
    match msg {
        Message::Quit => println!("Quit message"),
        Message::Move { x, y } => println!("Move to ({}, {})", x, y),
        Message::Write(text) => println!("Write message: {}", text),
    }
}
AMove to (10, 20)
BQuit message
CWrite message: Move to (10, 20)
DError: match arms missing wildcard pattern
Attempts:
2 left
๐Ÿ’ก Hint

Look at which enum variant is created and matched.

๐Ÿง  Conceptual
expert
2:00remaining
How many elements does this nested tuple contain?

Consider this nested tuple in Rust:

let nested = ((1, 2), (3, (4, 5)));

How many individual integer elements does nested contain?

A4
B3
C5
D6
Attempts:
2 left
๐Ÿ’ก Hint

Count all integers inside all inner tuples.