审查生成的派发辅助函数

来自 移动、部分移动与丢弃
Rust 1.98 高级 7分钟 找出 4处问题

根据任务要求审查这个生成的队列操作。

索引有效时移除并返回对应任务;索引无效时保持队列不变;保留任务数据;不复制队列;不产生输出。

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)
}

生成代码仅作示例,不代表任何特定模型

在试验场中打开
报告错误