0
0
Rustprogramming~20 mins

Variable lifetime basics in Rust - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
๐ŸŽ–๏ธ
Rust Lifetime 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 with lifetimes?

Consider the following Rust code that uses references with explicit lifetimes. What will it print?

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

fn main() {
    let string1 = String::from("apple");
    let string2 = "banana";
    let result = longest(string1.as_str(), string2);
    println!("{}", result);
}
Abanana
Bapple
CCompilation error due to lifetime mismatch
Dapplebanana
Attempts:
2 left
๐Ÿ’ก Hint

Think about which string slice is longer and how lifetimes ensure safety.

โ“ Predict Output
intermediate
2:00remaining
What error does this Rust code produce?

Look at this Rust code snippet. What error will the compiler show?

Rust
fn main() {
    let r;
    {
        let x = 5;
        r = &x;
    }
    println!("{}", r);
}
ACompilation error: cannot assign twice to immutable variable
BPrints 5
CCompilation error: borrowed value does not live long enough
DRuntime error: use after free
Attempts:
2 left
๐Ÿ’ก Hint

Check the scope of x and the reference r.

๐Ÿ”ง Debug
advanced
3:00remaining
Why does this Rust code fail to compile due to lifetimes?

Identify the reason this Rust code does not compile and what lifetime rule it violates.

Rust
fn get_str<'a>() -> &'a str {
    let s = String::from("hello");
    &s
}

fn main() {
    let result = get_str();
    println!("{}", result);
}
AReturns reference to a local variable that is dropped, causing dangling reference
BMissing lifetime annotation on function parameter
CCannot return a reference without specifying 'static lifetime
DFunction should return String, not &str
Attempts:
2 left
๐Ÿ’ก Hint

Think about the lifetime of s inside the function.

๐Ÿ“ Syntax
advanced
2:00remaining
Which option correctly declares a function with lifetime parameters?

Choose the correct Rust function signature that returns the longer of two string slices with proper lifetime annotations.

Afn longest(x: &str, y: &str) -> &str
Bfn longest<'a>(x: &'a str, y: &'a str) -> &'a str
Cfn longest<'a, 'b>(x: &'a str, y: &'b str) -> &'a str
Dfn longest(x: &'a str, y: &'a str) -> &'a str
Attempts:
2 left
๐Ÿ’ก Hint

Remember to declare lifetime parameters before using them in references.

๐Ÿš€ Application
expert
3:00remaining
How many items does this Rust code store in the vector?

Given this Rust code using references with lifetimes, how many items will be stored in the vector vec after execution?

Rust
fn main() {
    let mut vec = Vec::new();
    {
        let s = String::from("hello");
        vec.push(&s);
    }
    println!("{}", vec.len());
}
A1
B0
CRuntime panic due to invalid reference
DCompilation error due to lifetime mismatch
Attempts:
2 left
๐Ÿ’ก Hint

Consider the lifetime of s and the reference stored in the vector.