Advertisement
Guest User

Untitled

a guest
Mar 18th, 2019
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.94 KB | None | 0 0
  1. fn main() {
  2. // Try to pass the result of `chain_ref` into `consume_move`.
  3. consume_move(Foo::default().chain_ref().chain_ref());
  4.  
  5. // NOTE: You can pass the result of `chain_move` to `consume_ref`, but you
  6. // can't pass the result of `chain_ref` to `consume_move`.
  7. consume_ref(&Foo::default().chain_move().chain_move());
  8. // consume_move(Foo::default().chain_ref().chain_ref()); // Doesn't compile.
  9. }
  10.  
  11. // ============================================================================
  12. // Shared definitions.
  13. // ============================================================================
  14.  
  15. #[derive(Debug, Default)]
  16. struct Foo {
  17. value: usize,
  18. }
  19.  
  20. impl Foo {
  21. fn chain_move(mut self) -> Self {
  22. self.value += 1;
  23. self
  24. }
  25.  
  26. fn chain_ref(&mut self) -> &mut Self {
  27. self.value += 1;
  28. self
  29. }
  30. }
  31.  
  32. fn consume_move(foo: Foo) {
  33. println!("{:?}", foo);
  34. }
  35.  
  36. fn consume_ref(foo: &Foo) {
  37. println!("{:?}", foo);
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement