【发布时间】:2015-09-03 20:09:57
【问题描述】:
貌似是std::fs::PathExt和std::fs::metadata之间的选择,但还是建议选择后者,因为它更稳定。以下是我一直在使用的代码,因为它基于文档:
use std::fs;
pub fn path_exists(path: &str) -> bool {
let metadata = try!(fs::metadata(path));
assert!(metadata.is_file());
}
但是,由于某些奇怪的原因,let metadata = try!(fs::metadata(path)) 仍然需要函数返回 Result<T,E>,即使我只是想返回一个布尔值,如 assert!(metadata.is_file()) 所示。
尽管可能很快就会对此进行大量更改,但我如何绕过try!() 问题?
下面是相关的编译错误:
error[E0308]: mismatched types
--> src/main.rs:4:20
|
4 | let metadata = try!(fs::metadata(path));
| ^^^^^^^^^^^^^^^^^^^^^^^^ expected bool, found enum `std::result::Result`
|
= note: expected type `bool`
found type `std::result::Result<_, _>`
= note: this error originates in a macro outside of the current crate
error[E0308]: mismatched types
--> src/main.rs:3:40
|
3 | pub fn path_exists(path: &str) -> bool {
| ________________________________________^
4 | | let metadata = try!(fs::metadata(path));
5 | | assert!(metadata.is_file());
6 | | }
| |_^ expected (), found bool
|
= note: expected type `()`
found type `bool`
【问题讨论】:
-
你看过this related question关于
try!吗?还是Rust Book 错误处理部分?
标签: rust