April 2026
Intermediate
631 pages
16h 20m
English
This section provides the code solutions for the practice exercises in Section 4.7. The code is largely self-explanatory; however, we have included comments and additional explanations wherever necessary to enhance understanding.
Fix the compilation error
fn main() { let s1: String = String::from("this is me, "); let s2: &str = "Nouman"; some_function(&s1, s2); println!("{} {}", s1, s2);}fn some_function(a1: &String, a2: &str) { // update the first input to a // reference String println!("{} {}", a1, a2);}
Ownership in a loop
fn main() { let mut my_vec = vec![1, 2, 3, 4, 5]; let mut temp; while !my_vec.is_empty() { temp = my_vec.clone(); /* during the first iteration, the transfer of ownership occurs from my_vec to temp, ...
Read now
Unlock full access