Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2019
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.77 KB | None | 0 0
  1. #![deny(clippy::pedantic)]
  2.  
  3. fn bind_by_ref<T>(mut f: impl FnMut(&T)) -> impl FnMut(T) {
  4. move |x| f(&x)
  5. }
  6.  
  7. fn main() {
  8. let borrowed_structs = vec![BorrowedStruct, BorrowedStruct];
  9.  
  10. //Selected into_iter specifically to reproduce the minimal scenario that closure gets value instead of reference
  11. borrowed_structs
  12. .into_iter()
  13. //.for_each(|consumed_struct: BorrowedStruct| MyStruct::my_method(&consumed_struct));
  14. // I want to write it with static method reference like following line:
  15. .for_each(bind_by_ref(MyStruct::my_method));
  16. }
  17.  
  18. struct MyStruct;
  19. struct BorrowedStruct;
  20.  
  21. impl MyStruct {
  22. fn my_method(prm: &BorrowedStruct) {
  23. prm.say_hello();
  24. }
  25. }
  26.  
  27. impl BorrowedStruct {
  28. fn say_hello(&self) {
  29. println!("hello");
  30. }
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement