为什么Rust在读取循环后看不到变量?

时间:2017-08-23 06:17:35

标签: rust compiler-warnings

我有一个功能,其缩写版本可能如下:

loop {
    let mut changed = false;
    for p in 0..sys.len() {
        k = ...
        if k == None { continue; }
        // some stuff
        for (v, _) in &key.0 {
           // some stuff
            loop {
                if n == 2 {
                    changed = true;
                } else if n % 2 == 0 {
                    changed = true;
                } else if n >= 2 {
                    changed = true;
                } else {
                    changed = true;
                }
            }
        }
    }
    if !changed { return; }
}

您可以看到在最后一个if语句中访问该标志。但是我收到了这条警告信息:

warning: value assigned to `changed` is never read
202 |                         changed = true;
    |                         ^^^^^^^
    |
    = note: #[warn(unused_assignments)] on by default

我没有收到任何其他警告/错误消息。这是为什么?

1 个答案:

答案 0 :(得分:10)

因为你永远不会退出内循环吗?