Advertisement
Guest User

Untitled

a guest
Aug 18th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.66 KB | None | 0 0
  1. fn main() {
  2. // Set float and int, m and b, respectively.
  3. let (fm, fb) = (3.0, 2.0);
  4. let (im, ib) = (3, 2);
  5.  
  6. // Two closures each using floats and ints
  7. let float_line = move |x| fm * x + fb;
  8. let int_line = |x| im * x + ib;
  9.  
  10. // Interesting bit. The slope_function doesn't set
  11. // type inference to float, once the 'I' type is used.
  12. let i_line = slope_function(int_line, 2);
  13. let f_line = slope_function(float_line, 2.0);
  14.  
  15. println!("Hello, world! {:?} {:?} {:?}", float_line(2.0), i_line, f_line);
  16.  
  17. }
  18.  
  19. // Closure function taking a closure, and int as input.
  20. fn slope_function<I>(f: impl Fn(I) -> I, x: I) -> I {
  21. f(x)
  22. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement