Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use std::cell::Cell;
- struct DropCell<'a>(&'a Cell<i32>);
- impl<'a> DropCell<'a> {
- /// A method on the struct that lets us use it in a match expression
- fn get_matchable(&self) -> Option<()> {
- Some(())
- }
- }
- impl<'a> Drop for DropCell<'a> {
- fn drop(&mut self) {
- println!("{}", self.0.get())
- }
- }
- fn main() {
- let cell = Cell::new(0);
- // prints "0"
- let _ = DropCell(&cell).get_matchable();
- // prints "2"
- match DropCell(&cell).get_matchable() {
- Some(()) => {
- cell.set(2);
- },
- _ => (),
- }; // semicolon necessary to prevent borrow-check error
- }
Add Comment
Please, Sign In to add comment