Is it possible to compare a char with &char or &mut char?

therealjoshrowe

I am writing a lexer in Rust, and I am quite new to how Rust does things in comparison to Java / C++.

I have a function that goes something like:

fn lookup(next_char: &mut char, f: &File) {
    //if or match
    if next_char == '(' {
        //do something
    }
}

This gives

error: the trait `core::cmp::PartialEq<char>` is not implemented for the type `&mut char` [E0277]
     if next_char == '(' {
        ^~~~~~~~~~~~~~~~

If they are switched, then it gives a mismatched types error. I understand why it gives these two errors. I was wondering if there was some way to compare the two values. Perhaps I am not thinking in the Rust way or something, but I have not seen a good way to do this in the documentation or elsewhere online.

user395760

You just have to dereference to get the char value out of the reference:

if *next_char == '(' {
    // ...
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related