I get an "expected item, found keyword `let`" error and I don't know how else to write this code

use std::collections::VecDeque;

fn clear_window (wnd: &mut VecDeque<usize>, now: usize, width: usize){
        while let Some(x) = wnd.front() {
            if x + width > now {
                return;
            }
            wnd.pop_front();
        }
    }
let mut w1 = VecDeque::new();
let mut w10 = VecDeque::new();
let mut w60 = VecDeque::new();

    for t in requests {
        clear_window(&mut w1, t, 1);
        clear_window(&mut w10, t, 10);
        clear_window(&mut w60, t, 60);
        let should_drop = w1.len() >= 3 || w10.len() >= 20 || w60.len() >= 60;

        w1.push_back(t);
        w10.push_back(t);
        w60.push_back(t);
        println!("{}: {}", t, should_drop);
    }

(Playground)

Code in the linked playground works fine. Here's the playground with the code from post.

As for the error - it's simply due to the mismatched brace. To illustrate it, I've wrapped the whole code in another function and run rustfmt - here's the modified playground; you can see that everything since let mut w1 = VecDeque::new(); is outside fn clear_window.

2 Likes

Yeah, this solves the issue. Thanks for your help.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.