Going through rustlings for the first time

ex(16): lifetimes

Changed files
+84 -13
exercises
solutions
+6 -2
.rustlings-state.txt
··· 1 1 DON'T EDIT THIS FILE! 2 2 3 - quiz3 3 + tests1 4 4 5 5 intro1 6 6 intro2 ··· 64 64 traits2 65 65 traits3 66 66 traits4 67 - traits5 67 + traits5 68 + quiz3 69 + lifetimes1 70 + lifetimes2 71 + lifetimes3
+1 -1
exercises/16_lifetimes/lifetimes1.rs
··· 4 4 // not own their own data. What if their owner goes out of scope? 5 5 6 6 // TODO: Fix the compiler error by updating the function signature. 7 - fn longest(x: &str, y: &str) -> &str { 7 + fn longest<'a>(x: &'a str, y: &'a str) -> &'a str { 8 8 if x.len() > y.len() { 9 9 x 10 10 } else {
+1 -1
exercises/16_lifetimes/lifetimes2.rs
··· 11 11 // TODO: Fix the compiler error by moving one line. 12 12 13 13 let string1 = String::from("long string is long"); 14 + let string2 = String::from("xyz"); 14 15 let result; 15 16 { 16 - let string2 = String::from("xyz"); 17 17 result = longest(&string1, &string2); 18 18 } 19 19 println!("The longest string is '{result}'");
+3 -3
exercises/16_lifetimes/lifetimes3.rs
··· 1 1 // Lifetimes are also needed when structs hold references. 2 2 3 3 // TODO: Fix the compiler errors about the struct. 4 - struct Book { 5 - author: &str, 6 - title: &str, 4 + struct Book<'a> { 5 + author: &'a str, 6 + title: &'a str, 7 7 } 8 8 9 9 fn main() {
+26 -2
solutions/16_lifetimes/lifetimes1.rs
··· 1 + // The Rust compiler needs to know how to check whether supplied references are 2 + // valid, so that it can let the programmer know if a reference is at risk of 3 + // going out of scope before it is used. Remember, references are borrows and do 4 + // not own their own data. What if their owner goes out of scope? 5 + 6 + fn longest<'a>(x: &'a str, y: &'a str) -> &'a str { 7 + // ^^^^ ^^ ^^ ^^ 8 + if x.len() > y.len() { 9 + x 10 + } else { 11 + y 12 + } 13 + } 14 + 1 15 fn main() { 2 - // DON'T EDIT THIS SOLUTION FILE! 3 - // It will be automatically filled after you finish the exercise. 16 + // You can optionally experiment here. 17 + } 18 + 19 + #[cfg(test)] 20 + mod tests { 21 + use super::*; 22 + 23 + #[test] 24 + fn test_longest() { 25 + assert_eq!(longest("abcd", "123"), "abcd"); 26 + assert_eq!(longest("abc", "1234"), "1234"); 27 + } 4 28 }
+31 -2
solutions/16_lifetimes/lifetimes2.rs
··· 1 + fn longest<'a>(x: &'a str, y: &'a str) -> &'a str { 2 + if x.len() > y.len() { 3 + x 4 + } else { 5 + y 6 + } 7 + } 8 + 1 9 fn main() { 2 - // DON'T EDIT THIS SOLUTION FILE! 3 - // It will be automatically filled after you finish the exercise. 10 + let string1 = String::from("long string is long"); 11 + // Solution1: You can move `strings2` out of the inner block so that it is 12 + // not dropped before the print statement. 13 + let string2 = String::from("xyz"); 14 + let result; 15 + { 16 + result = longest(&string1, &string2); 17 + } 18 + println!("The longest string is '{result}'"); 19 + // `string2` dropped at the end of the function. 20 + 21 + // ========================================================================= 22 + 23 + let string1 = String::from("long string is long"); 24 + let result; 25 + { 26 + let string2 = String::from("xyz"); 27 + result = longest(&string1, &string2); 28 + // Solution2: You can move the print statement into the inner block so 29 + // that it is executed before `string2` is dropped. 30 + println!("The longest string is '{result}'"); 31 + // `string2` dropped here (end of the inner scope). 32 + } 4 33 }
+16 -2
solutions/16_lifetimes/lifetimes3.rs
··· 1 + // Lifetimes are also needed when structs hold references. 2 + 3 + struct Book<'a> { 4 + // ^^^^ added a lifetime annotation 5 + author: &'a str, 6 + // ^^ 7 + title: &'a str, 8 + // ^^ 9 + } 10 + 1 11 fn main() { 2 - // DON'T EDIT THIS SOLUTION FILE! 3 - // It will be automatically filled after you finish the exercise. 12 + let book = Book { 13 + author: "George Orwell", 14 + title: "1984", 15 + }; 16 + 17 + println!("{} by {}", book.title, book.author); 4 18 }