Advertisement
Guest User

Untitled

a guest
Nov 17th, 2021
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 0.65 KB | None | 0 0
  1. #![feature(unboxed_closures, fn_traits)]
  2.  
  3. struct SelfFunc {
  4.     sum: i32,
  5. }
  6.  
  7. impl FnOnce<(i32, i32)> for SelfFunc {
  8.     type Output = SelfFunc;
  9.  
  10.     extern "rust-call" fn call_once(mut self, args: (i32, i32)) -> Self::Output {
  11.         println!("Adding {}", args.0);
  12.         self.sum += args.0;
  13.         self
  14.     }
  15. }
  16.  
  17. impl FnOnce<()> for SelfFunc {
  18.     type Output = i32;
  19.  
  20.     extern "rust-call" fn call_once(self, _args: ()) -> Self::Output {
  21.         println!("Finished adding");
  22.         self.sum
  23.     }
  24. }
  25.  
  26. fn main() {
  27.     let f = SelfFunc { sum: 0 };
  28.     let a = (1, 2);
  29.     let sum = f(1, 0)(2, 0)(3, 0)();
  30.     println!("Sum = {}", sum)
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement