0
0
Rustprogramming~20 mins

Why Rust is used - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
๐ŸŽ–๏ธ
Rust Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
๐Ÿง  Conceptual
intermediate
2:00remaining
Why does Rust emphasize memory safety?

Rust is known for its focus on memory safety without a garbage collector. Why is this important?

AIt prevents bugs like null pointer dereferencing and data races at compile time.
BIt requires manual memory management like C and C++.
CIt allows programs to run slower but with more features.
DIt uses garbage collection to manage memory automatically.
Attempts:
2 left
๐Ÿ’ก Hint

Think about common bugs in languages like C and how Rust tries to avoid them.

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

Consider this Rust code snippet:

fn main() {
    let s1 = String::from("hello");
    let s2 = s1;
    println!("{}", s1);
}

What happens when you run this code?

AIt prints 'hello' successfully.
BIt causes a runtime error due to null pointer.
CIt causes a compile-time error because s1 is moved to s2.
DIt prints an empty string.
Attempts:
2 left
๐Ÿ’ก Hint

Think about what happens when ownership is transferred in Rust.

โ“ Predict Output
advanced
2:00remaining
What is the output of this Rust code using borrowing?

Look at this Rust code:

fn main() {
    let s = String::from("hello");
    let len = calculate_length(&s);
    println!("Length: {}", len);
}

fn calculate_length(s: &String) -> usize {
    s.len()
}

What will this program print?

ARuntime error due to null reference
BLength: 0
CCompile-time error due to borrowing rules violation
DLength: 5
Attempts:
2 left
๐Ÿ’ก Hint

Borrowing allows reading data without taking ownership.

๐Ÿง  Conceptual
advanced
2:00remaining
Why is Rust popular for system programming?

Rust is gaining popularity for system-level programming. What is the main reason?

AIt does not support concurrency to avoid complexity.
BIt combines low-level control with memory safety and concurrency without a garbage collector.
CIt is an interpreted language like Python.
DIt uses a garbage collector to simplify memory management.
Attempts:
2 left
๐Ÿ’ก Hint

Think about what system programming needs and how Rust addresses those needs.

โ“ Predict Output
expert
3:00remaining
What is the output of this Rust code using pattern matching and enums?

Consider this Rust code:

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

fn process_message(msg: Message) {
    match msg {
        Message::Quit => println!("Quit message"),
        Message::Move { x, y } => println!("Move to ({}, {})", x, y),
        Message::Write(text) => println!("Text message: {}", text),
        Message::ChangeColor(r, g, b) => println!("Change color to RGB({}, {}, {})", r, g, b),
    }
}

fn main() {
    let msg = Message::Move { x: 10, y: 20 };
    process_message(msg);
}

What will this program print?

AMove to (10, 20)
BQuit message
CText message: Move to (10, 20)
DChange color to RGB(10, 20, 0)
Attempts:
2 left
๐Ÿ’ก Hint

Look at how the enum variant and pattern matching work together.