Commented code can compile successfully,but I can't figure out the difference between them.
type HookBuilder = Box<dyn Fn() -> HookFunc + Send + Sync>;
type HookFunc = Box<dyn Fn(&str)>;
pub fn consumer(make_hook_func: HookBuilder) {
let f = make_hook_func;
let s = "test".to_string();
f()(s.as_str())
}
static OBJ: Owner = Owner {data: "owned data"};
struct Owner {
data: &'static str
}
pub fn main() {
// consumer(Box::new(|| OBJ.make_func()))
consumer(OBJ.make_func())
}
impl Owner {
// fn make_func(&self) -> HookFunc {
// let data: &'static str = self.data;
// if cfg!(debug_assertions) {
// Box::new(move |s| println!("{}-{}", s, data))
// } else {
// Box::new(|_| {println!("123")})
// }
// }
fn make_func(&self) -> HookBuilder {
let data: &'static str = self.data;
let f:HookFunc = if cfg!(debug_assertions) {
Box::new(move |s| println!("{}{}", s, data))
} else {
Box::new(|_| {println!("123")})
};
Box::new(|| f)
}
}