【发布时间】:2016-09-12 02:03:23
【问题描述】:
我正在尝试摆脱程序中的一些代码重复,我决定使用一个函数,该函数将采用 Fn() -> Result<io::Write> 类型的闭包,当我要求它时将为我提供一个输出流。
所以这是这个函数:
fn dowload_body<F>(/* some params ommited */ write_supplier: F) -> Result<()>
where F: Fn() -> Result<Write> {
if ... {
let mut destination = try!(write_supplier());
// use this stream
}
}
我想像这样使用它:
let destination_path = Path::new("some path");
let result = Self::dowload_body(|| try_str!(OpenOptions::new().append(true).open(destination_path)));
我收到以下错误:
src/http.rs:105:3: 121:4 error: the trait bound `std::io::Write + 'static: std::marker::Sized` is not satisfied [E0277]
src/http.rs:105 fn dowload_body<F>(write_supplier: F) -> Result<()>
^
我是 rust 新手,没有找到解决方案。
【问题讨论】:
标签: rust traits higher-order-functions