我目前正在尝试在Rust中实现一个简单的Parser-Combinator库.为此,我希望有一个泛型map函数来转换解析器的结果.
问题是我不知道如何复制持有闭包的结构.示例是Map以下示例中的结构.它有一个mapFunction存储函数的字段,它接收前一个解析器的结果并返回一个新结果.Map它本身就是一个可以与其他解析器进一步组合的解析器.
但是,对于要组合的解析器,我需要它们是可复制的(具有Clone特征限制),但是我该如何提供Map呢?
示例:(只有伪代码,很可能无法编译)
trait Parser<A> { // Cannot have the ": Clone" bound because of `Map`.
// Every parser needs to have a `run` function that takes the input as argument
// and optionally produces a result and the remaining input.
fn run(&self, input: ~str) -> Option<(A, ~str)>
}
struct Char {
chr: char
}
impl Parser<char> for Char {
// The char parser returns Some(char) if …Run Code Online (Sandbox Code Playgroud)