SHARE
TWEET

Untitled

a guest May 23rd, 2019 61 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. struct Foo<'a> {
  2.     x: i32,
  3.  
  4.     /// y points to x
  5.     x_ref: Option<&'a i32>
  6. }
  7.  
  8. impl<'a> Foo<'a> {
  9.     pub fn new(x: i32) -> Self {
  10.         Self { x, x_ref: None }
  11.     }
  12.  
  13.     pub fn set(&'a mut self) {
  14.         self.x_ref = Some(&self.x);
  15.     }
  16.  
  17.     fn x(&self) -> i32 {
  18.         self.x
  19.     }
  20.  
  21.     fn x_ref(&self) -> Option<&i32> {
  22.         self.x_ref
  23.     }
  24. }
  25.  
  26. fn main() {
  27.     let foo = Foo::new(10);
  28.  
  29.     foo.set();
  30.  
  31.     println!("{}", foo.x());
  32.     println!("{:?}", foo.x_ref());
  33. }
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