// https://play.rust-lang.org/?gist=9f1f7358288467c49d79f792c9ddf35c&version=stable&mode=debug
fn main() {
let f: Box<Fn(&str) -> String> = Box::new(|s| s.to_owned());
func0(f);
let f: Box<Fn(&str) -> String> = Box::new(|s| s.to_owned());
func1(f);
let f: Box<Fn(&str) -> String> = Box::new(|s| s.to_owned());
func2(f.as_ref());
let f: Box<Fn(&str) -> String> = Box::new(|s| s.to_owned());
// func3(f); // TODO how to use impl trait here?
assert_eq!(hrtb(|x| x * 2), 22*2 + 44*2);
}
fn hrtb(f: impl Fn(&u32) -> u32) -> u32 {
f(&22) + f(&44)
}
fn func0(f: Box<Fn(&str) -> String>) {
println!("Call func: {}", f("test"));
}
fn func1<F>(f: Box<F>) where F: Fn(&str) -> String + ?Sized {
println!("Call func: {}", f("test"));
}
fn func2(f: &Fn(&str) -> String) {
println!("Call func: {}", f("test"));
}
fn func3(f: impl Fn(&str) -> String) {
println!("Call func: {}", f("test"))
}
Comments