gpt4 book ai didi

rust - 返回一个闭包,返回Rust中的函数参数

转载 作者:行者123 更新时间:2023-12-03 11:33:53 25 4
gpt4 key购买 nike

我正在尝试编写一个函数,该函数将接受类型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`.

最佳答案

这是simplified version in the Rust playground,它避免了Box和Parser。该函数使用value,并返回一个闭包,该闭包在被调用时仅返回value。请注意,闭包的类型为FnOnce(或更确切地说,是基于FnOnce的某种类型),因此我们只能调用一次。调用之后,与value关联的内存属于调用者。
如果改为使用Fn,则可以多次调用该闭包。但是在第一次调用之后,闭包不再拥有value -它已作为返回值提供给第一个调用者-因此第二次调用闭包将无法正常工作。因此,错误。如果闭包将多次返回该值,则它必须返回value的副本或克隆,并保留原始副本的所有权。由于该函数是对valueT的类型通用的,因此您需要将T约束为CopyClone

关于rust - 返回一个闭包,返回Rust中的函数参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64257293/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com