在过go 的一周里,我一直在玩Rust
个.我似乎不知道如何在调用方法时传递定义为参数的函数,也没有遇到任何文档显示它们以这种方式使用.
在Rust
中调用函数时,是否可以在参数列表中定义函数?
这就是我到目前为止try 过的...
fn main() {
// This works
thing_to_do(able_to_pass);
// Does not work
thing_to_do(fn() {
println!("found fn in indent position");
});
// Not the same type
thing_to_do(|| {
println!("mismatched types: expected `fn()` but found `||`")
});
}
fn thing_to_do(execute: fn()) {
execute();
}
fn able_to_pass() {
println!("Hey, I worked!");
}