[{"id":"rustadvanced-001","courseId":"rust-advanced","domain":"ownership-and-borrowing","topic":"Weak pointers break reference cycles","prompt":"A leaf node holds a Weak pointer to a branch node that was created in an inner scope. Put these events in the order they happen.","hint":"Ask at which point the strong count of the branch node reaches zero.","explain":"A Weak reference does not keep its target alive. The target is freed when its strong count reaches zero, and later upgrades of a Weak then fail. Follow the counts as the scope opens and closes.","source":"https://doc.rust-lang.org/book/ch15-06-reference-cycles.html","interaction":{"type":"sort","items":[{"text":"Inside the inner scope, branch is created and leaf's parent pointer is set with Rc::downgrade.","why":"The branch node must exist before a Weak pointer to it can be made, so this comes first."},{"text":"The inner scope ends, branch's strong count drops to 0, and its node is dropped.","why":"Once the scope closes, the only strong reference goes away, and leaf's Weak does not stop that drop."},{"text":"leaf calls upgrade on its parent pointer after the scope and gets None.","why":"With the strong count at zero, upgrade cannot produce an Rc, so it returns None."},{"text":"The program ends, and leaf's Rc has strong count 1 and weak count 0.","why":"Last of all, leaf is the only remaining reference, and nothing holds a Weak pointer to it."}]},"options":[],"answer":"o:abcd","explanation":"A Weak<T> does not own its target: once the strong count reaches zero the value is dropped, and upgrade returns None afterwards.","concept":"A Weak<T> does not own its target: once the strong count reaches zero the value is dropped, and upgrade returns None afterwards.","rev":1,"updatedAt":"2026-10-23","kind":"scenario","takeaways":["Weak pointers let parent and child point at each other without keeping each other alive."]},{"id":"rustadvanced-002","courseId":"rust-advanced","domain":"ownership-and-borrowing","topic":"Cell for Copy values","prompt":"On stable Rust, with no feature flags enabled, you can call get_cloned() on a Cell<Rc<i32>> to get a new Cell holding a clone of the Rc.","hint":"Check the stability note in the method's documentation.","explain":"Some standard library methods are still experimental. They compile only on nightly, behind a feature flag, and the documentation marks them with a stability note.","source":"https://doc.rust-lang.org/std/cell/struct.Cell.html","interaction":{"type":"true-false"},"options":[{"id":"a","text":"True","whyNot":"Stable Rust has get_cloned as a regular method, since Rc is a cheap type to clone."},{"id":"b","text":"False","why":"get_cloned is a nightly-only experimental API, so on stable Rust it is not available."}],"answer":"b","explanation":"Cell::get_cloned, which copies a non-Copy value out of a Cell, is a nightly-only experimental API behind the cell_get_cloned feature.","concept":"Cell::get_cloned, which copies a non-Copy value out of a Cell, is a nightly-only experimental API behind the cell_get_cloned feature.","rev":1,"updatedAt":"2026-10-23","kind":"scenario","takeaways":["Check a method's stability note before using it in stable code."]},{"id":"rustadvanced-003","courseId":"rust-advanced","domain":"ownership-and-borrowing","topic":"UnsafeCell as the interior mutability primitive","prompt":"Two threads each hold a raw pointer from .get() on the same UnsafeCell<i32>. One writes through its pointer while the other reads through its pointer, with no locks or atomics. What is the status of this code?","hint":"Think about what UnsafeCell changes and what it leaves alone.","explain":"A shared reference normally promises that the data behind it will not change. This wrapper relaxes that promise, but the rules for concurrent access still apply.","source":"https://doc.rust-lang.org/std/cell/struct.UnsafeCell.html","interaction":{"type":"single"},"options":[{"id":"a","text":"Undefined behavior: UnsafeCell allows mutation through a shared reference but does nothing to prevent data races.","why":"The cell only relaxes immutability for shared references; unsynchronized conflicting access is still undefined behavior."},{"id":"b","text":"Safe: UnsafeCell makes each access atomic, so the two threads cannot overlap.","whyNot":"UnsafeCell adds no synchronization; conflicting accesses must go through the atomic APIs."},{"id":"c","text":"Safe: data behind a shared reference never changes, so the write cannot race the read.","whyNot":"Mutation behind a shared reference is exactly what UnsafeCell permits, so this reasoning is backwards."},{"id":"d","text":"Safe: UnsafeCell turns every shared reference into a unique one while it is used.","whyNot":"UnsafeCell leaves the uniqueness rule for &mut unchanged; it does not turn a & into a &mut."}],"answer":"a","explanation":"UnsafeCell opts out of the immutability guarantee of &T, but it does nothing to avoid data races, which remain undefined behavior.","concept":"UnsafeCell opts out of the immutability guarantee of &T, but it does nothing to avoid data races, which remain undefined behavior.","rev":1,"updatedAt":"2026-10-23","kind":"scenario","takeaways":["Interior mutability does not license unsynchronized concurrent access."]},{"id":"rustadvanced-004","courseId":"rust-advanced","domain":"ownership-and-borrowing","topic":"Higher-ranked trait bounds","prompt":"A struct stores a closure F, and its call method passes F a reference whose lifetime is only chosen inside call. Which TWO bounds on F express this correctly?","hint":"Ask who picks the lifetime: the impl, or each call to the closure.","explain":"Some bounds must hold for every lifetime at once, not for one lifetime the code happens to pick. Rust writes such a bound with a special for<...> form that quantifies over lifetimes.","source":"https://doc.rust-lang.org/nomicon/hrtb.html","interaction":{"type":"multiple","tellCount":true},"options":[{"id":"a","text":"where for<'a> F: Fn(&'a (u8, u16)) -> &'a u8","why":"for<'a> means F must accept a reference of every lifetime, and the returned reference uses that same lifetime."},{"id":"b","text":"where F: for<'a> Fn(&'a (u8, u16)) -> &'a u8","why":"The for<'a> can sit on the Fn bound instead; this is the same bound written another way."},{"id":"c","text":"where F: Fn(&'a (u8, u16)) -> &'a u8","whyNot":"'a is never declared, and a single named lifetime would be fixed before call runs, which is too early."},{"id":"d","text":"where for<'a> F: Fn(&'a (u8, u16)) -> &'static u8","whyNot":"The returned reference must borrow from the input's lifetime, not be 'static."},{"id":"e","text":"where F: 'static + Fn(&'static (u8, u16)) -> &'static u8","whyNot":"This forces every argument to be 'static, but call only borrows self for a shorter time."},{"id":"f","text":"where for<'a> F: Fn(&'a (u8, u16)) -> u8","whyNot":"It returns a copied u8, so the output no longer borrows from the input, which the code does."}],"answer":"ab","explanation":"for<'a> F: Fn(&'a T) requires F to work for every lifetime 'a; the elided form Fn(&T) -> &T is sugar for that higher-ranked bound.","concept":"for<'a> F: Fn(&'a T) requires F to work for every lifetime 'a; the elided form Fn(&T) -> &T is sugar for that higher-ranked bound.","rev":1,"updatedAt":"2026-10-23","kind":"scenario","takeaways":["Fn bounds on reference arguments are higher-ranked: they must hold for every lifetime."]},{"id":"rustadvanced-005","courseId":"rust-advanced","domain":"ownership-and-borrowing","topic":"Variance and invariance of &mut T","prompt":"A generic function is declared as fn put<T>(slot: &mut T, value: T). A caller passes slot as &mut &'static str, and value as a shorter-lived &'a str. Why does the compiler reject this call?","hint":"Look at what the &mut wrapper does to the type inside it.","explain":"Subtyping lets a longer lifetime stand in for a shorter one, but only at some type positions. Whether it applies depends on how the surrounding type uses its parameter.","source":"https://doc.rust-lang.org/nomicon/subtyping.html","interaction":{"type":"single"},"options":[{"id":"a","text":"&mut T is invariant over T, so T must be exactly &'static str, and the shorter lifetime cannot stand in for it.","why":"Invariance forbids subtyping inside a &mut, so the value's shorter lifetime cannot be used for T."},{"id":"b","text":"&mut references are covariant over their target, so 'static can shrink to 'a inside the slot.","whyNot":"Covariance would allow that, but &mut T is invariant over T, so the target cannot be shrunk."},{"id":"c","text":"Lifetimes are never subtyped, so the two lifetimes must always be identical.","whyNot":"Lifetimes do subtype, since 'static outlives every shorter lifetime; the problem is the &mut around it."},{"id":"d","text":"Generic arguments are contravariant, so the caller must lengthen the shorter lifetime instead.","whyNot":"Contravariance applies to function argument types, not to a generic T used this way, so it is not the cause."}],"answer":"a","explanation":"&mut T is invariant over T, so a &mut T cannot be subtyped in T; &'a T, by contrast, is covariant over its lifetime.","concept":"&mut T is invariant over T, so a &mut T cannot be subtyped in T; &'a T, by contrast, is covariant over its lifetime.","rev":1,"updatedAt":"2026-10-23","kind":"scenario","takeaways":["A mutable reference makes the type inside it invariant, which blocks lifetime shortening."]}]