exploring error handling in Rust vs other languages, I wonder what is the most idiomatic / clean way to handle these errors in rust (where dst is deleted if Copy or Close fails) ?
What is the equivalent signature of this function in rust? Also, what is referred to by the defer keyword? Last, I presume that w stands for write and r for read?
defer is the equivalent of a function-wide c#-syle finally clause.
For cleanup of files specifically, it's the tempfile crate (you can change a temp file to a permanent one on success).
In general, for cleanups, the idiomatic way in is to take advantage of the Drop trait. For example, file.close() doesn't exist and you never have to worry about calling it, because it's always closed when file goes out of scope. In Rust the destruction is deterministic (unlike finalization with languages with GC), so it's fine to use it to also manage external resources like files on disk.
If you really like Go's approach, there's scopeguard crate that emulates it, but that's usually a bit awkward and not idiomatic.
Will allocate the file in memory and store it in src as a Vec<u8>. The ? part at the end will automatically box the error into a generic error. This could be changed to std::io::Error instead, because these operations' errors are that error. The write will replace the contents (or create a new file) of a file with the &[u8] that it's passed.
There's .map_err(|e| { fs::remove(); e })? to make it a one-liner. And if let Err(err) = fs::copy() {} if you don't care about the Ok case.
with a Drop it'd be:
fn copy_file(src_path: &Path, dst_path: &Path) -> io::Result<()> {
let src = File::open(src_path)?;
let dst = File::create(dst_path)?;
let autodelete = Autodelete(dst_path);
… do copying here …
autodelete.dont_delete_actually();
}
where you'd implement your own struct Autodelete(PathBuf) with Drop impl that calls fs::remove() on the path unless you tell it otherwise.
It may seem more boilerplate than defer, but the difference is that you need this rarely. You implement Drop once per type ever, not once per use of that type.