Advertisement
Guest User

Untitled

a guest
Mar 18th, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.83 KB | None | 0 0
  1. const SOME_CONDITION: bool = true;
  2.  
  3. fn main() {
  4. let mut foo = Foo::default();
  5. let foo = Foo::default().chain_move().chain_move();
  6. let foo = if SOME_CONDITION {
  7. foo.chain_move().chain_move().chain_move()
  8. } else {
  9. foo
  10. };
  11. consume_ref(&foo);
  12. consume_move(foo);
  13. }
  14.  
  15. // ============================================================================
  16. // Shared definitions.
  17. // ============================================================================
  18.  
  19. #[derive(Debug, Default)]
  20. struct Foo {
  21. value: usize,
  22. }
  23.  
  24. impl Foo {
  25. fn chain_move(mut self) -> Self {
  26. self.value += 1;
  27. self
  28. }
  29.  
  30. fn chain_ref(&mut self) -> &mut Self {
  31. self.value += 1;
  32. self
  33. }
  34. }
  35.  
  36. fn consume_move(foo: Foo) {
  37. println!("{:?}", foo);
  38. }
  39.  
  40. fn consume_ref(foo: &Foo) {
  41. println!("{:?}", foo);
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement