我试图在Rust中使用高阶函数,我很纠结,因为我想传递的参数函数是一个捕捉值的闭包。
这是我的第一次尝试(playground link):
/// Takes a value, n, doubles it, and then applies the function f
fn double_then_f(n:u64, f: fn(u64) -> u64) -> u64 {
f(n * 2)
}
fn main() {
// Simple f closure works just fine
let example_1 = double_then_f(5, |n| n + 1);
// More realistic closure doesn't work
let dynamic_value = vec![1, 2, 3].iter().sum::<u64>();
let example_2 = double_then_f(5, |n| n + dynamic_value);
}
无法使用编译
note: expected fn pointer `fn(u64) -> u64`
found closure `[closure@src/main.rs:12:38: 12:41]`
note: closures can only be coerced to `fn` types if they do not capture any variables
在阅读了Fn
trait和fn
类型之间的区别之后,这是我的第二次尝试:
/// Takes a value, n, doubles it, and then applies the function f
fn double_then_f(n:u64, f: Box<dyn Fn(u64) -> u64>) -> u64 {
f(n * 2)
}
fn main() {
// Simple f closure works just fine
let example_1 = double_then_f(5, Box::new(|n| n + 1));
// More realistic closure doesn't work
let dynamic_value = vec![1, 2, 3].iter().sum::<u64>();
let example_2 = double_then_f(5, Box::new(|n| n + dynamic_value));
}
这也无法编译,并出现与生存期相关的错误,即“cast要求dynamic_value
被'static
借用“。我猜它所谈论的cast是将我的闭包封装到trait对象中?
我能做些什么来使这个代码工作?
1条答案
按热度按时间tzdcorbm1#
使用Fn特性!