Guest User

Untitled

a guest
Jun 18th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.78 KB | None | 0 0
  1. // Takes a function pointer from i32 to i32
  2. fn takes_a_fp(f: fn(i32) -> i32, value: i32) -> i32 {
  3. f(value)
  4. }
  5.  
  6. // Takes a closure with the trait bound Fn(i32) -> i32
  7. fn takes_a_closure<T: Fn(i32) -> i32>(f: T, value: i32) -> i32 {
  8. f(value)
  9. }
  10.  
  11. // Takes a closure with the trait bound Fn(i32) -> i32 using 1.26 impl syntax
  12. fn takes_a_closure_impl(f: impl Fn(i32) -> i32, value: i32) -> i32 {
  13. f(value)
  14. }
  15.  
  16. // Takes an optional function pointer from i32 to i32
  17. fn takes_an_opt_fp(f: Option<fn(i32) -> i32>, value: i32) -> Option<i32> {
  18. if let Some(f) = f {
  19. Some(f(value))
  20. } else {
  21. None
  22. }
  23. }
  24.  
  25. // Takes an optional closure with the trait bound Fn(i32) -> i32
  26. fn takes_an_opt_closure<T: Fn(i32) -> i32>(f: Option<T>, value: i32) -> Option<i32> {
  27. if let Some(f) = f {
  28. Some(f(value))
  29. } else {
  30. None
  31. }
  32. }
  33.  
  34. // Takes an optional closure with the trait bound Fn(i32) -> i32 using 1.26 impl syntax
  35. fn takes_an_opt_closure_impl(f: Option<impl Fn(i32) -> i32>, value: i32) -> Option<i32> {
  36. if let Some(f) = f {
  37. Some(f(value))
  38. } else {
  39. None
  40. }
  41. }
  42.  
  43. // Function from i32 -> i32
  44. fn id(x: i32) -> i32 {
  45. x
  46. }
  47.  
  48. fn main() {
  49. // Closure from i32 -> i32
  50. let closure = |x: i32| x;
  51.  
  52. println!("takes_a_fp(closure, 42): {}", takes_a_fp(closure, 42));
  53. println!("takes_a_fp(fn, 42): {}", takes_a_fp(id, 42));
  54. println!();
  55.  
  56. println!("takes_a_closure(closure, 42): {}", takes_a_closure(closure, 42));
  57. println!("takes_a_closure(fn, 42): {}", takes_a_closure(id, 42));
  58. println!();
  59.  
  60. println!("takes_a_closure_impl(closure, 42): {}", takes_a_closure_impl(closure, 42));
  61. println!("takes_a_closure_impl(fn, 42): {}", takes_a_closure_impl(id, 42));
  62. println!();
  63.  
  64. println!("takes_an_opt_fp(closure, 42): {:?}", takes_an_opt_fp(Some(closure), 42));
  65. println!("takes_an_opt_fp(fn, 42): {:?}", takes_an_opt_fp(Some(id), 42));
  66. // The type of `f` is unambiguous, therefore Rust can infer the correct type of Option<f> for None
  67. println!("takes_an_opt_fp(None, 42): {:?}", takes_an_opt_fp(None, 42));
  68. println!();
  69.  
  70. println!("takes_an_opt_closure(closure, 42): {:?}", takes_an_opt_closure(Some(closure), 42));
  71. println!("takes_an_opt_closure(fn, 42): {:?}", takes_an_opt_closure(Some(id), 42));
  72. println!("takes_an_opt_closure(None, 42): {:?}", takes_an_opt_closure(None::<fn(_) -> i32>, 42));
  73. println!();
  74.  
  75. println!("takes_an_opt_closure_impl(closure, 42): {:?}", takes_an_opt_closure_impl(Some(closure), 42));
  76. println!("takes_an_opt_closure_impl(fn, 42): {:?}", takes_an_opt_closure_impl(Some(id), 42));
  77. // The type of `f` is unambiguous, therefore Rust can infer the correct type of Option<f> for None
  78. println!("takes_an_opt_closure_impl(None, 42): {:?}", takes_an_opt_closure_impl(None::<fn(_) -> i32>, 42));
  79. }
Add Comment
Please, Sign In to add comment