rust - How to delay destruction of unnamed objects? -
i using tempdir
struct create , remove folders on disk. tempdir
not referenced in code apart construction.
since compiler warns unused objects, tried (un)naming tempdir-struct _
results in struct being destroyed.
is there nice solution this?
see example code, compare one
two
:
pub struct res; impl drop res { fn drop(&mut self) { println!("dropping self!"); } } fn one() { println!("one"); let r = res; // <--- dropping @ end of function // compiler warns unused object println!("before exit"); } fn two() { println!("two"); let _ = res; // <--- dropping println!("before exit"); } fn main() { one(); two(); }
giving variable name prefixing name underscore delays destruction until end of scope not trigger unused variable warning.
struct noisy; impl drop noisy { fn drop(&mut self) { println!("dropping"); } } fn main() { { let _ = noisy; println!("before end of first scope"); } { let _noisy = noisy; println!("before end of second scope"); } }
dropping before end of first scope before end of second scope dropping
Comments
Post a Comment