Our main function is split into three parts:
- Creating a file.
- Overwriting a file, which in this context is called truncating.
- Appending to a file.
In the first two parts, we load the entire content of the file into a single String and display it [11 and 18]. In the last one, we iterate over the individual lines in the file and print them [28].
File::open() opens a file in read-only mode and returns you a handle to it [39]. Because this handle implements the Read trait, we could now just directly read it into a string with read_to_string. However, in our examples, we wrap it first in a BufReader[42]. This is because a dedicated reader can greatly improve the performance of their resource access by collecting read instructions, which is called buffering, and executing them in big batches. For the first reading example, read_file[37], this doesn't make any difference whatsoever, as we read it all in one go anyway. We still use it because it is a good practice...