Advertisement
Guest User

Untitled

a guest
May 23rd, 2019
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.48 KB | None | 0 0
  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 mut foo = Foo::new(10);
  28.  
  29. foo.set();
  30.  
  31. println!("{}", foo.x());
  32. println!("{:?}", foo.x_ref());
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement