After Chs 5 and 6 (see the reading club post here), we get a capstone quiz that covers ownership along with struts
and enums
.
So, lets do the quiz together! If you’ve done it already, revisiting might still be very instructive! I certainly thought these questions were useful “revision”.
I’ll post a comment for each question with the answer, along with my own personal notes (and quotes from The Book if helpful), behind spoiler tags.
Feel free to try to answer in a comment before checking (if you dare). But the main point is to understand the point the question is making, so share any confusions/difficulties too, and of course any corrections of my comments/notes!.
Q6: Fixing
Of the following fixes (highlighted in yellow), which fix best satisfies these three criteria:
/// Gets the string out of an option if it exists, /// returning a default otherwise fn get_or_default(arg: &Option<String>) -> String { if arg.is_none() { return String::new(); } let s = arg.unwrap(); s.clone() }
1:
fn get_or_default(arg: &Option<&str>) -> String { if arg.is_none() { return String::new(); } let s = arg.unwrap(); s.to_string() }
2:
fn get_or_default(arg: &mut Option<String>) -> String { if arg.is_none() { return String::new(); } let s = arg.as_mut().unwrap(); s.clone() }
3:
fn get_or_default(arg: Option<String>) -> String { if arg.is_none() { return String::new(); } let s = arg.unwrap(); s.clone() }
4:
fn get_or_default(arg: &Option<String>) -> String { match arg { None => String::new(), Some(s) => s.clone() } }
Answer
4
4
is a better, more idiomatic version of3
, especially because it requires ownership ofarg
which is restrictive and may not even be available3
does fix the problem1
doesn’t fix the problem2
… I’m not sure about … but I don’t think having a mutables
helps with the problem either (?)