Hi, A brief example to capture and manipulate this error using match??
use std::{fs, io};
// yes, i know that is a photo
const ORIG: &str = "/mnt/z04/02_CONVERTIDOS/photo.jpeg";
// ==========================> MAIN
fn main() -> io::Result<()> {
for entry in fs::read_dir(ORIG)? { // inicia origen
let path = entry?.path();
// Get path string.
let path_str = path.to_str().unwrap();
println!("DIR IS: {}", path_str);
}
Ok(())
}
Output:
Error: Os { code: 20, kind: Other, message: "Not a directory" }
You can format multiline code input in your question by wrapping it with three backticks: ``` .
Your error arises here, because ORIG is not a directory.
The ? is the error propagation operator in rust. If you use ? on a Result in a function which returns a Result, your error is propagated and the function returns exactly where the error occurred. Here is the relevant section from the book about error propagation and the ? operator.
If you want to catch the error and handle it inside your function, you could write something like this instead:
match fs::read_dir(ORIG) {
Ok(entries) => {
for entry in entries {
// ...
}
}
Err(e) => {
// handle error here, in your case ORIG not being a directory
}
}