0
0
Rustprogramming~20 mins

Why lifetimes exist in Rust - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Rust Lifetimes 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 code involving references?

Consider this Rust code snippet. What will it print when run?

Rust
fn main() {
    let r;
    {
        let x = 5;
        r = &x;
    }
    println!("{}", r);
}
A5
B0
CCompilation error due to borrowed value does not live long enough
DRuntime panic due to null reference
Attempts:
2 left
💡 Hint

Think about the scope of x and the reference r.

🧠 Conceptual
intermediate
1:30remaining
Why does Rust require explicit lifetimes in function signatures?

Why does Rust sometimes require you to write explicit lifetime annotations in function signatures?

ATo allow functions to mutate immutable references
BTo improve runtime performance by managing memory manually
CTo enable multi-threading support automatically
DTo tell the compiler how long references passed to and returned from the function are valid
Attempts:
2 left
💡 Hint

Think about what lifetimes describe in Rust.

🔧 Debug
advanced
2:30remaining
Identify the lifetime error in this Rust code

What lifetime-related error will this Rust code produce?

Rust
fn longest(x: &str, y: &str) -> &str {
    if x.len() > y.len() {
        x
    } else {
        y
    }
}

fn main() {
    let string1 = String::from("long string");
    let result;
    {
        let string2 = String::from("short");
        result = longest(string1.as_str(), string2.as_str());
    }
    println!("{}", result);
}
ACompilation error: missing lifetime specifier in function signature
BRuntime error: dangling reference
CCompilation error: cannot borrow immutable variable as mutable
DNo error, prints 'long string'
Attempts:
2 left
💡 Hint

Look at the function longest and its return type.

📝 Syntax
advanced
2:00remaining
Which function signature correctly uses lifetimes?

Which of the following Rust function signatures correctly uses lifetime annotations for a function that returns a reference to one of its string slice parameters?

Afn first<'a>(x: &'a str, y: &str) -> &'a str
Bfn first<'a, 'b>(x: &'a str, y: &'b str) -> &'a str
Cfn first<'a>(x: &str, y: &'a str) -> &'a str
Dfn first(x: &str, y: &str) -> &str
Attempts:
2 left
💡 Hint

Consider which parameters the returned reference can validly point to.

🚀 Application
expert
3:00remaining
What is the output of this Rust code demonstrating lifetime usage?

What will this Rust program print?

Rust
fn main() {
    let string1 = String::from("abcd");
    let string2 = "xyz";

    let result = longest(string1.as_str(), string2);
    println!("{}", result);
}

fn longest<'a>(x: &'a str, y: &'a str) -> &'a str {
    if x.len() > y.len() {
        x
    } else {
        y
    }
}
Aabcd
Bxyz
CCompilation error due to lifetime mismatch
DRuntime panic due to invalid reference
Attempts:
2 left
💡 Hint

Check which string slice is longer and the lifetimes involved.