fn consume<T>(_: T) {}
pub fn weird_func(buf: &mut [u8]) {
let mut x = Foo::new(buf);
consume(x);
x = Foo::new(buf);
&x; // even adding a simple `&x` will re-trigger the error.
}
that’s fascinating. That issue shows an almost identical example that doesn’t compile. And indeed, this difference seems to be relevant in order to make it compile:
fn main() {
let mut s = "hi".to_string();
let r = &mut s;
fn f(r: &mut String) {
let mut v = Dropper(r);
drop(v);
- v = Dropper(r);
+ v = Dropper::new(r);
}
}
struct Dropper<'a>(&'a mut String);
impl Drop for Dropper<'_> {
fn drop(&mut self) {
self.0.push_str("dropped!");
}
}
impl<'a> Dropper<'a> {
fn new(s: &'a mut String) -> Self {
Dropper(s)
}
}