From the course: Rust Essential Training

Unlock this course with a free trial

Join today to access over 25,300 courses taught by industry experts.

Reading from files

Reading from files

- [Instructor] There's a good chance your program will need input from more than just the user in the command line. You'll often need to read data from files. Fortunately, the fs module from the Rust Standard Library for manipulating the file system provides a handful of functions to make doing that simple. The fs module is not part of the default Rust prelude. So, to use it in our program, we'll need to explicitly import it. That gives us access to some simplified functions for manipulating files. For this demonstration, let's look at how we can use it to read a file. I've included this file named planet.text in the project folder for this example. It lists the names of all eight planets in our solar system on separate lines. To read that file into our program, we'll declare a new variable to hold its contents. And then from the fs module, we'll use the read to string function. That function takes a single input…

Contents