File system
We want to store the list of trending people in a file on a disk.
Exercise 4.a: write a function with signature fn write_to_disk(path: &str, data: Vec<String>) -> Result<(), io::Error>
which writes every string in data
on its own line in file located at path
.
The method join("\n")
applied to a Vec<String>
will return a new String
with the \n
character (line feed) inserted between each line. Do not forget to add an extra '\n' character at the end of the returned string.
To write a string content
into a file file_name
, you can use std::fs::write(file_name, content)
which returns a Result<(), std::io::Error>
.
Exercise 4.b: modify your main()
function such that you write the trending names into a file named "trending.txt"
.
You will have to add a new variant to your Error
enum in order to encapsulate the std::io::Error
if there is a problem when writing a file.
Check that the error detection and reporting works fine by attempting to write in a file you cannot write to (such as "/trending.txt"
, you most likely don't have the rights to write at the root of your file system).