我想要创建一个空文件,所以我使用了一个例子并使用了今天的夜间构建
use std::fs::File;
use std::io::prelude::*;
fn main() {
let mut f = try!(File::create("foo"));
}运行rustc有以下错误:
<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 ) ) } } )
<std macros>:1:1: 6:48 note: in expansion of try!
file_io.rs:5:17: 5:42 note: expansion site
<std macros>:5:8: 6:42 help: pass `--explain E0308` to see a detailed explanation
error: aborting due to previous error如果我删除try!,它会编译,但是我应该如何处理错误呢?为什么这个例子没有按原样编译呢?
发布于 2015-05-15 23:47:57
try!是一个宏,用于返回Result的函数。因此,它不能在main函数中使用,因为它返回单元(空元组)。
看看它是如何膨胀的:
fn main() {
let mut f = match File::create("foo") {
Ok(val) => val,
Err(err) => return Err(From::from(err)),
};
}http://blog.burntsushi.net/rust-error-handling/是一篇很好的关于锈蚀中错误处理的文章;对于像您这样简单的脚本来说,使用Result::unwrap (File::create("foo").unwrap())可能是合理的。
发布于 2015-05-15 23:44:27
让我们看一下try!()宏:
macro_rules! try {
($expr:expr) => (match $expr {
$crate::result::Result::Ok(val) => val,
$crate::result::Result::Err(err) => {
return $crate::result::Result::Err($crate::convert::From::from(err))
}
})
}如您所见,要么try!()生成val作为表达式,要么它从函数中返回Err。因此,在处理宏之后,未包装的代码如下所示:
fn main() {
let mut f = (match File::create("foo") {
Ok(val) => val,
Err(err) => {
return Err(...)
}
})
}希望现在错误是显而易见的:main()应该返回(),但是您返回一个Err (它的类型是Result<File>)。
故事的寓意是在错误的场景中使用try!()。它只应在已经设计为返回Result的函数中使用,就像在C++或Java中重新抛出(冒泡)异常一样。但是,在您的情况下,您需要显式地处理错误--不可能出现冒泡。一个可能的解决方案,虽然不是很优雅,是使用.unwrap()使程序崩溃的情况下发生Err。
https://stackoverflow.com/questions/30270193
复制相似问题