Guest User

Untitled

a guest
Feb 18th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.63 KB | None | 0 0
  1. use std::cell::Cell;
  2.  
  3. struct DropCell<'a>(&'a Cell<i32>);
  4.  
  5. impl<'a> DropCell<'a> {
  6. /// A method on the struct that lets us use it in a match expression
  7. fn get_matchable(&self) -> Option<()> {
  8. Some(())
  9. }
  10. }
  11.  
  12. impl<'a> Drop for DropCell<'a> {
  13. fn drop(&mut self) {
  14. println!("{}", self.0.get())
  15. }
  16. }
  17.  
  18. fn main() {
  19. let cell = Cell::new(0);
  20.  
  21. // prints "0"
  22. let _ = DropCell(&cell).get_matchable();
  23.  
  24. // prints "2"
  25. match DropCell(&cell).get_matchable() {
  26. Some(()) => {
  27. cell.set(2);
  28. },
  29. _ => (),
  30. }; // semicolon necessary to prevent borrow-check error
  31. }
Add Comment
Please, Sign In to add comment