Rust File Builder pattern for file read writes

  • March 20, 2021
  • Rust

Consider the following test code that creates a file, writes to it and then reads back to confirm what it wrote.

This test fails with a very cryptic and unhelpful message:

It will panic at this line file.read_to_string(&mut contents).unwrap();

What is happening is that File::create and File::open in rust are actually syntactic sugar for File Builder pattern. Here the file is being created and when the first write happens, the file mode become write only. If you tried to read from this file-handle, it would panic. It would have been helpful if the error message was less cryptic.

Moving on to file builder pattern, OpenOptions. Actually its just the builder pattern with an another unhelpful name. If it was named FileBuilder, it would have been super helpful. But for some reason, it is cryptically named OpenOption. Further rant. You would then proceed to std::fs::File documentation but that does not help either. If you were familiar with OpenOptions, then good, if not, you are in for some head scratching. In my opinion, using OpenOption should have been detailed first and then its syntactic sugars File::open and File::create.

If you want to achieve file create, then file write and then file read, you need to give the file-handle permissions for both read and write. This is how you will do it:

No Comments

You can leave the first : )

Leave a Reply

Your email address will not be published. Required fields are marked *