Guest User

Untitled

a guest
Jun 23rd, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.64 KB | None | 0 0
  1. #[derive(Copy, Clone, Debug)]
  2. struct Thing {
  3. pub value: u16,
  4. }
  5.  
  6. impl Thing {
  7. pub fn new(value: u16) -> Thing {
  8. Thing {
  9. value: value,
  10. }
  11. }
  12. }
  13.  
  14. fn mutate_thing(t: &mut Thing) -> &Thing {
  15. t.value = 0xb33f;
  16. return t;
  17. }
  18.  
  19. fn main() {
  20. let mut thing = Thing::new(0xcafe);
  21.  
  22. // Create a new scope which we will borrow `thing` into
  23. {
  24. let thing2 = mutate_thing(&mut thing);
  25. println!("thing2 {:?} {:p}", thing2, thing2);
  26. }
  27. // Closing the scope ends the borrow and brings `thing` back into ownership
  28. // of the original scope
  29.  
  30. println!("thing {:?} {:p}", thing, &thing);
  31. }
Add Comment
Please, Sign In to add comment