[{"id":"rustintermediate-001","courseId":"rust-intermediate","domain":"ownership-and-borrowing","topic":"Lifetime annotations on functions","prompt":"A teammate writes this function and cargo refuses to compile it, reporting a missing lifetime specifier. Why does the compiler stop here?","hint":"Focus on what the compiler needs to know about the reference the function returns.","explain":"A function that returns a reference must let the compiler link that reference's lifetime to its inputs. Lifetime parameters are names that express those links; they don't change how long any value lives.","source":"https://doc.rust-lang.org/book/ch10-03-lifetime-syntax.html","interaction":{"type":"single"},"options":[{"id":"a","text":"Rust can't tell whether the returned reference borrows from x or from y.","why":"The return type needs a lifetime that shows its source, and the body returns either parameter, so the compiler can't tell which one."},{"id":"b","text":"Rust can't measure the strings' lengths until the program runs.","whyNot":"The length comparison is ordinary runtime code; the error is about the lifetime of the returned reference."},{"id":"c","text":"A function can never return a &str without 'static on the return type.","whyNot":"'static isn't needed here; a named lifetime shared by the inputs and the output fixes the error."},{"id":"d","text":"Every reference parameter must carry a lifetime, even one that is never returned.","whyNot":"A parameter with no relationship to the return value doesn't need a named lifetime."}],"answer":"a","explanation":"A returned reference needs a lifetime tied to the inputs it may borrow from; when the compiler can't tell which input that is, it asks for an explicit lifetime parameter.","concept":"A returned reference needs a lifetime tied to the inputs it may borrow from; when the compiler can't tell which input that is, it asks for an explicit lifetime parameter.","rev":1,"updatedAt":"2026-10-23","kind":"scenario","code":"fn longest(x: &str, y: &str) -> &str {\n    if x.len() > y.len() { x } else { y }\n}","takeaways":["Name a lifetime when a returned reference could come from more than one input."]},{"id":"rustintermediate-002","courseId":"rust-intermediate","domain":"ownership-and-borrowing","topic":"Lifetimes in structs holding references","prompt":"A struct holds a borrowed string slice, as declared here. What does the 'a annotation guarantee about an instance of this struct?","hint":"Think about what the struct stores: a reference to text owned somewhere else.","explain":"A struct that stores a reference must declare a lifetime parameter. The compiler checks each instance against the data its reference fields borrow.","source":"https://doc.rust-lang.org/book/ch10-03-lifetime-syntax.html","interaction":{"type":"single"},"options":[{"id":"a","text":"An instance can't outlive the reference it holds in its part field.","why":"The lifetime parameter ties each instance to the reference it holds, so the instance cannot live longer than that reference."},{"id":"b","text":"The annotation is only a hint to the optimizer and restricts nothing.","whyNot":"Lifetime annotations are checked by the compiler, so they do restrict which code is accepted."},{"id":"c","text":"An instance may outlive the text, since the struct holds only a pointer.","whyNot":"Holding a pointer is exactly why the lifetime matters; the struct can't outlive what it points to."},{"id":"d","text":"Only structs with two or more reference fields need a lifetime parameter.","whyNot":"A struct with one reference field still needs one, as ImportantExcerpt shows."}],"answer":"a","explanation":"A struct holding references needs a lifetime parameter, and each instance cannot outlive the data its reference fields point to.","concept":"A struct holding references needs a lifetime parameter, and each instance cannot outlive the data its reference fields point to.","rev":1,"updatedAt":"2026-10-23","kind":"scenario","code":"struct ImportantExcerpt<'a> {\n    part: &'a str,\n}","takeaways":["Any struct with a reference field needs a lifetime parameter on that field."]},{"id":"rustintermediate-003","courseId":"rust-intermediate","domain":"ownership-and-borrowing","topic":"The 'static lifetime","prompt":"Every string literal in a Rust program has the 'static lifetime, so it stays valid for the whole run of the program.","hint":"Think about where the text of a literal is stored, not the scope it is written in.","explain":"'static is a special lifetime name for references that stay valid for the entire program. Before writing it, ask whether the data really lives that long.","source":"https://doc.rust-lang.org/book/ch10-03-lifetime-syntax.html","interaction":{"type":"true-false"},"options":[{"id":"a","text":"True","why":"The text is stored directly in the program's binary, which is always available, so the lifetime of every literal is 'static."},{"id":"b","text":"False","whyNot":"Literals are not limited to a function's scope; their text lives in the binary for the whole run."}],"answer":"a","explanation":"String literals have the 'static lifetime because their text is stored directly in the program's binary.","concept":"String literals have the 'static lifetime because their text is stored directly in the program's binary.","rev":1,"updatedAt":"2026-10-23","kind":"scenario","takeaways":["Use 'static only when the data really lives for the whole program."]},{"id":"rustintermediate-004","courseId":"rust-intermediate","domain":"ownership-and-borrowing","topic":"Lifetime elision rules","prompt":"Which of these function signatures compiles without any explicit lifetime annotations?","hint":"Count the lifetimes the inputs use, then ask whether the output can be given one.","explain":"Lifetime elision is a fixed set of rules that fills in omitted lifetimes. Each elided input gets its own lifetime; an output lifetime is filled in only from one parameter lifetime or from a &self receiver.","source":"https://doc.rust-lang.org/reference/lifetime-elision.html","interaction":{"type":"single"},"options":[{"id":"a","text":"fn substr(s: &str, until: usize) -> &str","why":"Only one input carries a lifetime, so the elision rules assign it to the output."},{"id":"b","text":"fn frob(s: &str, t: &str) -> &str","whyNot":"Two elided input lifetimes make the output ambiguous, so the compiler needs names."},{"id":"c","text":"fn get_str() -> &str","whyNot":"With no parameters there is nothing to borrow from, so the output lifetime can't be inferred."},{"id":"d","text":"fn first_word(words: &[&str]) -> &str","whyNot":"The outer slice reference and the inner &str each add an elided lifetime, so the output is ambiguous."}],"answer":"a","explanation":"Elision fills in an output lifetime only when the parameters use exactly one lifetime, or when a &self or &mut self receiver supplies it.","concept":"Elision fills in an output lifetime only when the parameters use exactly one lifetime, or when a &self or &mut self receiver supplies it.","rev":1,"updatedAt":"2026-10-23","kind":"scenario","takeaways":["Expect to write lifetime names when a function takes two reference inputs and returns one."]},{"id":"rustintermediate-005","courseId":"rust-intermediate","domain":"ownership-and-borrowing","topic":"Box for recursive types","prompt":"A teammate writes this enum and cargo rejects it. What is the underlying reason?","hint":"Think about whether the compiler can work out how much space the type needs.","explain":"The compiler must know the size of every type when it builds the program. A box has a fixed size whatever it points to, which matters when a type contains itself.","source":"https://doc.rust-lang.org/book/ch15-01-box.html","interaction":{"type":"single"},"options":[{"id":"a","text":"The type would contain itself with no fixed size, so the compiler can't compute its size.","why":"Rust needs each type's size at compile time, and a List nested directly inside a List never ends."},{"id":"b","text":"Enums can't store an i32 next to another value in one variant.","whyNot":"Enum variants can hold several fields; the problem is the size of the recursive field."},{"id":"c","text":"The Nil variant has no data, so the enum has no size at all.","whyNot":"Nil takes no space beyond the tag; the failure comes from the recursive field."},{"id":"d","text":"Recursive types are only allowed in structs, not in enums.","whyNot":"Enums can be recursive when the recursion goes through a box or another fixed-size pointer."}],"answer":"a","explanation":"A type's size must be known at compile time, so a recursive type needs an indirection such as Box<T>, which has a known size.","concept":"A type's size must be known at compile time, so a recursive type needs an indirection such as Box<T>, which has a known size.","rev":1,"updatedAt":"2026-10-23","kind":"scenario","code":"enum List {\n    Cons(i32, List),\n    Nil,\n}","takeaways":["Wrap the recursive field in Box<T> to give the type a known size."]}]