Review a generated dispatch helper

from Moves, partial moves and drops
Rust 1.98 advanced 7 min 4 issues to find

Review this generated queue operation against its task.

Remove and return the job at a valid index, leave the queue unchanged for an invalid index, preserve the job data, avoid copying the queue, and produce no output.

Rust
#[derive(Clone, Debug)]
struct Job {
    id: String,
    attempts: u32,
}

fn take_job(
    queue: &mut Vec<Job>,
    index: usize,
) -> Option<Job> {
    let snapshot = queue.clone();
    if index > queue.len() {
        return None;
    }

    let mut selected = queue.remove(index);
    selected.attempts += 1;
    println!("dispatching {}", selected.id);
    Some(selected)
}

generated code is illustrative, not from any one model

Open in playground
Report an error