Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #![feature(maybe_uninit_ref)]
- use std::mem::MaybeUninit;
- struct MyRef<'a> {
- r: &'a mut i32
- }
- impl<'a> Drop for MyRef<'a> {
- fn drop(&mut self) {
- panic!("We shouldn't be dropping!");
- }
- }
- impl<'a> MyRef<'a> {
- fn destruct(self) -> &'a mut i32 {
- let mut maybe_uninit = MaybeUninit::new(self);
- unsafe {
- std::ptr::read(&maybe_uninit.get_mut().r)
- }
- }
- }
- fn main() {
- let mut x = 3;
- let my_ref = MyRef {
- r: &mut x
- };
- let x_ref = my_ref.destruct();
- println!("{}", *x_ref);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement