SHARE
TWEET

Untitled

a guest Oct 21st, 2019 85 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #[derive(Debug)]
  2. struct Data(i32);
  3.  
  4. struct Stuff {
  5.     a: Data,
  6. }
  7. impl Stuff {
  8.     fn mutator(&mut self) -> Mutator {
  9.         Mutator { stuff: self }
  10.     }
  11.     fn get(&self) -> &Data {
  12.         &self.a
  13.     }
  14. }
  15.  
  16. struct Mutator<'s> {
  17.     stuff: &'s mut Stuff,
  18. }
  19. impl<'s> Mutator<'s> {
  20.     fn set_get(&mut self, value: Data) -> &Data {
  21.         (*self.stuff).a = value;
  22.         &self.stuff.a
  23.     }
  24. }
  25.  
  26. fn main() {
  27.     let mut stuff = Stuff { a: Data(0) };
  28.     // In teoria il riferimento a Data è valido anche
  29.     // oltre il lifetime di Mutator, perché è agganciato
  30.     // al lifetime di Stuff, ma come glielo spiego?
  31.     fn update(stuff: &mut Stuff) -> &Data {
  32.         let mut mutator = stuff.mutator();
  33.         mutator.set_get(Data(1));
  34.         stuff.get()
  35.     };
  36.     let data = update(&mut stuff);
  37.     println!("{:?}", data);
  38. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top