我在Rust写了一个词法分析器,与Java/C++相比,我对Rust的处理方式还很陌生.
我有一个功能类似于:
fn lookup(next_char: &mut char, f: &File) {
//if or match
if next_char == '(' {
//do something
}
}
Run Code Online (Sandbox Code Playgroud)
这给了
error: the trait `core::cmp::PartialEq<char>` is not implemented for the type `&mut char` [E0277]
if next_char == '(' {
^~~~~~~~~~~~~~~~
Run Code Online (Sandbox Code Playgroud)
如果它们被切换,那么它会给出不匹配的类型错误.我明白为什么它会给出这两个错误.我想知道是否有某种方法来比较这两个值.也许我不是在考虑Rust的方式或其他什么,但我还没有看到在文档或其他地方在网上做到这一点的好方法.
小智 9
您只需要取消引用即可从引用中获取char值:
if *next_char == '(' {
// ...
}
Run Code Online (Sandbox Code Playgroud)