2016-03-31 12 views
-1

なぜこの錆コードはコンパイルされませんか?試してみてください!一致しない型をコンパイルしません

docsのコードとよく似ています。

コンパイルエラー:あなたは、ループ内の各ディレクトリエントリのための新たな結合をしたいよう

<std macros>:3:43: 3:46 error: mismatched types: 
expected `core::result::Result<std::fs::DirEntry, std::io::error::Error>`, 
    found `std::fs::DirEntry` 
(expected enum `core::result::Result`, 
    found struct `std::fs::DirEntry`) [E0308] 
<std macros>:3 $ crate:: result:: Result:: Ok (val) => val , $ crate:: result:: Result:: 
                 ^~~ 
src/main.rs:13:17: 13:28 note: in this expansion of try! (defined in <std macros>) 
<std macros>:3:43: 3:46 help: run `rustc --explain E0308` to see a detailed explanation 
src/main.rs:12:5: 14:6 error: mismatched types: 
expected `core::result::Result<(), std::io::error::Error>`, 
    found `()` 
(expected enum `core::result::Result`, 
    found()) [E0308] 
src/main.rs:12  for entry in try!(fs::read_dir(path)) { 
src/main.rs:13   entry = try!(entry); 
src/main.rs:14  } 
src/main.rs:12:5: 14:6 help: run `rustc --explain E0308` to see a detailed explanation 

答えて

2

に見えます:

for entry in try!(fs::read_dir(path)) { 
    let entry = try!(entry); 
} 

この新しいバインディングがfor式に導入された1つをシャドウします。

ループによって導入されたentryのタイプは、try!を使用してアンラップしようとしているResult<DirEntry>です。しかし、結果としてDirEntryをタイプResult<DirEntry>のバインディングに割り当てようとしているため、エラーです。

2番目のエラーは、関数の戻り値が、宣言された型のio::Result<()>と一致しないことを示します。 Ok(())を返すことができます:

fn do_job(path: &Path) -> io::Result<()> { 
    let mut files: Vec<&Path> = Vec::new(); 

    for entry in try!(fs::read_dir(path)) { 
     let entry = try!(entry); 
     //process entry 
    } 
    Ok(()) 
} 
+0

は同じことを行うドキュメントの例ではありませんか? –

+0

@VictorAurélio - 'entry'の前に' let'がありません。 – Lee

+0

私は 'let'を追加しましたが、まだエラーがあります(2番目のエラーのみ)。 –

関連する問題