Rust 无法打开基于文档示例的文件

Rust 无法打开基于文档示例的文件,rust,Rust,我似乎无法让这辆车开始工作。我添加了主要功能等: use std::io::prelude::*; use std::fs::File; fn main(){ let mut f = try!(File::create("foo.txt")); try!(f.write_all(b"Hello, world!")); let mut f = try!(File::open("foo.txt")); let mut s = String::new();

我似乎无法让这辆车开始工作。我添加了主要功能等:

use std::io::prelude::*;
use std::fs::File;

fn main(){
    let mut f = try!(File::create("foo.txt"));
    try!(f.write_all(b"Hello, world!"));

    let mut f = try!(File::open("foo.txt"));
    let mut s = String::new();
    try!(f.read_to_string(&mut s));
    assert_eq!(s, "Hello, world!");
}
给了我这些错误:

 file git:(master) ✗ rustc main.rs -o /tmp/rrust.out
<std macros>:5:8: 6:42 error: mismatched types:
 expected `()`,
    found `core::result::Result<_, _>`
(expected (),
    found enum `core::result::Result`) [E0308]
<std macros>:5 return $ crate:: result:: Result:: Err (
<std macros>:6 $ crate:: convert:: From:: from ( err ) ) } } )
main.rs:5:17: 5:46 note: in this expansion of try! (defined in <std macros>)
<std macros>:5:8: 6:42 help: run `rustc --explain E0308` to see a detailed explanation
<std macros>:5:8: 6:42 error: mismatched types:
 expected `()`,
    found `core::result::Result<_, _>`
(expected (),
    found enum `core::result::Result`) [E0308]
<std macros>:5 return $ crate:: result:: Result:: Err (
<std macros>:6 $ crate:: convert:: From:: from ( err ) ) } } )
main.rs:6:5: 6:41 note: in this expansion of try! (defined in <std macros>)
<std macros>:5:8: 6:42 help: run `rustc --explain E0308` to see a detailed explanation
<std macros>:5:8: 6:42 error: mismatched types:
 expected `()`,
    found `core::result::Result<_, _>`
(expected (),
    found enum `core::result::Result`) [E0308]
<std macros>:5 return $ crate:: result:: Result:: Err (
<std macros>:6 $ crate:: convert:: From:: from ( err ) ) } } )
main.rs:8:17: 8:44 note: in this expansion of try! (defined in <std macros>)
<std macros>:5:8: 6:42 help: run `rustc --explain E0308` to see a detailed explanation
<std macros>:5:8: 6:42 error: mismatched types:
 expected `()`,
    found `core::result::Result<_, _>`
(expected (),
    found enum `core::result::Result`) [E0308]
<std macros>:5 return $ crate:: result:: Result:: Err (
<std macros>:6 $ crate:: convert:: From:: from ( err ) ) } } )
main.rs:10:5: 10:36 note: in this expansion of try! (defined in <std macros>)
<std macros>:5:8: 6:42 help: run `rustc --explain E0308` to see a detailed explanation
error: aborting due to 4 previous errors
文件git:(主)✗ rustc main.rs-o/tmp/rrust.out
:5:8:6:42错误:不匹配的类型:
期望`()`,
找到'core::result::result'`
(预期为(),
找到枚举'core::result::result`)[E0308]
:5返回$CLATE::结果::结果::错误(
:6$crate::convert::From::From(err))})
main.rs:5:17:5:46注:在此扩展中尝试!(定义见)
:5:8:6:42帮助:运行'rustc--explain E0308'查看详细说明
:5:8:6:42错误:不匹配的类型:
期望`()`,
找到'core::result::result'`
(预期为(),
找到枚举'core::result::result`)[E0308]
:5返回$CLATE::结果::结果::错误(
:6$crate::convert::From::From(err))})
main.rs:6:5:6:41注:在此扩展中尝试!(定义见)
:5:8:6:42帮助:运行'rustc--explain E0308'查看详细说明
:5:8:6:42错误:不匹配的类型:
期望`()`,
找到'core::result::result'`
(预期为(),
找到枚举'core::result::result`)[E0308]
:5返回$CLATE::结果::结果::错误(
:6$crate::convert::From::From(err))})
main.rs:8:17:8:44注:在此扩展中尝试!(定义见)
:5:8:6:42帮助:运行'rustc--explain E0308'查看详细说明
:5:8:6:42错误:不匹配的类型:
期望`()`,
找到'core::result::result'`
(预期为(),
找到枚举'core::result::result`)[E0308]
:5返回$CLATE::结果::结果::错误(
:6$crate::convert::From::From(err))})
main.rs:10:5:10:36注:在此扩展中尝试!(定义见)
:5:8:6:42帮助:运行'rustc--explain E0308'查看详细说明
错误:由于之前的4个错误而中止

在我看来,它确实不喜欢那些
试试语句。我使用的是Rust 1.5.0。

答案只是
展开
,而不是使用
尝试
try
用于弹出结果(这很有意义!)