我正在尝试编写一个函数,该函数将接受类型T的参数并返回一个闭包,该闭包在被调用时将返回该参数。 我该怎么写?
pub struct ParseError {}
pub type Parser<T> = Box<dyn Fn(&str) -> Result<(&str, T), ParseError>>;
pub fn pure<T: 'static>(value: T) -> Parser<T> {
Box::new(move |input: &str| -> Result<(&str, T), ParseError> { Ok((input, value)) })
}
错误:
Checking parsec v0.1.0 (/home/arjaz/Documents/code/rust/parsec)
error[E0507]: cannot move out of `value`, a captured variable in an `Fn` closure
--> src/parsec.rs:16:79
|
12 | pub fn pure<T: 'static>(value: T) -> Parser<T>
| ----- captured outer variable
...
16 | Box::new(move |input: &str| -> Result<(&str, T), ParseError> { Ok((input, value)) })
| ^^^^^ move occurs because `value` has type `T`, which does not implement the `Copy` trait
error: aborting due to previous error
For more information about this error, try `rustc --explain E0507`.
error: could not compile `parsec`.
答案 0 :(得分:3)
这里是simplified version in the Rust playground,可以避免Box和Parser。该函数使用value
,并返回一个闭包,该闭包在被调用时仅返回value
。请注意,闭包的类型为FnOnce
(或更准确地说,是基于FnOnce
的某种类型),因此我们只能调用一次。呼叫之后,与value
关联的内存属于呼叫者。
如果改为使用Fn
,则可以多次调用该闭包。但是在第一次调用之后,闭包不再拥有value
-它已作为返回值提供给第一个调用者-因此,第二次调用闭包将不起作用。因此,错误。如果闭包将多次返回值,则它必须返回value
的副本或副本,并保留对原始副本的所有权。由于该函数在value
,T
类型上是通用的,因此您需要将T
约束为Copy
或Clone
。