Is it possible to pass an object method as argument to a function and bind it to the object?
- 阅读更多 关于 Is it possible to pass an object method as argument to a function and bind it to the object?
问题 Is it possible to make a bind to object method? For example, I have a vector and a lot of functions which do something if some item exists in the vector. I would implement it as follows: fn perform_if_exists(item: u8, vector: &Vec<u8>, func: fn(usize)) { let idx = vector.iter().position(|i| *i == item ); match idx { Some(i) => func(i), None => {}, } } fn main() { let v: Vec<u8> = vec![1, 2, 3]; perform_if_exists(1, &v, Vec<_>::remove); } but it gives a lot of errors. I think they are