Challenge - 5 Problems
Rust Ownership Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat is the output of this Rust code involving ownership and borrowing?
Consider the following Rust code. What will it print when run?
Rust
fn main() {
let s = String::from("hello");
let len = calculate_length(&s);
println!("The length of '{}' is {}.", s, len);
}
fn calculate_length(s: &String) -> usize {
s.len()
}Attempts:
2 left
💡 Hint
Look at how the string is passed to the function: is it moved or borrowed?
✗ Incorrect
The function calculate_length borrows the string with &String, so ownership is not moved. The original string s remains valid and can be printed after the function call.
❓ Predict Output
intermediateWhat happens when ownership is moved into a function?
What will be the output or error of this Rust code?
Rust
fn main() {
let s = String::from("world");
takes_ownership(s);
println!("After function call: {}", s);
}
fn takes_ownership(some_string: String) {
println!("Inside function: {}", some_string);
}Attempts:
2 left
💡 Hint
Check what happens to ownership when passing s to takes_ownership.
✗ Incorrect
The function takes_ownership takes ownership of s, so s is moved and no longer valid in main. Trying to use s after the call causes a compile error.
🔧 Debug
advancedIdentify the error in this Rust code with mutable references
This code tries to modify a string through a mutable reference. What error will the compiler report?
Rust
fn main() {
let mut s = String::from("hello");
let r1 = &mut s;
let r2 = &mut s;
r1.push_str(", world");
r2.push_str("!");
println!("{}", s);
}Attempts:
2 left
💡 Hint
Rust allows only one mutable reference at a time.
✗ Incorrect
The code tries to create two mutable references to s at the same time, which Rust forbids to prevent data races. This causes a compile-time error.
❓ Predict Output
advancedWhat is the output of this Rust code with ownership transfer and return?
What will this Rust program print?
Rust
fn main() {
let s1 = String::from("hello");
let s2 = takes_and_gives_back(s1);
println!("s2 is: {}", s2);
}
fn takes_and_gives_back(a_string: String) -> String {
a_string + " world"
}Attempts:
2 left
💡 Hint
The function takes ownership and returns a new String.
✗ Incorrect
The function takes ownership of s1, appends " world" and returns the new String. s2 receives ownership and can be printed.
❓ Predict Output
expertWhat is the output of this Rust code demonstrating ownership and borrowing with slices?
Analyze the code and select the correct output.
Rust
fn main() {
let s = String::from("hello world");
let word = first_word(&s);
println!("First word: {}", word);
}
fn first_word(s: &String) -> &str {
let bytes = s.as_bytes();
for (i, &item) in bytes.iter().enumerate() {
if item == b' ' {
return &s[0..i];
}
}
&s[..]
}Attempts:
2 left
💡 Hint
The function returns a slice up to the first space character.
✗ Incorrect
The function finds the first space and returns a slice of the string from start to that index. This slice is "hello".
