Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #![deny(clippy::pedantic)]
- fn bind_by_ref<T>(mut f: impl FnMut(&T)) -> impl FnMut(T) {
- move |x| f(&x)
- }
- fn main() {
- let borrowed_structs = vec![BorrowedStruct, BorrowedStruct];
- //Selected into_iter specifically to reproduce the minimal scenario that closure gets value instead of reference
- borrowed_structs
- .into_iter()
- //.for_each(|consumed_struct: BorrowedStruct| MyStruct::my_method(&consumed_struct));
- // I want to write it with static method reference like following line:
- .for_each(bind_by_ref(MyStruct::my_method));
- }
- struct MyStruct;
- struct BorrowedStruct;
- impl MyStruct {
- fn my_method(prm: &BorrowedStruct) {
- prm.say_hello();
- }
- }
- impl BorrowedStruct {
- fn say_hello(&self) {
- println!("hello");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement