2017-08-23 88 views
0

我有一個功能,它的縮寫版本可能看起來像:爲什麼Rust在讀取循環後沒有看到變量?

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 

而且我沒有收到其他警告/錯誤消息。爲什麼是這樣?

回答

10

因爲你永遠不會退出內循環?

+5

漂亮的智能編譯器 – Malice

相關問題