7 ms·
The crucial bit for Vec::drain is in these two lines of code, which the article lists but does not discuss: // set self.vec length's to start, to be sa
by ynik 2y ago
The crucial bit for Vec::drain is in these two lines of code, which the article lists but does not discuss:
// set self.vec length's to start, to be safe in case Drain is leaked
self.set_len(start);
You cannot rely on Drop alone for safety, you need a fallback strategy in case of a leak (mem::forget or Rc cycle).
Rust lifetimes only ensure that there is no use after free; there is no guarantee that Drop is called before the end of the lifetime. It's possible to mutably access the original Vec after leaking a Drain.
- nextaccountic 2y agoYeah that's the "pre" in "pre-pooping your pants"
- chriskrycho 2y agoThanks to the feedback in this discussion, the article now does discuss 5at line; much obliged to all the folks who helped me fix a substantial error.