पसंद 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`
आप को देखा है [इस संबंधित सवाल] (http://stackoverflow.com/questions/30555477/try-does-not-compile) के बारे में '' कोशिश!? या [जंग बुक] (https://doc.rust-lang.org/book/error-handling.html) त्रुटि को सौंपने पर अनुभाग? – Shepmaster