Can someone explain me Result , unwrap, etc. in simple understandable words?
I studied this and seems all points are clear. But if something is a bit different clarity evaporates, disappears
https://www.tutorialspoint.com/rust/rust_error_handling.htm
use std::fs::File;
let f = File::open("main.jpg"); // main.jpg doesn't exist
match f {
Ok(f)=> {
println!("file found {:?}",f);
},
Err(e)=> {
println!("file not found \n{:?}",e); //handled error
}
}
I try this, and it works fine
use std :: fs ;
fs :: remove_file ( "data.txt" ) . expect ( "could not remove file" ) ;
println ! ( "file is removed" ) ;
But it stops execution (panic). I want delete file only if it exists, if not, ok, go forward.. do not stop...
let del = fs :: remove_file ( "data.txt" ) .Result<bool,String> ;
match del {
Ok ( del ) => {
println ! ( "file foundfile is removed{:?}" , del ) ;
} ,
Err ( e ) => {
println ! ( "file not foundcould not remove file \n{:? } " , e ) ; //handled error
}
}
I could find similar code, but now I want to understand the situation deeply.
Excuse, me, if my question is annoying, distracting...