#cannot assign twice to immutable variable `i`

1 messages · Page 1 of 1 (latest)

neon tartan
#

Hey! I get this error in my first loop, and im not declaring i anywhere else, what's wrong?

#

fn main() {
    let mut input = String::new();
    let mut tape = vec![0];
    let mut ptr = 0;

    println!("Enter your Brainfuck code: ");
    io::stdin().read_line(&mut input).expect("Failed to read line");

    for i in 0..input.len() 
    {
        match input.chars().nth(i).unwrap() 
        {
            '>' => 
            {
                ptr += 1;
                if ptr == tape.len() {
                    tape.push(0);
                }
            },
            '<' => 
            {
                ptr -= 1;
            },
            '+' => 
            {
                tape[ptr] += 1;
            },
            '-' => {
                tape[ptr] -= 1;
            },
            '.' => 
            {
                print!("{}", tape[ptr] as u8 as char);
            },
            ',' => 
            {
                let mut input = String::new();
                io::stdin().read_line(&mut input).expect("Failed to read line");
                tape[ptr] = input.trim().parse().expect("Please type a number!");
            },
            '[' => 
            {
                if tape[ptr] == 0 
                {
                    let mut brac = 1;
                    while brac != 0 
                    {
                        i += 1;
                        if input.chars().nth(i).unwrap() == '[' 
                        {
                            brac += 1;
                        }
                        if input.chars().nth(i).unwrap() == ']' 
                        {
                            brac -= 1;
                        }
                    }
                }
            },
            ']' => 
            {
                if tape[ptr] != 0 
                {
                    let mut brac = 1;
                    while brac != 0 
                    {
                        i -= 1;
                        if input.chars().nth(i).unwrap() == '[' 
                        {
                            brac -= 1;
                        }
                        if input.chars().nth(i).unwrap() == ']' 
                        {
                            brac += 1;
                        }
                    }
                }
            },
            _ => {},
        }
    }
    println!("");
}```
lyric musk
#

i cannot be assigned to.

#

you could write for mut i in ... which would solve the error, but it wouldn't do what you want

#

if you want a loop where you can adjust the current index from within the loop and have that affect the next iteration, you need to declare your own variable and use while instead of for

#

(the way a for loop works is that it binds i in each iteration to a new item gotten from the iterator (in this case 0..input.len()); even if you do assign to i that doesn't change the iterator's state)

neon tartan
#

ohhh alright, thank you!