How do I change a character in a string?

Viewed 41

I have a code like this

fn main() {
    let mut map:String = String::from(
                    "111111111111\n
                     100000000001\n
                     100000000001\n
                     100000000001\n
                     100000000001\n
                     111111111111");
    let mut count = 0;
    for i in map.chars() {
        if i == '1' {
            map.chars().nth(count).unwrap() = 'W';
        }
        else if i == '0' {
            map.chars().nth(count).unwrap() = '_';
        }
        count += 1; 
    }
}

I'm trying to draw a map on the console but this code doesn't change '1's with 'W's and '0's with '__'s. Compiler says 'invalid left-hand side of assignment'. How do I change '1's with 'W's '0's with '_'s?

I also tried 'replace_range' but now compiler says 'mutable borrow occurs here'

Here is the code

fn main() {
let mut map:String = String::from(
                "111111111111\n
                 100000000001\n
                 100000000001\n
                 100000000001\n
                 100000000001\n
                 111111111111");
let mut count:usize = 0;
for i in map.chars() {
    if i == '1' {
        map.replace_range(count..count, "W");
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^mutable borrow occurs here
    }
    count += 1;
    }
}
0 Answers
Related