简体   繁体   中英

Declare char for comparison in rust

As part of Advent of Code 2020 day 3, I'm trying to compare characters in a string to a specific character.

fn main(){
    let str_foo = 
"...##..#
##.#..#.";
    
    for char in str_foo.chars() {
        println!("{}", char == "#");
    }
    
}

The error I get is expected char, found &str .

I'm struggling to find a clean way to cast either the left or right side of the equality check so that they can be compared.

Use single quotes for character literals. Fixed:

fn main() {
    let str_foo = 
"...##..#
##.#..#.";
    
    for char in str_foo.chars() {
        println!("{}", char == '#');
    }
}

playground

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM